Class: GraphQL::Schema

Inherits:
Object
  • Object
show all
Includes:
Define::InstanceDefinable
Defined in:
lib/graphql/schema.rb,
lib/graphql/schema/loader.rb,
lib/graphql/schema/warden.rb,
lib/graphql/schema/printer.rb,
lib/graphql/schema/null_mask.rb,
lib/graphql/schema/traversal.rb,
lib/graphql/schema/validation.rb,
lib/graphql/schema/possible_types.rb,
lib/graphql/schema/base_64_encoder.rb,
lib/graphql/schema/type_expression.rb,
lib/graphql/schema/middleware_chain.rb,
lib/graphql/schema/rescue_middleware.rb,
lib/graphql/schema/default_type_error.rb,
lib/graphql/schema/invalid_type_error.rb,
lib/graphql/schema/timeout_middleware.rb,
lib/graphql/schema/unique_within_type.rb,
lib/graphql/schema/catchall_middleware.rb,
lib/graphql/schema/default_parse_error.rb,
lib/graphql/schema/build_from_definition.rb,
lib/graphql/schema/build_from_definition/resolve_map.rb,
lib/graphql/schema/build_from_definition/resolve_map/default_resolve.rb

Overview

A GraphQL schema which may be queried with Query.

The Schema contains:

  • types for exposing your application
  • query analyzers for assessing incoming queries (including max depth & max complexity restrictions)
  • execution strategies for running incoming queries
  • middleware for interacting with execution

Schemas start with root types, #query, #mutation and #subscription. The schema will traverse the tree of fields & types, using those as starting points. Any undiscoverable types may be provided with the types configuration.

