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

Instance Method Summary collapse

Class Method Details

.for(schema_class) ⇒ Module

Returns A helpers module which always uses the given schema.

Parameters:

Returns:

  • (Module)

    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: {}) ⇒ 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
# File 'lib/graphql/testing/helpers.rb', line 42

def run_graphql_field(schema, field_path, object, arguments: {}, context: {})
  type_name, *field_names = field_path.split(".")
  dummy_query = GraphQL::Query.new(schema, context: context)
  query_context = dummy_query.context
  object_type = dummy_query.get_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.get_field(object_type, field_name)
      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)
          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

Yields:

  • (resolution_context)


78
79
80
81
82
83
84
85
86
87
# File 'lib/graphql/testing/helpers.rb', line 78

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