Class: GraphQL::Schema

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, FindInheritedValue, Member::AcceptsDefinition, Member::HasAstNode
Includes:
Define::InstanceDefinable
Defined in:
lib/graphql/schema.rb,
lib/graphql/schema/enum.rb,
lib/graphql/schema/list.rb,
lib/graphql/schema/field.rb,
lib/graphql/schema/union.rb,
lib/graphql/schema/finder.rb,
lib/graphql/schema/loader.rb,
lib/graphql/schema/member.rb,
lib/graphql/schema/object.rb,
lib/graphql/schema/scalar.rb,
lib/graphql/schema/warden.rb,
lib/graphql/schema/printer.rb,
lib/graphql/schema/timeout.rb,
lib/graphql/schema/wrapper.rb,
lib/graphql/schema/argument.rb,
lib/graphql/schema/mutation.rb,
lib/graphql/schema/non_null.rb,
lib/graphql/schema/resolver.rb,
lib/graphql/schema/directive.rb,
lib/graphql/schema/interface.rb,
lib/graphql/schema/null_mask.rb,
lib/graphql/schema/traversal.rb,
lib/graphql/schema/enum_value.rb,
lib/graphql/schema/validation.rb,
lib/graphql/schema/input_object.rb,
lib/graphql/schema/subscription.rb,
lib/graphql/schema/member/scoped.rb,
lib/graphql/schema/built_in_types.rb,
lib/graphql/schema/directive/skip.rb,
lib/graphql/schema/possible_types.rb,
lib/graphql/schema/base_64_encoder.rb,
lib/graphql/schema/field_extension.rb,
lib/graphql/schema/late_bound_type.rb,
lib/graphql/schema/member/has_path.rb,
lib/graphql/schema/type_expression.rb,
lib/graphql/schema/type_membership.rb,
lib/graphql/schema/middleware_chain.rb,
lib/graphql/schema/directive/feature.rb,
lib/graphql/schema/directive/include.rb,
lib/graphql/schema/member/build_type.rb,
lib/graphql/schema/member/has_fields.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/directive/transform.rb,
lib/graphql/schema/member/has_ast_node.rb,
lib/graphql/schema/directive/deprecated.rb,
lib/graphql/schema/find_inherited_value.rb,
lib/graphql/schema/introspection_system.rb,
lib/graphql/schema/member/has_arguments.rb,
lib/graphql/schema/build_from_definition.rb,
lib/graphql/schema/field/scope_extension.rb,
lib/graphql/schema/member/instrumentation.rb,
lib/graphql/schema/member/relay_shortcuts.rb,
lib/graphql/schema/member/validates_input.rb,
lib/graphql/schema/relay_classic_mutation.rb,
lib/graphql/schema/member/base_dsl_methods.rb,
lib/graphql/schema/member/accepts_definition.rb,
lib/graphql/schema/member/graphql_type_names.rb,
lib/graphql/schema/resolver/has_payload_type.rb,
lib/graphql/schema/field/connection_extension.rb,
lib/graphql/schema/member/type_system_helpers.rb,
lib/graphql/schema/member/cached_graphql_definition.rb,
lib/graphql/schema/build_from_definition/resolve_map.rb,
lib/graphql/schema/build_from_definition/resolve_map/default_resolve.rb

Overview

Extend this class to define GraphQL enums in your schema.

By default, GraphQL enum values are translated into Ruby strings. You can provide a custom value with the value: keyword.

Examples:

# equivalent to
# enum PizzaTopping {
#   MUSHROOMS
#   ONIONS
#   PEPPERS
# }
class PizzaTopping < GraphQL::Enum
  value :MUSHROOMS
  value :ONIONS
  value :PEPPERS
end

Defined Under Namespace

Modules: Base64Encoder, BuildFromDefinition, CatchallMiddleware, DefaultParseError, DefaultTypeError, FindInheritedValue, Interface, Loader, NullMask, ResolveTypeWithType, TypeExpression, UniqueWithinType Classes: Argument, CyclicalDefinitionError, Directive, DuplicateTypeNamesError, Enum, EnumValue, Field, FieldExtension, Finder, InputObject, IntrospectionSystem, InvalidDocumentError, InvalidTypeError, LateBoundType, List, Member, MiddlewareChain, Mutation, NonNull, Object, PossibleTypes, Printer, RelayClassicMutation, RescueMiddleware, Resolver, Scalar, Subscription, Timeout, TimeoutMiddleware, Traversal, TypeMembership, Union, UnresolvedLateBoundTypeError, Validation, Warden, Wrapper

Constant Summary collapse

DYNAMIC_FIELDS =
["__type", "__typename", "__schema"].freeze
BUILT_IN_TYPES =
{
  "Int" => GraphQL::Types::Int,
  "String" => GraphQL::Types::String,
  "Float" => GraphQL::Types::Float,
  "Boolean" => GraphQL::Types::Boolean,
  "ID" => GraphQL::Types::ID,
}

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.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/graphql/schema.rb', line 204

def initialize
  @tracers = []
  @definition_error = nil
  @orphan_types = []
  @directives = {}
  self.class.default_directives.each do |name, dir|
    @directives[name] = dir.graphql_definition
  end
  @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::Execution::Lazy, :value)
  @cursor_encoder = Base64Encoder
  # Default to the built-in execution strategy:
  @analysis_engine = GraphQL::Analysis
  @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
  @context_class = GraphQL::Query::Context
  @introspection_namespace = nil
  @introspection_system = nil
  @interpreter = false
  @error_bubbling = false
  @disable_introspection_entry_points = false
  @disable_schema_introspection_entry_point = false
  @disable_type_introspection_entry_point = false
end

Class Attribute Details

.analysis_engineObject



1210
1211
1212
# File 'lib/graphql/schema.rb', line 1210

def analysis_engine
  @analysis_engine || find_inherited_value(:analysis_engine, GraphQL::Analysis)
end

.connectionsGraphQL::Pagination::Connections

Returns if installed.

Returns:



961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/graphql/schema.rb', line 961

def connections
  if defined?(@connections)
    @connections
  else
    inherited_connections = find_inherited_value(:connections, nil)
    # This schema is part of an inheritance chain which is using new connections,
    # make a new instance, so we don't pollute the upstream one.
    if inherited_connections
      @connections = Pagination::Connections.new(schema: self)
    else
      nil
    end
  end
end

.default_execution_strategyObject



1303
1304
1305
1306
1307
1308
1309
# File 'lib/graphql/schema.rb', line 1303

def default_execution_strategy
  if superclass <= GraphQL::Schema
    superclass.default_execution_strategy
  else
    @default_execution_strategy ||= GraphQL::Execution::Execute
  end
end

.error_bubbling(new_error_bubbling = nil) ⇒ Object



1228
1229
1230
1231
1232
1233
1234
# File 'lib/graphql/schema.rb', line 1228

