Module: GraphQL::Testing::Helpers
- Included in:
- SchemaHelpers
- Defined in:
- lib/graphql/testing/helpers.rb
Defined Under Namespace
Modules: SchemaHelpers Classes: Error, FieldNotDefinedError, FieldNotVisibleError, ResolutionAssertionContext, TypeNotDefinedError, TypeNotVisibleError
Class Method Summary collapse
-
.for(schema_class) ⇒ Module
A helpers module which always uses the given schema.
Instance Method Summary collapse
-
#run_graphql_field(schema, field_path, object, arguments: {}, context: {}, ast_node: nil, lookahead: nil) ⇒ Object
-
#with_resolution_context(schema, type:, object:, context: {}) {|resolution_context| ... } ⇒ Object
Class Method Details
.for(schema_class) ⇒ Module
Returns A helpers module which always uses the given schema.
7 8 9 |
# File 'lib/graphql/testing/helpers.rb', line 7 def self.for(schema_class) SchemaHelpers.for(schema_class) end |
Instance Method Details
#run_graphql_field(schema, field_path, object, arguments: {}, context: {}, ast_node: nil, lookahead: nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/graphql/testing/helpers.rb', line 42 def run_graphql_field(schema, field_path, object, arguments: {}, context: {}, ast_node: nil, lookahead: nil) type_name, *field_names = field_path.split(".") dummy_query = GraphQL::Query.new(schema, "{ __typename }", context: context) query_context = dummy_query.context object_type = dummy_query.types.type(type_name) # rubocop:disable Development/ContextIsPassedCop if object_type graphql_result = object field_names.each do |field_name| inner_object = graphql_result graphql_result = object_type.wrap(inner_object, query_context) if graphql_result.nil? return nil end visible_field = dummy_query.types.field(object_type, field_name) # rubocop:disable Development/ContextIsPassedCop if visible_field dummy_query.context.dataloader.run_isolated { field_args = visible_field.coerce_arguments(graphql_result, arguments, query_context) field_args = schema.sync_lazy(field_args) if visible_field.extras.any? extra_args = {} visible_field.extras.each do |extra| extra_args[extra] = case extra when :ast_node ast_node ||= GraphQL::Language::Nodes::Field.new(name: visible_field.graphql_name) when :lookahead lookahead ||= begin ast_node ||= GraphQL::Language::Nodes::Field.new(name: visible_field.graphql_name) Execution::Lookahead.new( query: dummy_query, ast_nodes: [ast_node], field: visible_field, ) end else raise ArgumentError, "This extra isn't supported in `run_graphql_field` yet: `#{extra.inspect}`. Open an issue on GitHub to request it: https://github.com/rmosolgo/graphql-ruby/issues/new" end end field_args = field_args.merge_extras(extra_args) end graphql_result = visible_field.resolve(graphql_result, field_args.keyword_arguments, query_context) graphql_result = schema.sync_lazy(graphql_result) } object_type = visible_field.type.unwrap elsif object_type.all_field_definitions.any? { |f| f.graphql_name == field_name } raise FieldNotVisibleError.new(field_name: field_name, type_name: type_name) else raise FieldNotDefinedError.new(type_name: type_name, field_name: field_name) end end graphql_result elsif schema.has_defined_type?(type_name) raise TypeNotVisibleError.new(type_name: type_name) else raise TypeNotDefinedError.new(type_name: type_name) end end |
#with_resolution_context(schema, type:, object:, context: {}) {|resolution_context| ... } ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/graphql/testing/helpers.rb', line 100 def with_resolution_context(schema, type:, object:, context:{}) resolution_context = ResolutionAssertionContext.new( self, schema: schema, type_name: type, object: object, context: context ) yield(resolution_context) end |