Module: Rspec::GraphQLTypes

Extended by:
ActiveSupport::Concern
Defined in:
lib/rspec/graphql_types.rb,
lib/rspec/graphql_types/version.rb

Defined Under Namespace

Classes: Configuration, TestQuery

Constant Summary collapse

VERSION =
"1.0.8"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_arguments(graphql_field, field_arguments) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rspec/graphql_types.rb', line 96

def self.build_arguments(graphql_field, field_arguments)
  arguments = {}
  graphql_field.arguments.values.each do |arg|
    arguments[arg.keyword] = arg.default_value if arg.default_value?
  end
  field_arguments.each do |key, value|
    raise "Invalid Argument #{key}" unless graphql_field.arguments[key.to_s.camelize(:lower)]
    arguments[key] = value
  end
  arguments
end

.configurationObject



55
56
57
# File 'lib/rspec/graphql_types.rb', line 55

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



59
60
61
# File 'lib/rspec/graphql_types.rb', line 59

def self.configure
  yield configuration
end

.find_graphql_schemaObject



116
117
118
119
120
# File 'lib/rspec/graphql_types.rb', line 116

def self.find_graphql_schema
  ::GraphQL::Schema.subclasses
                   .filter { |schema| !schema.name&.start_with?("GraphQL::") }
                   .filter { |schema| !schema.name&.ends_with?("::SCHEMA") }
end

.schema_classObject



108
109
110
111
112
113
114
# File 'lib/rspec/graphql_types.rb', line 108

def self.schema_class
  return configuration.graphql_schema if configuration.graphql_schema

  schemas = find_graphql_schema
  raise "Could not find valid schema. Found: #{schemas.join(", ")} Use Rspec::GraphQLTypes.configure to specify the correct schema" unless schemas.length == 1
  schemas.first
end

Instance Method Details

#graphql_field(object, field, **field_arguments) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rspec/graphql_types.rb', line 84

def graphql_field(object, field, **field_arguments)
  field_name = field.to_s.camelize(:lower)
  graphql_field = object.class.fields[field_name]
  raise "Could not find field #{field_name} on #{object.class}" unless graphql_field
  arguments = GraphQLTypes.build_arguments(graphql_field, field_arguments)
  value = context.dataloader.run_isolated do
    graphql_field.resolve(object, arguments, context)
  end
  raise value if value.is_a?(Exception)
  graphql_object(graphql_field.type, value)
end

#graphql_object(type, value) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rspec/graphql_types.rb', line 63

def graphql_object(type, value)
  if value.nil?
    raise "Received a nil value for #{type}" if type.non_null?
    return nil
  end
  case type.kind.name
  when 'OBJECT'
    type.authorized_new(value, context)
  when 'NON_NULL'
    graphql_object(type.of_type, value)
  when 'SCALAR'
    type.coerce_result(value, context)
  when 'LIST'
    value.map { |v| graphql_object(type.of_type, v) }
  when 'ENUM'
    type.coerce_result(value, context)
  else
    raise "Unknown type kind #{type.kind.name}"
  end
end