def error_bubbling(new_error_bubbling = nil)
  if !new_error_bubbling.nil?
    @error_bubbling = new_error_bubbling
  else
    @error_bubbling.nil? ? find_inherited_value(:error_bubbling) : @error_bubbling
  end
end

.error_handlerGraphQL::Execution::Errors, Class<GraphQL::Execution::Errors::NullErrorHandler>



1444
1445
1446
# File 'lib/graphql/schema.rb', line 1444

def error_handler
  @error_handler ||= GraphQL::Execution::Errors::NullErrorHandler
end

.interpreter=(value) ⇒ Object (writeonly)

Sets the attribute interpreter

Parameters:

  • value

    the value to set the attribute interpreter to.



1226
1227
1228
# File 'lib/graphql/schema.rb', line 1226

def interpreter=(value)
  @interpreter = value
end

.max_complexity(max_complexity = nil) ⇒ Object



1198
1199
1200
1201
1202
1203
1204
1205
1206
# File 'lib/graphql/schema.rb', line 1198

def max_complexity(max_complexity = nil)
  if max_complexity
    @max_complexity = max_complexity
  elsif defined?(@max_complexity)
    @max_complexity
  else
    find_inherited_value(:max_complexity)
  end
end

.max_depth(new_max_depth = nil) ⇒ Object



1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/graphql/schema.rb', line 1240

def max_depth(new_max_depth = nil)
  if new_max_depth
    @max_depth = new_max_depth
  elsif defined?(@max_depth)
    @max_depth
  else
    find_inherited_value(:max_depth)
  end
end

.subscriptionsGraphQL::Subscriptions



810
811
812
# File 'lib/graphql/schema.rb', line 810

def subscriptions
  @subscriptions
end

Instance Attribute Details

#analysis_engineObject

Returns the value of attribute analysis_engine.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def analysis_engine
  @analysis_engine
end

#ast_nodeObject

Returns the value of attribute ast_node.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def ast_node
  @ast_node
end

#connectionsObject

Returns the value of attribute connections.



791
792
793
# File 'lib/graphql/schema.rb', line 791

def connections
  @connections
end

#context_classClass

Returns Instantiated for each query.

Returns:

  • (Class)

    Instantiated for each query

See Also:

  • The parent class of these classes


165
166
167
# File 'lib/graphql/schema.rb', line 165

def context_class
  @context_class
end

#cursor_encoderObject

Returns the value of attribute cursor_encoder.