Schemas can restrict large incoming queries with max_depth and max_complexity configurations. (These configurations can be overridden by specific calls to #execute)

Schemas can specify how queries should be executed against them. query_execution_strategy, mutation_execution_strategy and subscription_execution_strategy each apply to corresponding root types.

A schema accepts a Relay::GlobalNodeIdentification instance for use with Relay IDs.

Examples:

defining a schema

MySchema = GraphQL::Schema.define do
  query QueryType
  middleware PermissionMiddleware
  rescue_from(ActiveRecord::RecordNotFound) { "Not found" }
  # If types are only connected by way of interfaces, they must be added here
  orphan_types ImageType, AudioType
end

Defined Under Namespace

Modules: Base64Encoder, BuildFromDefinition, CatchallMiddleware, DefaultParseError, DefaultTypeError, Loader, NullMask, TypeExpression, UniqueWithinType Classes: CyclicalDefinitionError, InvalidDocumentError, InvalidTypeError, MiddlewareChain, PossibleTypes, Printer, RescueMiddleware, TimeoutMiddleware, Traversal, Validation, Warden

Constant Summary collapse

BUILT_IN_TYPES =
DIRECTIVES =
[GraphQL::Directive::IncludeDirective, GraphQL::Directive::SkipDirective, GraphQL::Directive::DeprecatedDirective]
DYNAMIC_FIELDS =
["__type", "__typename", "__schema"]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

#metadata, #redefine

Constructor Details

#initializeSchema

Returns a new instance of Schema.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/graphql/schema.rb', line 111

def initialize
  @definition_error = nil
  @orphan_types = []
  @directives = DIRECTIVES.reduce({}) { |m, d| m[d.name] = d; m }
  @static_validator = GraphQL::StaticValidation::Validator.new(schema: self)
  @middleware = MiddlewareChain.new(final_step: GraphQL::Execution::Execute::FieldResolveStep)
  @query_analyzers = []
  @multiplex_analyzers = []
  @resolve_type_proc = nil
  @object_from_id_proc = nil
  @id_from_object_proc = nil
  @type_error_proc = DefaultTypeError
  @parse_error_proc = DefaultParseError
  @instrumenters = Hash.new { |h, k| h[k] = [] }
  @lazy_methods = GraphQL::Execution::Lazy::LazyMethodMap.new
  @lazy_methods.set(GraphQL::Relay::ConnectionResolve::LazyNodesWrapper, :never_called)
  @cursor_encoder = Base64Encoder
  # Default to the built-in execution strategy:
  @query_execution_strategy = self.class.default_execution_strategy
  @mutation_execution_strategy = self.class.default_execution_strategy
  @subscription_execution_strategy = self.class.default_execution_strategy
  @default_mask = GraphQL::Schema::NullMask
  @rebuilding_artifacts = false
end

Class Attribute Details

.default_execution_strategyObject

Returns the value of attribute default_execution_strategy.



96
97
98
# File 'lib/graphql/schema.rb', line 96

def default_execution_strategy
  @default_execution_strategy
end

Instance Attribute Details

#cursor_encoderObject

Returns the value of attribute cursor_encoder.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def cursor_encoder
  @cursor_encoder
end

#default_mask<#call(member, ctx)>

Returns A callable for filtering members of the schema.

Returns:

  • (<#call(member, ctx)>)

    A callable for filtering members of the schema

See Also:

  • for query-specific filters with `except:`


93
94
95
# File 'lib/graphql/schema.rb', line 93

def default_mask
  @default_mask
end

#default_max_page_sizeObject

Returns the value of attribute default_max_page_size.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def default_max_page_size
  @default_max_page_size
end

#directivesObject

Returns the value of attribute directives.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def directives
  @directives
end

#id_from_object_procObject (readonly)

Returns the value of attribute id_from_object_proc.



109
110
111
# File 'lib/graphql/schema.rb', line 109

def id_from_object_proc
  @id_from_object_proc
end

#instrumentersObject

Returns the value of attribute instrumenters.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def instrumenters
  @instrumenters
end

#lazy_methodsObject

Returns the value of attribute lazy_methods.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def lazy_methods
  @lazy_methods
end

#max_complexityObject

Returns the value of attribute max_complexity.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def max_complexity
  @max_complexity
end

#max_depthObject

Returns the value of attribute max_depth.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def max_depth
  @max_depth
end

#middlewareMiddlewareChain

Returns MiddlewareChain which is applied to fields during execution.

Returns:

  • (MiddlewareChain)

    MiddlewareChain which is applied to fields during execution



89
90
91
# File 'lib/graphql/schema.rb', line 89

def middleware
  @middleware
end

#multiplex_analyzersObject

Returns the value of attribute multiplex_analyzers.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def multiplex_analyzers
  @multiplex_analyzers
end

#mutationObject

Returns the value of attribute mutation.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def mutation
  @mutation
end

#mutation_execution_strategyObject

Returns the value of attribute mutation_execution_strategy.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def mutation_execution_strategy
  @mutation_execution_strategy
end

#object_from_id_procObject (readonly)

Returns the value of attribute object_from_id_proc.



109
110
111
# File 'lib/graphql/schema.rb', line 109

def object_from_id_proc
  @object_from_id_proc
end

#orphan_typesObject

Returns the value of attribute orphan_types.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def orphan_types
  @orphan_types
end

#queryObject

Returns the value of attribute query.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def query
  @query
end

#query_analyzersObject

Returns the value of attribute query_analyzers.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def query_analyzers
  @query_analyzers
end

#query_execution_strategyObject

Returns the value of attribute query_execution_strategy.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def query_execution_strategy
  @query_execution_strategy
end

#raise_definition_errorObject

Returns the value of attribute raise_definition_error.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def raise_definition_error
  @raise_definition_error
end

#resolve_type_procObject (readonly)

Returns the value of attribute resolve_type_proc.



109
110
111
# File 'lib/graphql/schema.rb', line 109

def resolve_type_proc
  @resolve_type_proc
end

#static_validatorObject (readonly)

Returns the value of attribute static_validator.



109
110
111
# File 'lib/graphql/schema.rb', line 109

def static_validator
  @static_validator
end

#subscriptionObject

Returns the value of attribute subscription.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def subscription
  @subscription
end

#subscription_execution_strategyObject

Returns the value of attribute subscription_execution_strategy.



75
76
77
# File 'lib/graphql/schema.rb', line 75

def subscription_execution_strategy
  @subscription_execution_strategy
end

#subscriptionsGraphQL::Subscriptions

Single, long-lived instance of the provided subscriptions class, if there is one.



86
87
88
# File 'lib/graphql/schema.rb', line 86

def subscriptions
  @subscriptions
end

Class Method Details

.from_definition(definition_or_path, default_resolve: BuildFromDefinition::DefaultResolve, parser: BuildFromDefinition::DefaultParser) ⇒ GraphQL::Schema

Create schema from an IDL schema or file containing an IDL definition.

Parameters:

  • definition_or_path (String)

    A schema definition string, or a path to a file containing the definition

  • default_resolve (<#call(type, field, obj, args, ctx)>) (defaults to: BuildFromDefinition::DefaultResolve)

    A callable for handling field resolution

  • parser (Object) (defaults to: BuildFromDefinition::DefaultParser)

    An object for handling definition string parsing (must respond to parse)

Returns:



498
499
500
501
502
503
504
505
506
# File 'lib/graphql/schema.rb', line 498

def self.from_definition(definition_or_path, default_resolve: BuildFromDefinition::DefaultResolve, parser: BuildFromDefinition::DefaultParser)
  # If the file ends in `.graphql`, treat it like a filepath
  definition = if definition_or_path.end_with?(".graphql")
    File.read(definition_or_path)
  else
    definition_or_path
  end
  GraphQL::Schema::BuildFromDefinition.from_definition(definition, default_resolve: default_resolve, parser: parser)
end

.from_introspection(introspection_result) ⇒ GraphQL::Schema

Create schema with the result of an introspection query.

Parameters:

Returns:



489
490
491
# File 'lib/graphql/schema.rb', line 489

def self.from_introspection(introspection_result)
  GraphQL::Schema::Loader.load(introspection_result)
end

Instance Method Details

#as_json(only: nil, except: nil, context: {}) ⇒ Hash

Return the Hash response of Introspection::INTROSPECTION_QUERY.

Parameters:

  • context (Hash) (defaults to: {})
  • only (<#call(member, ctx)>) (defaults to: nil)
  • except (<#call(member, ctx)>) (defaults to: nil)

Returns:

  • (Hash)

    GraphQL result



535
536
537
# File 'lib/graphql/schema.rb', line 535

def as_json(only: nil, except: nil, context: {})
  execute(Introspection::INTROSPECTION_QUERY, only: only, except: except, context: context)
end

#default_filterObject



99
100
101
# File 'lib/graphql/schema.rb', line 99

def default_filter
  GraphQL::Filter.new(except: default_mask)
end

#define(**kwargs, &block) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/graphql/schema.rb', line 188

def define(**kwargs, &block)
  super
  ensure_defined
  # Assert that all necessary configs are present:
  validation_error = Validation.validate(self)
  validation_error && raise(NotImplementedError, validation_error)
  rebuild_artifacts

  @definition_error = nil
  nil
rescue StandardError => err
  if @raise_definition_error || err.is_a?(CyclicalDefinitionError)
    raise
  else
    # Raise this error _later_ to avoid messing with Rails constant loading
    @definition_error = err
  end
  nil
end

#execute(query_str = nil, **kwargs) ⇒ Hash

Execute a query on itself. Raises an error if the schema definition is invalid.

Returns:

  • (Hash)

    query result, ready to be serialized as JSON

See Also:

  • for arguments.


255
256
257
258
259
260
261
262
# File 'lib/graphql/schema.rb', line 255

def execute(query_str = nil, **kwargs)
  if query_str
    kwargs[:query] = query_str
  end
  # Since we're running one query, don't run a multiplex-level complexity analyzer
  all_results = multiplex([kwargs], max_complexity: nil)
  all_results[0]
end

#execution_strategy_for_operation(operation) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/graphql/schema.rb', line 353

def execution_strategy_for_operation(operation)
  case operation
  when "query"
    query_execution_strategy
  when "mutation"
    mutation_execution_strategy
  when "subscription"
    subscription_execution_strategy
  else
    raise ArgumentError, "unknown operation type: #{operation}"
  end
end

#get_field(parent_type, field_name) ⇒ GraphQL::Field?

Resolve field named field_name for type parent_type. Handles dynamic fields __typename, __type and __schema, too

Parameters:

Returns:

See Also:

  • Restricted access to members of a schema


294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/graphql/schema.rb', line 294

def get_field(parent_type, field_name)
  with_definition_error_check do
    parent_type_name = case parent_type
    when GraphQL::BaseType
      parent_type.name
    when String
      parent_type
    else
      raise "Unexpected parent_type: #{parent_type}"
    end

    defined_field = @instrumented_field_map[parent_type_name][field_name]
    if defined_field
      defined_field
    elsif field_name == "__typename"
      GraphQL::Introspection::TypenameField
    elsif field_name == "__schema" && parent_type == query
      GraphQL::Introspection::SchemaField
    elsif field_name == "__type" && parent_type == query
      GraphQL::Introspection::TypeByNameField
    else
      nil
    end
  end
end

#get_fields(type) ⇒ Hash<String, GraphQL::Field>

Fields for this type, after instrumentation is applied

Returns:



322
323
324
# File 'lib/graphql/schema.rb', line 322

def get_fields(type)
  @instrumented_field_map[type.name]
end

#id_from_object(object, type, ctx) ⇒ String

Get a unique identifier from this object

Parameters:

Returns:

  • (String)

    a unique identifier for object which clients can use to refetch it



473
474
475
476
477
478
479
# File 'lib/graphql/schema.rb', line 473

def id_from_object(object, type, ctx)
  if @id_from_object_proc.nil?
    raise(NotImplementedError, "Can't generate an ID for #{object.inspect} of type #{type}, schema's `id_from_object` must be defined")
  else
    @id_from_object_proc.call(object, type, ctx)
  end
end

#id_from_object=(new_proc) ⇒ Object

Parameters:

  • new_proc (#call)

    A new callable for generating unique IDs



482
483
484
# File 'lib/graphql/schema.rb', line 482

def id_from_object=(new_proc)
  @id_from_object_proc = new_proc
end

#initialize_copy(other) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/graphql/schema.rb', line 136

def initialize_copy(other)
  super
  @orphan_types = other.orphan_types.dup
  @directives = other.directives.dup
  @static_validator = GraphQL::StaticValidation::Validator.new(schema: self)
  @middleware = other.middleware.dup
  @query_analyzers = other.query_analyzers.dup
  @multiplex_analyzers = other.multiplex_analyzers.dup

  @possible_types = GraphQL::Schema::PossibleTypes.new(self)

  @lazy_methods = other.lazy_methods.dup

  @instrumenters = Hash.new { |h, k| h[k] = [] }
  other.instrumenters.each do |key, insts|
    @instrumenters[key].concat(insts)
  end

  if other.rescues?
    @rescue_middleware = other.rescue_middleware
  end

  # This will be rebuilt when it's requested
  # or during a later `define` call
  @types = nil
end

#instrument(instrumentation_type, instrumenter) ⇒ void

This method returns an undefined value.

Attach instrumenter to this schema for instrumenting events of instrumentation_type.

Parameters:

  • instrumentation_type (Symbol)
  • instrumenter


212
213
214
215
216
217
# File 'lib/graphql/schema.rb', line 212

def instrument(instrumentation_type, instrumenter)
  @instrumenters[instrumentation_type] << instrumenter
  if instrumentation_type == :field
    rebuild_artifacts
  end
end

#lazy?(obj) ⇒ Boolean

Returns True if this object should be lazily resolved.

Returns:

  • (Boolean)

    True if this object should be lazily resolved



517
518
519
# File 'lib/graphql/schema.rb', line 517

def lazy?(obj)
  !!lazy_method_name(obj)
end

#lazy_method_name(obj) ⇒ Symbol?

Returns The method name to lazily resolve obj, or nil if obj's class wasn't registered wtih #lazy_resolve.

Returns:

  • (Symbol, nil)

    The method name to lazily resolve obj, or nil if obj's class wasn't registered wtih #lazy_resolve.



512
513
514
# File 'lib/graphql/schema.rb', line 512

def lazy_method_name(obj)
  @lazy_methods.get(obj)
end

#multiplex(queries, **kwargs) ⇒ Array<Hash>

Execute several queries on itself. Raises an error if the schema definition is invalid.

Examples:

Run several queries at once

context = { ... }
queries = [
  { query: params[:query_1], variables: params[:variables_1], context: context },
  { query: params[:query_2], variables: params[:variables_2], context: context },
]
results = MySchema.multiplex(queries)
render json: {
  result_1: results[0],
  result_2: results[1],
}

Parameters:

  • queries (Array<Hash>)

    Keyword arguments for each query

  • context (Hash)

    Multiplex-level context

Returns:

  • (Array<Hash>)

    One result for each query in the input

See Also:

  • for query keyword arguments
  • for multiplex keyword arguments


282
283
284
285
286
# File 'lib/graphql/schema.rb', line 282

def multiplex(queries, **kwargs)
  with_definition_error_check {
    GraphQL::Execution::Multiplex.run_all(self, queries, **kwargs)
  }
end

#object_from_id(id, ctx) ⇒ Any

Fetch an application object by its unique id

Parameters:

  • id (String)

    A unique identifier, provided previously by this GraphQL schema

  • ctx (GraphQL::Query::Context)

    The context for the current query

Returns:

  • (Any)

    The application object identified by id



411
412
413
414
415
416
417
# File 'lib/graphql/schema.rb', line 411

def object_from_id(id, ctx)
  if @object_from_id_proc.nil?
    raise(NotImplementedError, "Can't fetch an object for id \"#{id}\" because the schema's `object_from_id (id, ctx) -> { ... }` function is not defined")
  else
    @object_from_id_proc.call(id, ctx)
  end
end

#object_from_id=(new_proc) ⇒ Object

Parameters:

  • new_proc (#call)

    A new callable for fetching objects by ID



420
421
422
# File 'lib/graphql/schema.rb', line 420

def object_from_id=(new_proc)
  @object_from_id_proc = new_proc
end

#parse_error(err, ctx) ⇒ Object

A function to call when #execute receives an invalid query string

Parameters:

Returns:

  • void

See Also:

  • is the default behavior.


459
460
461
# File 'lib/graphql/schema.rb', line 459

def parse_error(err, ctx)
  @parse_error_proc.call(err, ctx)
end

#parse_error=(new_proc) ⇒ Object

Parameters:

  • new_proc (#call)

    A new callable for handling parse errors during execution



464
465
466
# File 'lib/graphql/schema.rb', line 464

def parse_error=(new_proc)
  @parse_error_proc = new_proc
end

#possible_types(type_defn) ⇒ Array<GraphQL::ObjectType>

Returns types which belong to type_defn in this schema.

Parameters:

Returns:

See Also:

  • Restricted access to members of a schema


333
334
335
336
# File 'lib/graphql/schema.rb', line 333

def possible_types(type_defn)
  @possible_types ||= GraphQL::Schema::PossibleTypes.new(self)
  @possible_types.possible_types(type_defn)
end

#references_to(type_name) ⇒ Hash

Returns a list of Arguments and Fields referencing a certain type

Parameters:

  • type_name (String)

Returns:

  • (Hash)


239
240
241
242
# File 'lib/graphql/schema.rb', line 239

def references_to(type_name)
  rebuild_artifacts unless defined?(@type_reference_map)
  @type_reference_map.fetch(type_name, [])
end

#remove_handler(*args, &block) ⇒ Object



167
168
169
# File 'lib/graphql/schema.rb', line 167

def remove_handler(*args, &block)
  rescue_middleware.remove_handler(*args, &block)
end

#rescue_from(*args, &block) ⇒ Object



163
164
165
# File 'lib/graphql/schema.rb', line 163

def rescue_from(*args, &block)
  rescue_middleware.rescue_from(*args, &block)
end

#resolve_type(type, object, ctx = :__undefined__) ⇒ GraphQL::ObjectType

Determine the GraphQL type for a given object. This is required for unions and interfaces (including Relay's Node interface)

Parameters:

  • type (GraphQL::UnionType, GraphQL:InterfaceType)

    the abstract type which is being resolved

  • object (Any)

    An application object which GraphQL is currently resolving on

  • ctx (GraphQL::Query::Context) (defaults to: :__undefined__)

    The context for the current query

Returns:

See Also:

  • Restricted access to members of a schema


373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/graphql/schema.rb', line 373

def resolve_type(type, object, ctx = :__undefined__)
  if ctx == :__undefined__
    # Old method signature
    ctx = object
    object = type
    type = nil
  end

  # Prefer a type-local function; fall back to the schema-level function
  type_proc = type && type.resolve_type_proc
  type_result = if type_proc
    type_proc.call(object, ctx)
  else
    if @resolve_type_proc.nil?
      raise(NotImplementedError, "Can't determine GraphQL type for: #{object.inspect}, define `resolve_type (type, obj, ctx) -> { ... }` inside `Schema.define`.")
    end
    @resolve_type_proc.call(type, object, ctx)
  end

  if type_result.nil?
    nil
  elsif !type_result.is_a?(GraphQL::BaseType)
    type_str = "#{type_result} (#{type_result.class.name})"
    raise "resolve_type(#{object}) returned #{type_str}, but it should return a GraphQL type"
  else
    type_result
  end
end

#resolve_type=(new_resolve_type_proc) ⇒ Object



402
403
404
405
# File 'lib/graphql/schema.rb', line 402

def resolve_type=(new_resolve_type_proc)
  callable = GraphQL::BackwardsCompatibility.wrap_arity(new_resolve_type_proc, from: 2, to: 3, last: true, name: "Schema#resolve_type(type, obj, ctx)")
  @resolve_type_proc = callable
end

#root_type_for_operation(operation) ⇒ GraphQL::ObjectType?

Returns:

See Also:

  • Resticted access to root types


340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/graphql/schema.rb', line 340

def root_type_for_operation(operation)
  case operation
  when "query"
    query
  when "mutation"
    mutation
  when "subscription"
    subscription
  else
    raise ArgumentError, "unknown operation type: #{operation}"
  end
end

#root_typesArray<GraphQL::BaseType>

Returns The root types of this schema.

Returns:



220
221
222
223
224
225
# File 'lib/graphql/schema.rb', line 220

def root_types
  @root_types ||= begin
    rebuild_artifacts
    @root_types
  end
end

#to_definition(only: nil, except: nil, context: {}) ⇒ String

Return the GraphQL IDL for the schema

Parameters:

  • context (Hash) (defaults to: {})
  • only (<#call(member, ctx)>) (defaults to: nil)
  • except (<#call(member, ctx)>) (defaults to: nil)

Returns:

  • (String)


526
527
528
# File 'lib/graphql/schema.rb', line 526

def to_definition(only: nil, except: nil, context: {})
  GraphQL::Schema::Printer.print_schema(self, only: only, except: except, context: context)
end

#to_json(*args) ⇒ String

Returns the JSON response of Introspection::INTROSPECTION_QUERY.

Returns:

  • (String)

See Also:

  • GraphQL::Schema.{{#as_json}


542
543
544
# File 'lib/graphql/schema.rb', line 542

def to_json(*args)
  JSON.pretty_generate(as_json(*args))
end

#type_error(err, ctx) ⇒ Object

When we encounter a type error during query execution, we call this hook.

You can use this hook to write a log entry, add a ExecutionError to the response (with ctx.add_error) or raise an exception and halt query execution.

Examples:

A nil is encountered by a non-null field

type_error ->(err, query_ctx) {
  err.is_a?(GraphQL::InvalidNullError) # => true
}

An object doesn't resolve to one of a UnionType's members

type_error ->(err, query_ctx) {
  err.is_a?(GraphQL::UnresolvedTypeError) # => true
}

Parameters:

  • err (GraphQL::TypeError)

    The error encountered during execution

  • ctx (GraphQL::Query::Context)

    The context for the field where the error occurred

Returns:

  • void

See Also:

  • is the default behavior.


444
445
446
# File 'lib/graphql/schema.rb', line 444

def type_error(err, ctx)
  @type_error_proc.call(err, ctx)
end

#type_error=(new_proc) ⇒ Object

Parameters:

  • new_proc (#call)

    A new callable for handling type errors during execution



449
450
451
# File 'lib/graphql/schema.rb', line 449

def type_error=(new_proc)
  @type_error_proc = new_proc
end

#type_from_ast(ast_node) ⇒ Object



326
327
328
# File 'lib/graphql/schema.rb', line 326

def type_from_ast(ast_node)
  GraphQL::Schema::TypeExpression.build_type(self.types, ast_node)
end

#typesGraphQL::Schema::TypeMap

Returns { name => type } pairs of types in this schema.

Returns:

  • (GraphQL::Schema::TypeMap)

    { name => type } pairs of types in this schema

See Also:

  • Restricted access to members of a schema


229
230
231
232
233
234
# File 'lib/graphql/schema.rb', line 229

def types
  @types ||= begin
    rebuild_artifacts
    @types
  end
end

#union_memberships(type) ⇒ Array<GraphQL::UnionType>

Returns a list of Union types in which a type is a member

Parameters:

Returns:



247
248
249
250
# File 'lib/graphql/schema.rb', line 247

def union_memberships(type)
  rebuild_artifacts unless defined?(@union_memberships)
  @union_memberships.fetch(type.name, [])
end

#validate(string_or_document, rules: nil) ⇒ Array<GraphQL::StaticValidation::Message>

Validate a query string according to this schema.

Parameters:

Returns:



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/graphql/schema.rb', line 174

def validate(string_or_document, rules: nil)
  doc = if string_or_document.is_a?(String)
    GraphQL.parse(string_or_document)
  else
    string_or_document
  end
  query = GraphQL::Query.new(self, document: doc)
  validator_opts = { schema: self }
  rules && (validator_opts[:rules] = rules)
  validator = GraphQL::StaticValidation::Validator.new(validator_opts)
  res = validator.validate(query)
  res[:errors]
end