137
138
139
# File 'lib/graphql/schema.rb', line 137

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:`


161
162
163
# File 'lib/graphql/schema.rb', line 161

def default_mask
  @default_mask
end

#default_max_page_sizeObject

Returns the value of attribute default_max_page_size.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def default_max_page_size
  @default_max_page_size
end

#directivesObject

Returns the value of attribute directives.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def directives
  @directives
end

#disable_introspection_entry_pointsObject

[Boolean] True if this object disables the introspection entry point fields



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

def disable_introspection_entry_points
  @disable_introspection_entry_points
end

#disable_schema_introspection_entry_pointObject

[Boolean] True if this object disables the __schema introspection entry point field



175
176
177
# File 'lib/graphql/schema.rb', line 175

def disable_schema_introspection_entry_point
  @disable_schema_introspection_entry_point
end

#disable_type_introspection_entry_pointObject

[Boolean] True if this object disables the __type introspection entry point field



182
183
184
# File 'lib/graphql/schema.rb', line 182

def disable_type_introspection_entry_point
  @disable_type_introspection_entry_point
end

#error_bubblingObject

[Boolean] True if this object bubbles validation errors up from a field into its parent InputObject, if there is one.



150
151
152
# File 'lib/graphql/schema.rb', line 150

def error_bubbling
  @error_bubbling
end

#id_from_object_procObject (readonly)

Returns the value of attribute id_from_object_proc.



202
203
204
# File 'lib/graphql/schema.rb', line 202

def id_from_object_proc
  @id_from_object_proc
end

#instrumentersObject

Returns the value of attribute instrumenters.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def instrumenters
  @instrumenters
end

#interpreter=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def interpreter=(value)
  @interpreter = value
end

#introspection_namespaceObject

Returns the value of attribute introspection_namespace.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def introspection_namespace
  @introspection_namespace
end

#lazy_methodsObject

Returns the value of attribute lazy_methods.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def lazy_methods
  @lazy_methods
end

#max_complexityObject

Returns the value of attribute max_complexity.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def max_complexity
  @max_complexity
end

#max_depthObject

Returns the value of attribute max_depth.



137
138
139
# File 'lib/graphql/schema.rb', line 137

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



157
158
159
# File 'lib/graphql/schema.rb', line 157

def middleware
  @middleware
end

#multiplex_analyzersObject

Returns the value of attribute multiplex_analyzers.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def multiplex_analyzers
  @multiplex_analyzers
end

#mutationObject

Returns the value of attribute mutation.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def mutation
  @mutation
end

#mutation_execution_strategyObject

Returns the value of attribute mutation_execution_strategy.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def mutation_execution_strategy
  @mutation_execution_strategy
end

#object_from_id_procObject (readonly)

Returns the value of attribute object_from_id_proc.



202
203
204
# File 'lib/graphql/schema.rb', line 202

def object_from_id_proc
  @object_from_id_proc
end

#orphan_typesObject

Returns the value of attribute orphan_types.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def orphan_types
  @orphan_types
end

#queryObject

Returns the value of attribute query.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def query
  @query
end

#query_analyzersObject

Returns the value of attribute query_analyzers.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def query_analyzers
  @query_analyzers
end

#query_execution_strategyObject

Returns the value of attribute query_execution_strategy.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def query_execution_strategy
  @query_execution_strategy
end

#raise_definition_errorObject

Returns the value of attribute raise_definition_error.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def raise_definition_error
  @raise_definition_error
end

#resolve_type_procObject (readonly)

Returns the value of attribute resolve_type_proc.



202
203
204
# File 'lib/graphql/schema.rb', line 202

def resolve_type_proc
  @resolve_type_proc
end

#static_validatorObject (readonly)

Returns the value of attribute static_validator.



202
203
204
# File 'lib/graphql/schema.rb', line 202

def static_validator
  @static_validator
end

#subscriptionObject

Returns the value of attribute subscription.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def subscription
  @subscription
end

#subscription_execution_strategyObject

Returns the value of attribute subscription_execution_strategy.



137
138
139
# File 'lib/graphql/schema.rb', line 137

def subscription_execution_strategy
  @subscription_execution_strategy
end

#subscriptionsGraphQL::Subscriptions

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



154
155
156
# File 'lib/graphql/schema.rb', line 154

def subscriptions
  @subscriptions
end

#tracersArray<#trace(key, data)> (readonly)

Returns Tracers applied to every query.

Returns:

  • (Array<#trace(key, data)>)

    Tracers applied to every query

See Also:

  • for query-specific tracers


198
199
200
# File 'lib/graphql/schema.rb', line 198

def tracers
  @tracers
end

Class Method Details

.accessible?(member, ctx) ⇒ Boolean

Returns:

  • (Boolean)


1377
1378
1379
# File 'lib/graphql/schema.rb', line 1377

def accessible?(member, ctx)
  member.type_class.accessible?(ctx)
end

.after_lazy(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Call the given block at the right time, either: - Right away, if value is not registered with lazy_resolve - After resolving value, if it’s registered with lazy_resolve (eg, Promise)



1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
# File 'lib/graphql/schema.rb', line 1583

def after_lazy(value)
  if lazy?(value)
    GraphQL::Execution::Lazy.new do
      result = sync_lazy(value)
      # The returned result might also be lazy, so check it, too
      after_lazy(result) do |final_result|
        yield(final_result) if block_given?
      end
    end
  else
    yield(value) if block_given?
  end
end

.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



824
825
826
# File 'lib/graphql/schema.rb', line 824

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

.context_class(new_context_class = nil) ⇒ Object



1311
1312
1313
1314
1315
1316
1317
# File 'lib/graphql/schema.rb', line 1311

def context_class(new_context_class = nil)
  if new_context_class
    @context_class = new_context_class
  else
    @context_class || find_inherited_value(:context_class, GraphQL::Query::Context)
  end
end

.cursor_encoder(new_encoder = nil) ⇒ Object



1157
1158
1159
1160
1161
1162
# File 'lib/graphql/schema.rb', line 1157

def cursor_encoder(new_encoder = nil)
  if new_encoder
    @cursor_encoder = new_encoder
  end
  @cursor_encoder || find_inherited_value(:cursor_encoder, Base64Encoder)
end

.default_directivesObject



1479
1480
1481
1482
1483
1484
1485
# File 'lib/graphql/schema.rb', line 1479

def default_directives
  {
    "include" => GraphQL::Schema::Directive::Include,
    "skip" => GraphQL::Schema::Directive::Skip,
    "deprecated" => GraphQL::Schema::Directive::Deprecated,
  }
end

.default_filterObject



855
856
857
# File 'lib/graphql/schema.rb', line 855

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

.default_mask(new_mask = nil) ⇒ Object



859
860
861
862
863
864
865
# File 'lib/graphql/schema.rb', line 859

def default_mask(new_mask = nil)
  if new_mask
    @own_default_mask = new_mask
  else
    @own_default_mask || find_inherited_value(:default_mask, Schema::NullMask)
  end
end

.default_max_page_size(new_default_max_page_size = nil) ⇒ Object



1164
1165
1166
1167
1168
1169
1170
# File 'lib/graphql/schema.rb', line 1164

def default_max_page_size(new_default_max_page_size = nil)
  if new_default_max_page_size
    @default_max_page_size = new_default_max_page_size
  else
    @default_max_page_size || find_inherited_value(:default_max_page_size)
  end
end

.directive(new_directive) ⇒ Object

Attach a single directive to this schema

Parameters:

  • new_directive (Class)


1474
1475
1476
1477
# File 'lib/graphql/schema.rb', line 1474

def directive(new_directive)
  add_type_and_traverse(new_directive, root: false)
  own_directives[new_directive.graphql_name] = new_directive
end

.directives(new_directives = nil) ⇒ Object

Add several directives at once

Parameters:

  • new_directives (Class) (defaults to: nil)


1464
1465
1466
1467
1468
1469
1470
# File 'lib/graphql/schema.rb', line 1464

def directives(new_directives = nil)
  if new_directives
    new_directives.each { |d| directive(d) }
  end

  find_inherited_value(:directives, default_directives).merge(own_directives)
end

.disable_introspection_entry_pointsObject



1250
1251
1252
1253
1254
# File 'lib/graphql/schema.rb', line 1250

def disable_introspection_entry_points
  @disable_introspection_entry_points = true
  # TODO: this clears the cache made in `def types`. But this is not a great solution.
  @introspection_system = nil
end

.disable_introspection_entry_points?Boolean

Returns:

  • (Boolean)


1268
1269
1270
1271
1272
1273
1274
# File 'lib/graphql/schema.rb', line 1268

def disable_introspection_entry_points?
  if instance_variable_defined?(:@disable_introspection_entry_points)
    @disable_introspection_entry_points
  else
    find_inherited_value(:disable_introspection_entry_points?, false)
  end
end

.disable_schema_introspection_entry_pointObject



1256
1257
1258
1259
1260
# File 'lib/graphql/schema.rb', line 1256

def disable_schema_introspection_entry_point
  @disable_schema_introspection_entry_point = true
  # TODO: this clears the cache made in `def types`. But this is not a great solution.
  @introspection_system = nil
end

.disable_schema_introspection_entry_point?Boolean

Returns:

  • (Boolean)


1276
1277
1278
1279
1280
1281
1282
# File 'lib/graphql/schema.rb', line 1276

def disable_schema_introspection_entry_point?
  if instance_variable_defined?(:@disable_schema_introspection_entry_point)
    @disable_schema_introspection_entry_point
  else
    find_inherited_value(:disable_schema_introspection_entry_point?, false)
  end
end

.disable_type_introspection_entry_pointObject



1262
1263
1264
1265
1266
# File 'lib/graphql/schema.rb', line 1262

def disable_type_introspection_entry_point
  @disable_type_introspection_entry_point = true
  # TODO: this clears the cache made in `def types`. But this is not a great solution.
  @introspection_system = nil
end

.disable_type_introspection_entry_point?Boolean

Returns:

  • (Boolean)


1284
1285
1286
1287
1288
1289
1290
# File 'lib/graphql/schema.rb', line 1284

def disable_type_introspection_entry_point?
  if instance_variable_defined?(:@disable_type_introspection_entry_point)
    @disable_type_introspection_entry_point
  else
    find_inherited_value(:disable_type_introspection_entry_point?, false)
  end
end

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

Execute a query on itself.

Returns:

  • (Hash)

    query result, ready to be serialized as JSON

See Also:

  • for arguments.


1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
# File 'lib/graphql/schema.rb', line 1526

def execute(query_str = nil, **kwargs)
  if query_str
    kwargs[:query] = query_str
  end
  # Some of the query context _should_ be passed to the multiplex, too
  multiplex_context = if (ctx = kwargs[:context])
    {
      backtrace: ctx[:backtrace],
      tracers: ctx[:tracers],
    }
  else
    {}
  end
  # Since we're running one query, don't run a multiplex-level complexity analyzer
  all_results = multiplex([kwargs], max_complexity: nil, context: multiplex_context)
  all_results[0]
end

.find(path) ⇒ Object



843
844
845
846
847
848
849
# File 'lib/graphql/schema.rb', line 843

def find(path)
  if !@finder
    @find_cache = {}
    @finder ||= GraphQL::Schema::Finder.new(self)
  end
  @find_cache[path] ||= @finder.find(path)
end

.from_definition(definition_or_path, default_resolve: nil, interpreter: true, parser: BuildFromDefinition::DefaultParser, using: {}) ⇒ Class

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: nil)

    A callable for handling field resolution

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

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

  • using (Hash) (defaults to: {})

    Plugins to attach to the created schema with use(key, value)

  • interpreter (Boolean) (defaults to: true)

    If false, the legacy Execution::Execute runtime will be used

Returns:

  • (Class)

    the schema described by document



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
# File 'lib/graphql/schema.rb', line 724

def self.from_definition(definition_or_path, default_resolve: nil, interpreter: true, parser: BuildFromDefinition::DefaultParser, using: {})
  # 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,
    using: using,
    interpreter: interpreter,
  )
end

.from_introspection(introspection_result) ⇒ GraphQL::Schema

Create schema with the result of an introspection query.

Parameters:

Returns:



713
714
715
# File 'lib/graphql/schema.rb', line 713

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

.get_field(type_or_name, field_name) ⇒ Object



1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'lib/graphql/schema.rb', line 1112

def get_field(type_or_name, field_name)
  parent_type = case type_or_name
  when LateBoundType
    get_type(type_or_name.name)
  when String
    get_type(type_or_name)
  when Module
    type_or_name
  else
    raise ArgumentError, "unexpected field owner for #{field_name.inspect}: #{type_or_name.inspect} (#{type_or_name.class})"
  end

  if parent_type.kind.fields? && (field = parent_type.get_field(field_name))
    field
  elsif parent_type == query && (entry_point_field = introspection_system.entry_point(name: field_name))
    entry_point_field
  elsif (dynamic_field = introspection_system.dynamic_field(name: field_name))
    dynamic_field
  else
    nil
  end
end

.get_fields(type) ⇒ Object



1135
1136
1137
# File 'lib/graphql/schema.rb', line 1135

def get_fields(type)
  type.fields
end

.get_type(type_name) ⇒ Module?

Returns A type, or nil if there’s no type called type_name.

Parameters:

  • type_name (String)

Returns:

  • (Module, nil)

    A type, or nil if there’s no type called type_name



951
952
953
954
955
# File 'lib/graphql/schema.rb', line 951

def get_type(type_name)
  own_types[type_name] ||
    introspection_system.types[type_name] ||
    find_inherited_value(:types, EMPTY_HASH)[type_name]
end

.graphql_definitionObject



851
852
853
# File 'lib/graphql/schema.rb', line 851

def graphql_definition
  @graphql_definition ||= to_graphql
end

.id_from_object(object, type, ctx) ⇒ Object



1369
1370
1371
# File 'lib/graphql/schema.rb', line 1369

def id_from_object(object, type, ctx)
  raise GraphQL::RequiredImplementationMissingError, "#{self.name}.id_from_object(object, type, ctx) must be implemented to create global ids (tried to create an id for `#{object.inspect}`)"
end

.inaccessible_fields(error) ⇒ AnalysisError?

This hook is called when a client tries to access one or more fields that fail the accessible? check.

By default, an error is added to the response. Override this hook to track metrics or return a different error to the client.

Parameters:

  • error (InaccessibleFieldsError)

    The analysis error for this check

Returns:



1389
1390
1391
# File 'lib/graphql/schema.rb', line 1389

def inaccessible_fields(error)
  error
end

.inherited(child_class) ⇒ Object

rubocop:enable Lint/DuplicateMethods



1353
1354
1355
1356
1357
1358
1359
# File 'lib/graphql/schema.rb', line 1353

def inherited(child_class)
  if self == GraphQL::Schema
    child_class.directives(default_directives.values)
  end
  child_class.singleton_class.prepend(ResolveTypeWithType)
  super
end

.instrument(instrument_step, instrumenter, options = {}) ⇒ Object



1452
1453
1454
1455
1456
1457
1458
1459
1460
# File 'lib/graphql/schema.rb', line 1452

def instrument(instrument_step, instrumenter, options = {})
  step = if instrument_step == :field && options[:after_built_ins]
    :field_after_built_ins
  else
    instrument_step
  end

  own_instrumenters[step] << instrumenter
end

.instrumentersObject



1572
1573
1574
1575
1576
1577
# File 'lib/graphql/schema.rb', line 1572

def instrumenters
  inherited_instrumenters = find_inherited_value(:instrumenters) || Hash.new { |h,k| h[k] = [] }
  inherited_instrumenters.merge(own_instrumenters) do |_step, inherited, own|
    inherited + own
  end
end

.interpreter?Boolean

Returns:

  • (Boolean)


1218
1219
1220
1221
1222
1223
1224
# File 'lib/graphql/schema.rb', line 1218

def interpreter?
  if defined?(@interpreter)
    @interpreter
  else
    find_inherited_value(:interpreter?, false)
  end
end

.introspection(new_introspection_namespace = nil) ⇒ Object



1139
1140
1141
1142
1143
1144
1145
1146
1147
# File 'lib/graphql/schema.rb', line 1139

def introspection(new_introspection_namespace = nil)
  if new_introspection_namespace
    @introspection = new_introspection_namespace
    # reset this cached value:
    @introspection_system = nil
  else
    @introspection || find_inherited_value(:introspection)
  end
end

.introspection_systemObject



1149
1150
1151
1152
1153
1154
1155
# File 'lib/graphql/schema.rb', line 1149

def introspection_system
  if !@introspection_system
    @introspection_system = Schema::IntrospectionSystem.new(self)
    @introspection_system.resolve_late_bindings
  end
  @introspection_system
end

.lazy?(obj) ⇒ Boolean

Returns True if this object should be lazily resolved.

Returns:

  • (Boolean)

    True if this object should be lazily resolved



1617
1618
1619
# File 'lib/graphql/schema.rb', line 1617

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.



1612
1613
1614
# File 'lib/graphql/schema.rb', line 1612

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

.lazy_resolve(lazy_class, value_method) ⇒ Object



1448
1449
1450
# File 'lib/graphql/schema.rb', line 1448

def lazy_resolve(lazy_class, value_method)
  lazy_methods.set(lazy_class, value_method)
end

.middleware(new_middleware = nil) ⇒ Object



1506
1507
1508
1509
1510
1511
1512
1513
# File 'lib/graphql/schema.rb', line 1506

def middleware(new_middleware = nil)
  if new_middleware
    own_middleware << new_middleware
  else
    # TODO make sure this is cached when running a query
    MiddlewareChain.new(steps: all_middleware, final_step: GraphQL::Execution::Execute::FieldResolveStep)
  end
end

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

Execute several queries on itself, concurrently.

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


1563
1564
1565
1566
1567
1568
1569
1570
# File 'lib/graphql/schema.rb', line 1563

def multiplex(queries, **kwargs)
  schema = if interpreter?
    self
  else
    graphql_definition
  end
  GraphQL::Execution::Multiplex.run_all(schema, queries, **kwargs)
end

.multiplex_analyzer(new_analyzer) ⇒ Object



1515
1516
1517
# File 'lib/graphql/schema.rb', line 1515

def multiplex_analyzer(new_analyzer)
  own_multiplex_analyzers << new_analyzer
end

.multiplex_analyzersObject



1519
1520
1521
# File 'lib/graphql/schema.rb', line 1519

def multiplex_analyzers
  find_inherited_value(:multiplex_analyzers, EMPTY_ARRAY) + own_multiplex_analyzers
end

.mutation(new_mutation_object = nil) ⇒ Object



994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/graphql/schema.rb', line 994

def mutation(new_mutation_object = nil)
  if new_mutation_object
    if @mutation_object
      raise GraphQL::Error, "Second definition of `mutation(...)` (#{new_mutation_object.inspect}) is invalid, already configured with #{@mutation_object.inspect}"
    else
      @mutation_object = new_mutation_object
      add_type_and_traverse(new_mutation_object, root: true)
      nil
    end
  else
    @mutation_object || find_inherited_value(:mutation)
  end
end

.mutation_execution_strategy(new_mutation_execution_strategy = nil) ⇒ Object



1180
1181
1182
1183
1184
1185
1186
# File 'lib/graphql/schema.rb', line 1180

def mutation_execution_strategy(new_mutation_execution_strategy = nil)
  if new_mutation_execution_strategy
    @mutation_execution_strategy = new_mutation_execution_strategy
  else
    @mutation_execution_strategy || find_inherited_value(:mutation_execution_strategy, self.default_execution_strategy)
  end
end

.new_connections?Boolean

Returns:

  • (Boolean)


976
977
978
# File 'lib/graphql/schema.rb', line 976

def new_connections?
  !!connections
end

.object_from_id(node_id, ctx) ⇒ Object



1365
1366
1367
# File 'lib/graphql/schema.rb', line 1365

def object_from_id(node_id, ctx)
  raise GraphQL::RequiredImplementationMissingError, "#{self.name}.object_from_id(node_id, ctx) must be implemented to load by ID (tried to load from id `#{node_id}`)"
end

.orphan_types(*new_orphan_types) ⇒ Object



1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
# File 'lib/graphql/schema.rb', line 1292

def orphan_types(*new_orphan_types)
  if new_orphan_types.any?
    new_orphan_types = new_orphan_types.flatten
    add_type_and_traverse(new_orphan_types, root: false)
    @orphan_types = new_orphan_types
    own_orphan_types.concat(new_orphan_types.flatten)
  end

  find_inherited_value(:orphan_types, EMPTY_ARRAY) + own_orphan_types
end

.parse_error(parse_err, ctx) ⇒ Object

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

The default is to add the error to context.errors

Parameters:

Returns:

  • void



1438
1439
1440
# File 'lib/graphql/schema.rb', line 1438

def parse_error(parse_err, ctx)
  ctx.errors.push(parse_err)
end

.pluginsObject



880
881
882
# File 'lib/graphql/schema.rb', line 880

def plugins
  find_inherited_value(:plugins, EMPTY_ARRAY) + own_plugins
end

.possible_types(type = nil, context = GraphQL::Query::NullContext) ⇒ Hash<String, Module>, Array<Module>

Parameters:

  • type (Module) (defaults to: nil)

    The type definition whose possible types you want to see

Returns:

  • (Hash<String, Module>)

    All possible types, if no type is given.

  • (Array<Module>)

    Possible types for type, if it’s given.



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/graphql/schema.rb', line 1044

def possible_types(type = nil, context = GraphQL::Query::NullContext)
  if type
    # TODO duck-typing `.possible_types` would probably be nicer here
    if type.kind.union?
      type.possible_types(context: context)
    else
      own_possible_types[type.graphql_name] ||
        introspection_system.possible_types[type.graphql_name] ||
        (
          superclass.respond_to?(:possible_types) ?
            superclass.possible_types(type, context) :
            EMPTY_ARRAY
        )
    end
  else
    find_inherited_value(:possible_types, EMPTY_HASH)
      .merge(own_possible_types)
      .merge(introspection_system.possible_types)
  end
end

.query(new_query_object = nil) ⇒ Object



980
981
982
983
984
985
986
987
988
989
990
991
992
# File 'lib/graphql/schema.rb', line 980

def query(new_query_object = nil)
  if new_query_object
    if @query_object
      raise GraphQL::Error, "Second definition of `query(...)` (#{new_query_object.inspect}) is invalid, already configured with #{@query_object.inspect}"
    else
      @query_object = new_query_object
      add_type_and_traverse(new_query_object, root: true)
      nil
    end
  else
    @query_object || find_inherited_value(:query)
  end
end

.query_analyzer(new_analyzer) ⇒ Object



1495
1496
1497
1498
1499
1500
# File 'lib/graphql/schema.rb', line 1495

def query_analyzer(new_analyzer)
  if new_analyzer == GraphQL::Authorization::Analyzer
    warn("The Authorization query analyzer is deprecated. Authorizing at query runtime is generally a better idea.")
  end
  own_query_analyzers << new_analyzer
end

.query_analyzersObject



1502
1503
1504
# File 'lib/graphql/schema.rb', line 1502

def query_analyzers
  find_inherited_value(:query_analyzers, EMPTY_ARRAY) + own_query_analyzers
end

.query_execution_strategy(new_query_execution_strategy = nil) ⇒ Object



1172
1173
1174
1175
1176
1177
1178
# File 'lib/graphql/schema.rb', line 1172

def query_execution_strategy(new_query_execution_strategy = nil)
  if new_query_execution_strategy
    @query_execution_strategy = new_query_execution_strategy
  else
    @query_execution_strategy || find_inherited_value(:query_execution_strategy, self.default_execution_strategy)
  end
end

.references_to(to_type = nil, from: nil) ⇒ Object



1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/graphql/schema.rb', line 1080

def references_to(to_type = nil, from: nil)
  @own_references_to ||= Hash.new { |h, k| h[k] = [] }
  if to_type
    if !to_type.is_a?(String)
      to_type = to_type.graphql_name
    end

    if from
      @own_references_to[to_type] << from
    else
      own_refs = @own_references_to[to_type]
      inherited_refs = find_inherited_value(:references_to, EMPTY_HASH)[to_type] || EMPTY_ARRAY
      own_refs + inherited_refs
    end
  else
    # `@own_references_to` can be quite large for big schemas,
    # and generally speaking, we won't inherit any values.
    # So optimize the most common case -- don't create a duplicate Hash.
    inherited_value = find_inherited_value(:references_to, EMPTY_HASH)
    if inherited_value.any?
      inherited_value.merge(@own_references_to)
    else
      @own_references_to
    end
  end
end

.rescue_from(*err_classes, &handler_block) ⇒ Object



1319
1320
1321
1322
1323
# File 'lib/graphql/schema.rb', line 1319

def rescue_from(*err_classes, &handler_block)
  err_classes.each do |err_class|
    own_rescues[err_class] = handler_block
  end
end

.rescuesObject



1361
1362
1363
# File 'lib/graphql/schema.rb', line 1361

def rescues
  find_inherited_value(:rescues, EMPTY_HASH).merge(own_rescues)
end

.resolve_type(type, obj, ctx) ⇒ Object



1344
1345
1346
1347
1348
1349
1350
# File 'lib/graphql/schema.rb', line 1344

def resolve_type(type, obj, ctx)
  if type.kind.object?
    type
  else
    raise GraphQL::RequiredImplementationMissingError, "#{self.name}.resolve_type(type, obj, ctx) must be implemented to use Union types or Interface types (tried to resolve: #{type.name})"
  end
end

.root_type_for_operation(operation) ⇒ GraphQL::ObjectType?

Returns:

See Also:

  • Resticted access to root types


1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/graphql/schema.rb', line 1024

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_typesObject



1037
1038
1039
# File 'lib/graphql/schema.rb', line 1037

def root_types
  @root_types
end

.static_validatorObject



867
868
869
# File 'lib/graphql/schema.rb', line 867

def static_validator
  GraphQL::StaticValidation::Validator.new(schema: self)
end

.subscription(new_subscription_object = nil) ⇒ Object



1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/graphql/schema.rb', line 1008

def subscription(new_subscription_object = nil)
  if new_subscription_object
    if @subscription_object
      raise GraphQL::Error, "Second definition of `subscription(...)` (#{new_subscription_object.inspect}) is invalid, already configured with #{@subscription_object.inspect}"
    else
      @subscription_object = new_subscription_object
      add_type_and_traverse(new_subscription_object, root: true)
      nil
    end
  else
    @subscription_object || find_inherited_value(:subscription)
  end
end

.subscription_execution_strategy(new_subscription_execution_strategy = nil) ⇒ Object



1188
1189
1190
1191
1192
1193
1194
# File 'lib/graphql/schema.rb', line 1188

def subscription_execution_strategy(new_subscription_execution_strategy = nil)
  if new_subscription_execution_strategy
    @subscription_execution_strategy = new_subscription_execution_strategy
  else
    @subscription_execution_strategy || find_inherited_value(:subscription_execution_strategy, self.default_execution_strategy)
  end
end

.sync_lazy(value) ⇒ Object

Override this method to handle lazy objects in a custom way.

Parameters:

Returns:

  • (Object)

    A GraphQL-ready (non-lazy) object



1601
1602
1603
1604
1605
1606
1607
1608
1609
# File 'lib/graphql/schema.rb', line 1601

def sync_lazy(value)
  lazy_method = lazy_method_name(value)
  if lazy_method
    synced_value = value.public_send(lazy_method)
    sync_lazy(synced_value)
  else
    value
  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)


833
834
835
# File 'lib/graphql/schema.rb', line 833

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

.to_documentGraphQL::Language::Document

Return the GraphQL::Language::Document IDL AST for the schema

Returns:

  • (GraphQL::Language::Document)


839
840
841
# File 'lib/graphql/schema.rb', line 839

def to_document
  GraphQL::Language::DocumentFromSchemaDefinition.new(self).document
end

.to_graphqlObject



884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/graphql/schema.rb', line 884

def to_graphql
  schema_defn = self.new
  schema_defn.raise_definition_error = true
  schema_defn.query = query && query.graphql_definition
  schema_defn.mutation = mutation && mutation.graphql_definition
  schema_defn.subscription = subscription && subscription.graphql_definition
  schema_defn.max_complexity = max_complexity
  schema_defn.error_bubbling = error_bubbling
  schema_defn.max_depth = max_depth
  schema_defn.default_max_page_size = default_max_page_size
  schema_defn.orphan_types = orphan_types.map(&:graphql_definition)
  schema_defn.disable_introspection_entry_points = disable_introspection_entry_points?
  schema_defn.disable_schema_introspection_entry_point = disable_schema_introspection_entry_point?
  schema_defn.disable_type_introspection_entry_point = disable_type_introspection_entry_point?

  prepped_dirs = {}
  directives.each { |k, v| prepped_dirs[k] = v.graphql_definition}
  schema_defn.directives = prepped_dirs
  schema_defn.introspection_namespace = introspection
  schema_defn.resolve_type = method(:resolve_type)
  schema_defn.object_from_id = method(:object_from_id)
  schema_defn.id_from_object = method(:id_from_object)
  schema_defn.type_error = method(:type_error)
  schema_defn.context_class = context_class
  schema_defn.cursor_encoder = cursor_encoder
  schema_defn.tracers.concat(tracers)
  schema_defn.query_analyzers.concat(query_analyzers)

  schema_defn.middleware.concat(all_middleware)
  schema_defn.multiplex_analyzers.concat(multiplex_analyzers)
  schema_defn.query_execution_strategy = query_execution_strategy
  schema_defn.mutation_execution_strategy = mutation_execution_strategy
  schema_defn.subscription_execution_strategy = subscription_execution_strategy
  schema_defn.default_mask = default_mask
  instrumenters.each do |step, insts|
    insts.each do |inst|
      schema_defn.instrumenters[step] << inst
    end
  end

  lazy_methods.each do |lazy_class, value_method|
    schema_defn.lazy_methods.set(lazy_class, value_method)
  end

  rescues.each do |err_class, handler|
    schema_defn.rescue_from(err_class, &handler)
  end

  schema_defn.subscriptions ||= self.subscriptions

  if !schema_defn.interpreter?
    schema_defn.instrumenters[:query] << GraphQL::Schema::Member::Instrumentation
  end
  schema_defn.send(:rebuild_artifacts)

  schema_defn
end

.to_json(*args) ⇒ String

Returns the JSON response of Introspection::INTROSPECTION_QUERY.

Returns:

  • (String)

See Also:

  • {{#as_json}


815
816
817
# File 'lib/graphql/schema.rb', line 815

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

.tracer(new_tracer) ⇒ Object



1487
1488
1489
# File 'lib/graphql/schema.rb', line 1487

def tracer(new_tracer)
  own_tracers << new_tracer
end

.tracersObject



1491
1492
1493
# File 'lib/graphql/schema.rb', line 1491

def tracers
  find_inherited_value(:tracers, EMPTY_ARRAY) + own_tracers
end

.type_error(type_err, ctx) ⇒ Object



1428
1429
1430
# File 'lib/graphql/schema.rb', line 1428

def type_error(type_err, ctx)
  DefaultTypeError.call(type_err, ctx)
end

.type_from_ast(ast_node, context: nil) ⇒ Object



1107
1108
1109
1110
# File 'lib/graphql/schema.rb', line 1107

def type_from_ast(ast_node, context: nil)
  type_owner = context ? context.warden : self
  GraphQL::Schema::TypeExpression.build_type(type_owner, ast_node)
end

.typesHash<String => Class>

Build a map of { name => type } and return it

Returns:

  • (Hash<String => Class>)

    A dictionary of type classes by their GraphQL name

See Also:



945
946
947
# File 'lib/graphql/schema.rb', line 945

def types
  non_introspection_types.merge(introspection_system.types)
end

.unauthorized_field(unauthorized_error) ⇒ Field

This hook is called when a field fails an authorized? check.

By default, this hook implements the same behavior as unauthorized_object.

Whatever value is returned from this method will be used instead of the unauthorized field . If an error is raised, then nil will be used.

If you want to add an error to the "errors" key, raise a ExecutionError in this hook.

Parameters:

Returns:

  • (Field)

    The returned field will be put in the GraphQL response



1424
1425
1426
# File 'lib/graphql/schema.rb', line 1424

def unauthorized_field(unauthorized_error)
  unauthorized_object(unauthorized_error)
end

.unauthorized_object(unauthorized_error) ⇒ Object

This hook is called when an object fails an authorized? check. You might report to your bug tracker here, so you can correct the field resolvers not to return unauthorized objects.

By default, this hook just replaces the unauthorized object with nil.

Whatever value is returned from this method will be used instead of the unauthorized object (accessible as unauthorized_error.object). If an error is raised, then nil will be used.

If you want to add an error to the "errors" key, raise a ExecutionError in this hook.

Parameters:

Returns:

  • (Object)

    The returned object will be put in the GraphQL response



1408
1409
1410
# File 'lib/graphql/schema.rb', line 1408

def unauthorized_object(unauthorized_error)
  nil
end

.union_memberships(type = nil) ⇒ Object



1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# File 'lib/graphql/schema.rb', line 1065

def union_memberships(type = nil)
  if type
    own_um = own_union_memberships.fetch(type.graphql_name, EMPTY_ARRAY)
    inherited_um = find_inherited_value(:union_memberships, EMPTY_HASH).fetch(type.graphql_name, EMPTY_ARRAY)
    own_um + inherited_um
  else
    joined_um = own_union_memberships.dup
    find_inherited_value(:union_memberhips, EMPTY_HASH).each do |k, v|
      um = joined_um[k] ||= []
      um.concat(v)
    end
    joined_um
  end
end

.use(plugin, **kwargs) ⇒ Object



871
872
873
874
875
876
877
878
# File 'lib/graphql/schema.rb', line 871

def use(plugin, **kwargs)
  if kwargs.any?
    plugin.use(self, **kwargs)
  else
    plugin.use(self)
  end
  own_plugins << [plugin, kwargs]
end

.using_ast_analysis?Boolean

Returns:

  • (Boolean)


1214
1215
1216
# File 'lib/graphql/schema.rb', line 1214

def using_ast_analysis?
  analysis_engine == GraphQL::Analysis::AST
end

.visible?(member, ctx) ⇒ Boolean

Returns:

  • (Boolean)


1373
1374
1375
# File 'lib/graphql/schema.rb', line 1373

def visible?(member, ctx)
  member.type_class.visible?(ctx)
end

Instance Method Details

#accessible?(member, context) ⇒ Boolean

Returns:

  • (Boolean)


673
674
675
# File 'lib/graphql/schema.rb', line 673

def accessible?(member, context)
  call_on_type_class(member, :accessible?, context, default: true)
end

#after_lazy(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Call the given block at the right time, either: - Right away, if value is not registered with lazy_resolve - After resolving value, if it’s registered with lazy_resolve (eg, Promise)



1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
# File 'lib/graphql/schema.rb', line 1875

def after_lazy(value)
  if lazy?(value)
    GraphQL::Execution::Lazy.new do
      result = sync_lazy(value)
      # The returned result might also be lazy, so check it, too
      after_lazy(result) do |final_result|
        yield(final_result) if block_given?
      end
    end
  else
    yield(value) if block_given?
  end
end

#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



776
777
778
# File 'lib/graphql/schema.rb', line 776

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

#call_on_type_class(member, method_name, context, default:) ⇒ Object

Given this schema member, find the class-based definition object whose method_name should be treated as an application hook

See Also:

  • GraphQL::Schema.{{.visible?}
  • GraphQL::Schema.{{.accessible?}


651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/graphql/schema.rb', line 651

def call_on_type_class(member, method_name, context, default:)
  member = if member.respond_to?(:type_class)
    member.type_class
  else
    member
  end

  if member.respond_to?(:relay_node_type) && (t = member.relay_node_type)
    member = t
  end

  if member.respond_to?(method_name)
    member.public_send(method_name, context)
  else
    default
  end
end

#check_resolved_type(type, object, ctx = :__undefined__) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is a compatibility hack so that instance-level and class-level methods can get correctness checks without calling one another



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/graphql/schema.rb', line 548

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

  if object.is_a?(GraphQL::Schema::Object)
    object = object.object
  end

  if type.respond_to?(:graphql_definition)
    type = type.graphql_definition
  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
    yield(type, object, ctx)
  end

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

#default_filterObject



192
193
194
# File 'lib/graphql/schema.rb', line 192

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

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



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/graphql/schema.rb', line 314

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

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

#disable_introspection_entry_points?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/graphql/schema.rb', line 170

def disable_introspection_entry_points?
  !!@disable_introspection_entry_points
end

#disable_schema_introspection_entry_point?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/graphql/schema.rb', line 177

def disable_schema_introspection_entry_point?
  !!@disable_schema_introspection_entry_point
end

#disable_type_introspection_entry_point?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/graphql/schema.rb', line 184

def disable_type_introspection_entry_point?
  !!@disable_type_introspection_entry_point
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.


397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/graphql/schema.rb', line 397

def execute(query_str = nil, **kwargs)
  if query_str
    kwargs[:query] = query_str
  end
  # Some of the query context _should_ be passed to the multiplex, too
  multiplex_context = if (ctx = kwargs[:context])
    {
      backtrace: ctx[:backtrace],
      tracers: ctx[:tracers],
    }
  else
    {}
  end
  # Since we're running one query, don't run a multiplex-level complexity analyzer
  all_results = multiplex([kwargs], max_complexity: nil, context: multiplex_context)
  all_results[0]
end

#execution_strategy_for_operation(operation) ⇒ Object



516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/graphql/schema.rb', line 516

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

#find(path) ⇒ GraphQL::BaseType, ...

Search for a schema member using a string path Schema.find(“Ensemble.musicians”)

Examples:

Finding a Field

Parameters:

  • path (String)

    A dot-separated path to the member

Returns:

Raises:

See Also:

  • for more examples


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

def find(path)
  rebuild_artifacts unless defined?(@finder)
  @find_cache[path] ||= @finder.find(path)
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


458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/graphql/schema.rb', line 458

def get_field(parent_type, field_name)
  with_definition_error_check do
    parent_type_name = case parent_type
    when GraphQL::BaseType, Class, Module
      parent_type.graphql_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 parent_type == query && (entry_point_field = introspection_system.entry_point(name: field_name))
      entry_point_field
    elsif (dynamic_field = introspection_system.dynamic_field(name: field_name))
      dynamic_field
    else
      nil
    end
  end
end

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

Fields for this type, after instrumentation is applied

Returns:



484
485
486
# File 'lib/graphql/schema.rb', line 484

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

#get_type(type_name) ⇒ Object



362
363
364
# File 'lib/graphql/schema.rb', line 362

def get_type(type_name)
  @types[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



697
698
699
700
701
702
703
# File 'lib/graphql/schema.rb', line 697

def id_from_object(object, type, ctx)
  if @id_from_object_proc.nil?
    raise(GraphQL::RequiredImplementationMissingError, "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



706
707
708
# File 'lib/graphql/schema.rb', line 706

def id_from_object=(new_proc)
  @id_from_object_proc = new_proc
end

#initialize_copy(other) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/graphql/schema.rb', line 254

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
  @tracers = other.tracers.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
  @introspection_system = nil
end

#inspectObject



250
251
252
# File 'lib/graphql/schema.rb', line 250

def inspect
  "#<#{self.class.name} ...>"
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


338
339
340
341
342
343
# File 'lib/graphql/schema.rb', line 338

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

#interpreter?Boolean

Returns True if using the new Execution::Interpreter.

Returns:



243
244
245
# File 'lib/graphql/schema.rb', line 243

def interpreter?
  @interpreter
end

#introspection_systemObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



367
368
369
370
371
372
# File 'lib/graphql/schema.rb', line 367

def introspection_system
  @introspection_system ||= begin
    rebuild_artifacts
    @introspection_system
  end
end

#lazy?(obj) ⇒ Boolean

Returns True if this object should be lazily resolved.

Returns:

  • (Boolean)

    True if this object should be lazily resolved



749
750
751
# File 'lib/graphql/schema.rb', line 749

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.



744
745
746
# File 'lib/graphql/schema.rb', line 744

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


433
434
435
436
437
# File 'lib/graphql/schema.rb', line 433

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

#new_connections?Boolean

Returns:

  • (Boolean)


787
788
789
# File 'lib/graphql/schema.rb', line 787

def new_connections?
  !!connections
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



598
599
600
601
602
603
604
# File 'lib/graphql/schema.rb', line 598

def object_from_id(id, ctx)
  if @object_from_id_proc.nil?
    raise(GraphQL::RequiredImplementationMissingError, "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



607
608
609
# File 'lib/graphql/schema.rb', line 607

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.


683
684
685
# File 'lib/graphql/schema.rb', line 683

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



688
689
690
# File 'lib/graphql/schema.rb', line 688

def parse_error=(new_proc)
  @parse_error_proc = new_proc
end

#possible_types(type_defn, context = GraphQL::Query::NullContext) ⇒ Array<GraphQL::ObjectType>

Returns types which belong to type_defn in this schema.

Parameters:

Returns:

See Also:

  • Restricted access to members of a schema


496
497
498
499
# File 'lib/graphql/schema.rb', line 496

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

#references_to(type_name = nil) ⇒ Hash

Returns a list of Arguments and Fields referencing a certain type

Parameters:

  • type_name (String) (defaults to: nil)

Returns:

  • (Hash)


377
378
379
380
381
382
383
384
# File 'lib/graphql/schema.rb', line 377

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

#remove_handler(*args, &block) ⇒ Object



286
287
288
# File 'lib/graphql/schema.rb', line 286

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

#rescue_from(*args, &block) ⇒ Object



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

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


536
537
538
539
540
541
542
543
# File 'lib/graphql/schema.rb', line 536

def resolve_type(type, object, ctx = :__undefined__)
  check_resolved_type(type, object, ctx) do |ok_type, ok_object, ok_ctx|
    if @resolve_type_proc.nil?
      raise(GraphQL::RequiredImplementationMissingError, "Can't determine GraphQL type for: #{ok_object.inspect}, define `resolve_type (type, obj, ctx) -> { ... }` inside `Schema.define`.")
    end
    @resolve_type_proc.call(ok_type, ok_object, ok_ctx)
  end
end

#resolve_type=(new_resolve_type_proc) ⇒ Object



589
590
591
592
# File 'lib/graphql/schema.rb', line 589

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


503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/graphql/schema.rb', line 503

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:



346
347
348
349
350
351
# File 'lib/graphql/schema.rb', line 346

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

#sync_lazy(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



1891
1892
1893
1894
1895
1896
1897
1898
1899
# File 'lib/graphql/schema.rb', line 1891

def sync_lazy(value)
  lazy_method = lazy_method_name(value)
  if lazy_method
    synced_value = value.public_send(lazy_method)
    sync_lazy(synced_value)
  else
    value
  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)


758
759
760
# File 'lib/graphql/schema.rb', line 758

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

#to_document(only: nil, except: nil, context: {}) ⇒ GraphQL::Language::Document

Return the GraphQL::Language::Document IDL AST for the schema

Parameters:

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

Returns:

  • (GraphQL::Language::Document)


767
768
769
# File 'lib/graphql/schema.rb', line 767

def to_document(only: nil, except: nil, context: {})
  GraphQL::Language::DocumentFromSchemaDefinition.new(self, only: only, except: except, context: context).document
end

#to_json(*args) ⇒ String

Returns the JSON response of Introspection::INTROSPECTION_QUERY.

Returns:

  • (String)

See Also:

  • GraphQL::Schema.{{#as_json}


783
784
785
# File 'lib/graphql/schema.rb', line 783

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.


631
632
633
# File 'lib/graphql/schema.rb', line 631

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



636
637
638
# File 'lib/graphql/schema.rb', line 636

def type_error=(new_proc)
  @type_error_proc = new_proc
end

#type_from_ast(ast_node, context:) ⇒ Object



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

def type_from_ast(ast_node, context:)
  GraphQL::Schema::TypeExpression.build_type(self, 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


355
356
357
358
359
360
# File 'lib/graphql/schema.rb', line 355

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:



389
390
391
392
# File 'lib/graphql/schema.rb', line 389

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

#using_ast_analysis?Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/graphql/schema.rb', line 290

def using_ast_analysis?
  @analysis_engine == GraphQL::Analysis::AST
end

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

Validate a query string according to this schema.

Parameters:

Returns:



300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/graphql/schema.rb', line 300

def validate(string_or_document, rules: nil, context: 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, context: context)
  validator_opts = { schema: self }
  rules && (validator_opts[:rules] = rules)
  validator = GraphQL::StaticValidation::Validator.new(**validator_opts)
  res = validator.validate(query)
  res[:errors]
end

#visible?(member, context) ⇒ Boolean

Returns:

  • (Boolean)


669
670
671
# File 'lib/graphql/schema.rb', line 669

def visible?(member, context)
  call_on_type_class(member, :visible?, context, default: true)
end