Class: GraphQL::Schema

Inherits:
Object
  • Object
show all
Extended by:
FindInheritedValue, Member::HasAstNode
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/addition.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/validator.rb,
lib/graphql/schema/enum_value.rb,
lib/graphql/schema/visibility.rb,
lib/graphql/schema/input_object.rb,
lib/graphql/schema/subscription.rb,
lib/graphql/schema/member/scoped.rb,
lib/graphql/schema/always_visible.rb,
lib/graphql/schema/built_in_types.rb,
lib/graphql/schema/directive/skip.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/directive/one_of.rb,
lib/graphql/schema/directive/feature.rb,
lib/graphql/schema/directive/flagged.rb,
lib/graphql/schema/directive/include.rb,
lib/graphql/schema/member/build_type.rb,
lib/graphql/schema/member/has_fields.rb,
lib/graphql/schema/unique_within_type.rb,
lib/graphql/schema/visibility/profile.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/visibility/migration.rb,
lib/graphql/schema/build_from_definition.rb,
lib/graphql/schema/field/scope_extension.rb,
lib/graphql/schema/member/has_directives.rb,
lib/graphql/schema/member/has_interfaces.rb,
lib/graphql/schema/member/has_validators.rb,
lib/graphql/schema/directive/specified_by.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/validator/all_validator.rb,
lib/graphql/schema/has_single_input_argument.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/validator/format_validator.rb,
lib/graphql/schema/validator/length_validator.rb,
lib/graphql/schema/validator/required_validator.rb,
lib/graphql/schema/member/has_deprecation_reason.rb,
lib/graphql/schema/validator/exclusion_validator.rb,
lib/graphql/schema/validator/inclusion_validator.rb,
lib/graphql/schema/validator/allow_null_validator.rb,
lib/graphql/schema/validator/allow_blank_validator.rb,
lib/graphql/schema/member/has_unresolved_type_error.rb,
lib/graphql/schema/validator/numericality_validator.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

Schemas start with root types, Schema.query, Schema.mutation and Schema.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 Schema.execute)

Examples:

defining a schema

class MySchema < GraphQL::Schema
  query QueryType
  # If types are only connected by way of interfaces, they must be added here
  orphan_types ImageType, AudioType
end

Direct Known Subclasses

Query::NullContext::NullSchema

Defined Under Namespace

Modules: AlwaysVisible, Base64Encoder, BuildFromDefinition, DefaultTraceClass, FindInheritedValue, HasSingleInputArgument, Interface, Loader, NullMask, ResolveTypeWithType, SubclassGetReferencesTo, TypeExpression, UniqueWithinType Classes: Addition, Argument, Directive, DuplicateNamesError, Enum, EnumValue, Field, FieldExtension, Finder, InputObject, IntrospectionSystem, InvalidDocumentError, LateBoundType, List, Member, Mutation, NonNull, Object, Printer, RelayClassicMutation, Resolver, Scalar, Subscription, Timeout, TypeMembership, Union, UnresolvedLateBoundTypeError, Validator, Visibility, Warden, Wrapper

Constant Summary collapse

NEW_HANDLER_HASH =
->(h, k) {
  h[k] = {
    class: k,
    handler: nil,
    subclass_handlers: Hash.new(&NEW_HANDLER_HASH),
   }
}
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

Attributes included from Member::HasAstNode

#ast_node

Class Method Summary collapse

Methods included from Member::HasAstNode

inherited

Class Attribute Details

.analysis_engineObject



884
885
886
# File 'lib/graphql/schema.rb', line 884

def analysis_engine
  @analysis_engine || find_inherited_value(:analysis_engine, self.default_analysis_engine)
end

.connectionsGraphQL::Pagination::Connections

Returns if installed.

Returns:



419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/graphql/schema.rb', line 419

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

.dataloader_classObject

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.

See Also:



660
661
662
# File 'lib/graphql/schema.rb', line 660

def dataloader_class
  @dataloader_class || GraphQL::Dataloader::NullDataloader
end

.error_bubbling(new_error_bubbling = nil) ⇒ Object



888
889
890
891
892
893
894
895
# File 'lib/graphql/schema.rb', line 888

def error_bubbling(new_error_bubbling = nil)
  if !new_error_bubbling.nil?
    warn("error_bubbling(#{new_error_bubbling.inspect}) is deprecated; the default value of `false` will be the only option in GraphQL-Ruby 3.0")
    @error_bubbling = new_error_bubbling
  else
    @error_bubbling.nil? ? find_inherited_value(:error_bubbling) : @error_bubbling
  end
end

.max_complexity(max_complexity = nil, count_introspection_fields: true) ⇒ Object



863
864
865
866
867
868
869
870
871
872
# File 'lib/graphql/schema.rb', line 863

def max_complexity(max_complexity = nil, count_introspection_fields: true)
  if max_complexity
    @max_complexity = max_complexity
    @max_complexity_count_introspection_fields = count_introspection_fields
  elsif defined?(@max_complexity)
    @max_complexity
  else
    find_inherited_value(:max_complexity)
  end
end

.max_depth(new_max_depth = nil, count_introspection_fields: true) ⇒ Object



901
902
903
904
905
906
907
908
909
910
# File 'lib/graphql/schema.rb', line 901

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

.use_visibility_profile=(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.



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

def use_visibility_profile=(value)
  @use_visibility_profile = value
end

.validate_max_errors(new_validate_max_errors = NOT_CONFIGURED) ⇒ Object



853
854
855
856
857
858
859
# File 'lib/graphql/schema.rb', line 853

def validate_max_errors(new_validate_max_errors = NOT_CONFIGURED)
  if NOT_CONFIGURED.equal?(new_validate_max_errors)
    defined?(@validate_max_errors) ? @validate_max_errors : find_inherited_value(:validate_max_errors)
  else
    @validate_max_errors = new_validate_max_errors
  end
end

.validate_timeout(new_validate_timeout = nil) ⇒ Object



815
816
817
818
819
820
821
822
823
# File 'lib/graphql/schema.rb', line 815

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

.visibilityObject

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.



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

def visibility
  @visibility
end

.visibility_profile_classObject

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.



577
578
579
580
581
582
583
584
585
# File 'lib/graphql/schema.rb', line 577

def visibility_profile_class
  if defined?(@visibility_profile_class)
    @visibility_profile_class
  elsif superclass.respond_to?(:visibility_profile_class)
    superclass.visibility_profile_class
  else
    GraphQL::Schema::Visibility::Profile
  end
end

.warden_classObject

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.



563
564
565
566
567
568
569
570
571
# File 'lib/graphql/schema.rb', line 563

def warden_class
  if defined?(@warden_class)
    @warden_class
  elsif superclass.respond_to?(:warden_class)
    superclass.warden_class
  else
    GraphQL::Schema::Warden
  end
end

Class Method Details

.add_subscription_extension_if_necessaryObject

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.



1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
# File 'lib/graphql/schema.rb', line 1538

def add_subscription_extension_if_necessary
  # TODO: when there's a proper API for extending root types, migrat this to use it.
  if !defined?(@subscription_extension_added) && @subscription_object.is_a?(Class) && self.subscriptions
    @subscription_extension_added = true
    subscription.all_field_definitions.each do |field|
      if !field.extensions.any? { |ext| ext.is_a?(Subscriptions::DefaultSubscriptionResolveExtension) }
        field.extension(Subscriptions::DefaultSubscriptionResolveExtension)
      end
    end
  end
end

.after_any_lazies(maybe_lazies) ⇒ 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.

Return a lazy if any of maybe_lazies are lazy, otherwise, call the block eagerly and return the result.

Parameters:

  • maybe_lazies (Array)


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

def after_any_lazies(maybe_lazies)
  if maybe_lazies.any? { |l| lazy?(l) }
    GraphQL::Execution::Lazy.all(maybe_lazies).then do |result|
      yield result
    end
  else
    yield maybe_lazies
  end
end

.after_lazy(value, &block) ⇒ 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)



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

def after_lazy(value, &block)
  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, &block)
    end
  else
    yield(value) if block_given?
  end
end

.as_json(context: {}, include_deprecated_args: true, include_schema_description: false, include_is_repeatable: false, include_specified_by_url: false, include_is_one_of: false) ⇒ Hash

Return the Hash response of Introspection::INTROSPECTION_QUERY.

Parameters:

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

    If true, deprecated arguments will be included in the JSON response

  • include_schema_description (Boolean) (defaults to: false)

    If true, the schema’s description will be queried and included in the response

  • include_is_repeatable (Boolean) (defaults to: false)

    If true, isRepeatable: true|false will be included with the schema’s directives

  • include_specified_by_url (Boolean) (defaults to: false)

    If true, scalar types’ specifiedByUrl: will be included in the response

  • include_is_one_of (Boolean) (defaults to: false)

    If true, isOneOf: true|false will be included with input objects

Returns:

  • (Hash)

    GraphQL result



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/graphql/schema.rb', line 272

def as_json(context: {}, include_deprecated_args: true, include_schema_description: false, include_is_repeatable: false, include_specified_by_url: false, include_is_one_of: false)
  introspection_query = Introspection.query(
    include_deprecated_args: include_deprecated_args,
    include_schema_description: include_schema_description,
    include_is_repeatable: include_is_repeatable,
    include_is_one_of: include_is_one_of,
    include_specified_by_url: include_specified_by_url,
  )

  execute(introspection_query, context: context).to_h
end

.build_trace_mode(mode) ⇒ Object



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
# File 'lib/graphql/schema.rb', line 208

def build_trace_mode(mode)
  case mode
  when :default
    # Use the superclass's default mode if it has one, or else start an inheritance chain at the built-in base class.
    base_class = (superclass.respond_to?(:trace_class_for) && superclass.trace_class_for(mode, build: true)) || GraphQL::Tracing::Trace
    const_set(:DefaultTrace, Class.new(base_class) do
      include DefaultTraceClass
    end)
  when :default_backtrace
    schema_base_class = trace_class_for(:default, build: true)
    const_set(:DefaultTraceBacktrace, Class.new(schema_base_class) do
      include(GraphQL::Backtrace::Trace)
    end)
  else
    # First, see if the superclass has a custom-defined class for this.
    # Then, if it doesn't, use this class's default trace
    base_class = (superclass.respond_to?(:trace_class_for) && superclass.trace_class_for(mode)) || trace_class_for(:default, build: true)
    # Prepare the default trace class if it hasn't been initialized yet
    base_class ||= (own_trace_modes[:default] = build_trace_mode(:default))
    mods = trace_modules_for(mode)
    if base_class < DefaultTraceClass
      mods = trace_modules_for(:default) + mods
    end
    # Copy the existing default options into this mode's options
    default_options = trace_options_for(:default)
    add_trace_options_for(mode, default_options)

    Class.new(base_class) do
      mods.any? && include(*mods)
    end
  end
end

.context_class(new_context_class = nil) ⇒ Object

Parameters:

  • new_context_class (Class<GraphQL::Query::Context>) (defaults to: nil)

    A subclass to use when executing queries



1059
1060
1061
1062
1063
1064
1065
# File 'lib/graphql/schema.rb', line 1059

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

.count_introspection_fieldsObject



912
913
914
915
916
917
918
# File 'lib/graphql/schema.rb', line 912

def count_introspection_fields
  if defined?(@count_introspection_fields)
    @count_introspection_fields
  else
    find_inherited_value(:count_introspection_fields, true)
  end
end

.cursor_encoder(new_encoder = nil) ⇒ Object



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

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

.default_analysis_engineObject



1028
1029
1030
1031
1032
1033
1034
# File 'lib/graphql/schema.rb', line 1028

def default_analysis_engine
  if superclass <= GraphQL::Schema
    superclass.default_analysis_engine
  else
    @default_analysis_engine ||= GraphQL::Analysis::AST
  end
end

.default_directivesObject



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

def default_directives
  @default_directives ||= {
    "include" => GraphQL::Schema::Directive::Include,
    "skip" => GraphQL::Schema::Directive::Skip,
    "deprecated" => GraphQL::Schema::Directive::Deprecated,
    "oneOf" => GraphQL::Schema::Directive::OneOf,
    "specifiedBy" => GraphQL::Schema::Directive::SpecifiedBy,
  }.freeze
end

.default_execution_strategyObject



1020
1021
1022
1023
1024
1025
1026
# File 'lib/graphql/schema.rb', line 1020

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

.default_logger(new_default_logger = NOT_CONFIGURED) ⇒ Object

Parameters:

  • new_default_logger (#log) (defaults to: NOT_CONFIGURED)

    Something to use for logging messages



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/graphql/schema.rb', line 1038

def default_logger(new_default_logger = NOT_CONFIGURED)
  if NOT_CONFIGURED.equal?(new_default_logger)
    if defined?(@default_logger)
      @default_logger
    elsif superclass.respond_to?(:default_logger)
      superclass.default_logger
    elsif defined?(Rails) && Rails.respond_to?(:logger) && (rails_logger = Rails.logger)
      rails_logger
    else
      def_logger = Logger.new($stdout)
      def_logger.info! # It doesn't output debug info by default
      def_logger
    end
  elsif new_default_logger == nil
    @default_logger = Logger.new(IO::NULL)
  else
    @default_logger = new_default_logger
  end
end

.default_max_page_size(new_default_max_page_size = nil) ⇒ Object



750
751
752
753
754
755
756
# File 'lib/graphql/schema.rb', line 750

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

.default_page_size(new_default_page_size = nil) ⇒ Object



769
770
771
772
773
774
775
# File 'lib/graphql/schema.rb', line 769

def default_page_size(new_default_page_size = nil)
  if new_default_page_size
    @default_page_size = new_default_page_size
  else
    @default_page_size || find_inherited_value(:default_page_size)
  end
end

.default_trace_mode(new_mode = nil) ⇒ Object

Parameters:

  • new_mode (Symbol) (defaults to: nil)

    If configured, this will be used when context: { trace_mode: ... } isn’t set.



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/graphql/schema.rb', line 147

def default_trace_mode(new_mode = nil)
  if new_mode
    @default_trace_mode = new_mode
  elsif defined?(@default_trace_mode)
    @default_trace_mode
  elsif superclass.respond_to?(:default_trace_mode)
    superclass.default_trace_mode
  else
    :default
  end
end

.deprecated_graphql_definitionObject



133
134
135
# File 'lib/graphql/schema.rb', line 133

def deprecated_graphql_definition
  graphql_definition(silence_deprecation_warning: true)
end

.description(new_description = nil) ⇒ String?

Returns:

  • (String, nil)


298
299
300
301
302
303
304
305
306
# File 'lib/graphql/schema.rb', line 298

def description(new_description = nil)
  if new_description
    @description = new_description
  elsif defined?(@description)
    @description
  else
    find_inherited_value(:description, nil)
  end
end

.did_you_mean(new_dym = NOT_CONFIGURED) ⇒ Object

Returns DidYouMean if it’s defined. Override this to return nil if you don’t want to use DidYouMean



1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
# File 'lib/graphql/schema.rb', line 1615

def did_you_mean(new_dym = NOT_CONFIGURED)
  if NOT_CONFIGURED.equal?(new_dym)
    if defined?(@did_you_mean)
      @did_you_mean
    else
      find_inherited_value(:did_you_mean, defined?(DidYouMean) ? DidYouMean : nil)
    end
  else
    @did_you_mean = new_dym
  end
end

.directive(new_directive) ⇒ Object

Attach a single directive to this schema

Parameters:

  • new_directive (Class)

Returns:

  • void



1335
1336
1337
1338
1339
1340
1341
# File 'lib/graphql/schema.rb', line 1335

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

.directives(*new_directives) ⇒ Object

Add several directives at once

Parameters:

  • new_directives (Class)


1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
# File 'lib/graphql/schema.rb', line 1319

def directives(*new_directives)
  if new_directives.any?
    new_directives.flatten.each { |d| directive(d) }
  end

  inherited_dirs = find_inherited_value(:directives, default_directives)
  if own_directives.any?
    inherited_dirs.merge(own_directives)
  else
    inherited_dirs
  end
end

.disable_introspection_entry_pointsObject



920
921
922
923
924
# File 'lib/graphql/schema.rb', line 920

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)


938
939
940
941
942
943
944
# File 'lib/graphql/schema.rb', line 938

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



926
927
928
929
930
# File 'lib/graphql/schema.rb', line 926

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)


946
947
948
949
950
951
952
# File 'lib/graphql/schema.rb', line 946

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



932
933
934
935
936
# File 'lib/graphql/schema.rb', line 932

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)


954
955
956
957
958
959
960
# File 'lib/graphql/schema.rb', line 954

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

.error_handlersObject



1095
1096
1097
1098
1099
1100
1101
# File 'lib/graphql/schema.rb', line 1095

def error_handlers
  @error_handlers ||= {
    class: nil,
    handler: nil,
    subclass_handlers: Hash.new(&NEW_HANDLER_HASH),
  }
end

.execute(query_str = nil, **kwargs) ⇒ GraphQL::Query::Result

Execute a query on itself.

Returns:

See Also:

  • for arguments.


1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
# File 'lib/graphql/schema.rb', line 1486

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],
      trace: ctx[:trace],
      dataloader: ctx[:dataloader],
      trace_mode: ctx[:trace_mode],
    }
  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

.extra_types(*new_extra_types) ⇒ Array<Module>

Returns Type definitions added to this schema.

Parameters:

  • new_extra_types (Module)

    Type definitions to include in printing and introspection, even though they aren’t referenced in the schema

Returns:

  • (Array<Module>)

    Type definitions added to this schema



964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
# File 'lib/graphql/schema.rb', line 964

def extra_types(*new_extra_types)
  if new_extra_types.any?
    new_extra_types = new_extra_types.flatten
    @own_extra_types ||= []
    @own_extra_types.concat(new_extra_types)
  end
  inherited_et = find_inherited_value(:extra_types, nil)
  if inherited_et
    if @own_extra_types
      inherited_et + @own_extra_types
    else
      inherited_et
    end
  else
    @own_extra_types || EMPTY_ARRAY
  end
end

.find(path) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/graphql/schema.rb', line 308

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, parser: GraphQL.default_parser, 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: GraphQL.default_parser)

    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)

Returns:

  • (Class)

    the schema described by document



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

def from_definition(definition_or_path, default_resolve: nil, parser: GraphQL.default_parser, using: {})
  # If the file ends in `.graphql` or `.graphqls`, treat it like a filepath
  if definition_or_path.end_with?(".graphql") || definition_or_path.end_with?(".graphqls")
    GraphQL::Schema::BuildFromDefinition.from_definition_path(
      self,
      definition_or_path,
      default_resolve: default_resolve,
      parser: parser,
      using: using,
    )
  else
    GraphQL::Schema::BuildFromDefinition.from_definition(
      self,
      definition_or_path,
      default_resolve: default_resolve,
      parser: parser,
      using: using,
    )
  end
end

.from_introspection(introspection_result) ⇒ Class<GraphQL::Schema>

Create schema with the result of an introspection query.

Parameters:

Returns:



102
103
104
# File 'lib/graphql/schema.rb', line 102

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

.get_field(type_or_name, field_name, context = GraphQL::Query::NullContext.instance) ⇒ Object



691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/graphql/schema.rb', line 691

def get_field(type_or_name, field_name, context = GraphQL::Query::NullContext.instance)
  parent_type = case type_or_name
  when LateBoundType
    get_type(type_or_name.name, context)
  when String
    get_type(type_or_name, context)
  when Module
    type_or_name
  else
    raise GraphQL::InvariantError, "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, context))
    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, context = GraphQL::Query::NullContext.instance) ⇒ Object



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

def get_fields(type, context = GraphQL::Query::NullContext.instance)
  type.fields(context)
end

.get_type(type_name, context = GraphQL::Query::NullContext.instance, use_visibility_profile = use_visibility_profile?) ) ⇒ Module?

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

Parameters:

  • type_name (String)
  • context (GraphQL::Query::Context) (defaults to: GraphQL::Query::NullContext.instance)

    Used for filtering definitions at query-time

  • use_visibility_profile (defaults to: use_visibility_profile?) )

    Private, for migration to Visibility

Returns:

  • (Module, nil)

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



372
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
401
402
403
404
405
406
407
408
# File 'lib/graphql/schema.rb', line 372

def get_type(type_name, context = GraphQL::Query::NullContext.instance, use_visibility_profile = use_visibility_profile?)
  if use_visibility_profile
    return Visibility::Profile.from_context(context, self).type(type_name)
  end
  local_entry = own_types[type_name]
  type_defn = case local_entry
  when nil
    nil
  when Array
    if context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)
      local_entry
    else
      visible_t = nil
      warden = Warden.from_context(context)
      local_entry.each do |t|
        if warden.visible_type?(t, context)
          if visible_t.nil?
            visible_t = t
          else
            raise DuplicateNamesError.new(
              duplicated_name: type_name, duplicated_definition_1: visible_t.inspect, duplicated_definition_2: t.inspect
            )
          end
        end
      end
      visible_t
    end
  when Module
    local_entry
  else
    raise "Invariant: unexpected own_types[#{type_name.inspect}]: #{local_entry.inspect}"
  end

  type_defn ||
    introspection_system.types[type_name] || # todo context-specific introspection?
    (superclass.respond_to?(:get_type) ? superclass.get_type(type_name, context, use_visibility_profile) : nil)
end

.handle_or_reraise(context, err) ⇒ 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.



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/graphql/schema.rb', line 1104

def handle_or_reraise(context, err)
  handler = Execution::Errors.find_handler_for(self, err.class)
  if handler
    obj = context[:current_object]
    args = context[:current_arguments]
    args = args && args.respond_to?(:keyword_arguments) ? args.keyword_arguments : nil
    field = context[:current_field]
    if obj.is_a?(GraphQL::Schema::Object)
      obj = obj.object
    end
    handler[:handler].call(err, obj, args, context, field)
  else
    raise err
  end
end

.has_defined_type?(type_name) ⇒ Boolean

Returns Does this schema have any definition for a type named type_name, regardless of visibility?.

Returns:

  • (Boolean)

    Does this schema have any definition for a type named type_name, regardless of visibility?



411
412
413
# File 'lib/graphql/schema.rb', line 411

def has_defined_type?(type_name)
  own_types.key?(type_name) || introspection_system.types.key?(type_name) || (superclass.respond_to?(:has_defined_type?) ? superclass.has_defined_type?(type_name) : false)
end

.id_from_object(application_object, graphql_type, context) ⇒ String

Return a stable ID string for object so that it can be refetched later, using object_from_id.

GlobalID(https://github.com/rails/globalid) and SQIDs(https://sqids.org/ruby) can both be used to create IDs.

Examples:

Using Rails’s GlobalID to generate IDs

def self.id_from_object(application_object, graphql_type, context)
  application_object.to_gid_param
end

Parameters:

  • application_object (Object)

    Some object encountered by GraphQL-Ruby while running a query

  • graphql_type (Class, Module)

    The type that GraphQL-Ruby is using for application_object during this query

  • context (GraphQL::Query::Context)

    The context for the operation that is currently running

Returns:

  • (String)

    A stable identifier which can be passed to object_from_id later to re-fetch application_object

Raises:



1216
1217
1218
# File 'lib/graphql/schema.rb', line 1216

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

.inherited(child_class) ⇒ Object

rubocop:enable Lint/DuplicateMethods



1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
# File 'lib/graphql/schema.rb', line 1168

def inherited(child_class)
  if self == GraphQL::Schema
    child_class.directives(default_directives.values)
    child_class.extend(SubclassGetReferencesTo)
  end
  # Make sure the child class has these built out, so that
  # subclasses can be modified by later calls to `trace_with`
  own_trace_modes.each do |name, _class|
    child_class.own_trace_modes[name] = child_class.build_trace_mode(name)
  end
  child_class.singleton_class.prepend(ResolveTypeWithType)

  if use_visibility_profile?
    vis = self.visibility
    child_class.visibility = vis.dup_for(child_class)
  end
  super
end

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



1307
1308
1309
1310
1311
1312
1313
1314
1315
# File 'lib/graphql/schema.rb', line 1307

def instrument(instrument_step, instrumenter, options = {})
  warn <<~WARN
  Schema.instrument is deprecated, use `trace_with` instead: https://graphql-ruby.org/queries/tracing.html"
    (From `#{self}.instrument(#{instrument_step}, #{instrumenter})` at #{caller(1, 1).first})

  WARN
  trace_with(Tracing::LegacyHooksTrace)
  own_instrumenters[instrument_step] << instrumenter
end

.instrumentersObject



1530
1531
1532
1533
1534
1535
# File 'lib/graphql/schema.rb', line 1530

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

.introspection(new_introspection_namespace = nil) ⇒ Module?

Pass a custom introspection module here to use it for this schema.

Parameters:

  • new_introspection_namespace (Module) (defaults to: nil)

    If given, use this module for custom introspection on the schema

Returns:

  • (Module, nil)

    The configured namespace, if there is one



721
722
723
724
725
726
727
728
729
730
731
# File 'lib/graphql/schema.rb', line 721

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

.introspection_systemSchema::IntrospectionSystem

Returns Based on introspection.

Returns:



734
735
736
737
738
739
740
741
# File 'lib/graphql/schema.rb', line 734

def introspection_system
  if !@introspection_system
    @introspection_system = Schema::IntrospectionSystem.new(self)
    @introspection_system.resolve_late_bindings
    self.visibility&.introspection_system_configured(@introspection_system)
  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



1595
1596
1597
# File 'lib/graphql/schema.rb', line 1595

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 with #lazy_resolve.

Returns:

  • (Symbol, nil)

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



1590
1591
1592
# File 'lib/graphql/schema.rb', line 1590

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

.lazy_resolve(lazy_class, value_method) ⇒ Object



1303
1304
1305
# File 'lib/graphql/schema.rb', line 1303

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

.load_type(type_name, ctx) ⇒ Object

Called when a type is needed by name at runtime



1234
1235
1236
# File 'lib/graphql/schema.rb', line 1234

def load_type(type_name, ctx)
  get_type(type_name, ctx)
end

.max_complexity_count_introspection_fieldsObject



874
875
876
877
878
879
880
# File 'lib/graphql/schema.rb', line 874

def max_complexity_count_introspection_fields
  if defined?(@max_complexity_count_introspection_fields)
    @max_complexity_count_introspection_fields
  else
    find_inherited_value(:max_complexity_count_introspection_fields, true)
  end
end

.max_query_string_tokens(new_max_tokens = NOT_CONFIGURED) ⇒ nil, Integer

A limit on the number of tokens to accept on incoming query strings. Use this to prevent parsing maliciously-large query strings.

Returns:

  • (nil, Integer)


761
762
763
764
765
766
767
# File 'lib/graphql/schema.rb', line 761

def max_query_string_tokens(new_max_tokens = NOT_CONFIGURED)
  if NOT_CONFIGURED.equal?(new_max_tokens)
    defined?(@max_query_string_tokens) ? @max_query_string_tokens : find_inherited_value(:max_query_string_tokens)
  else
    @max_query_string_tokens = new_max_tokens
  end
end

.multiplex(queries, **kwargs) ⇒ Array<GraphQL::Query::Result>

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:

See Also:

  • for query keyword arguments
  • for multiplex keyword arguments


1526
1527
1528
# File 'lib/graphql/schema.rb', line 1526

def multiplex(queries, **kwargs)
  GraphQL::Execution::Interpreter.run_all(self, queries, **kwargs)
end

.multiplex_analyzer(new_analyzer) ⇒ Object

Parameters:

See Also:



1467
1468
1469
# File 'lib/graphql/schema.rb', line 1467

def multiplex_analyzer(new_analyzer)
  own_multiplex_analyzers << new_analyzer
end

.multiplex_analyzersObject



1471
1472
1473
# File 'lib/graphql/schema.rb', line 1471

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

.mutation(new_mutation_object = nil, &lazy_load_block) ⇒ Class<GraphQL::Schema::Object>?

Get or set the root mutation { ... } object for this schema.

Examples:

Using Types::Mutation as the entry-point

mutation { Types::Mutation }

Parameters:

  • new_mutation_object (Class<GraphQL::Schema::Object>) (defaults to: nil)

    The root type to use for mutations

  • lazy_load_block

    If a block is given, then it will be called when GraphQL-Ruby needs the root mutation type.

Returns:



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/graphql/schema.rb', line 476

def mutation(new_mutation_object = nil, &lazy_load_block)
  if new_mutation_object || block_given?
    if @mutation_object
      dup_defn = new_mutation_object || yield
      raise GraphQL::Error, "Second definition of `mutation(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@mutation_object.inspect}"
    elsif use_visibility_profile?
      if block_given?
        @mutation_object = lazy_load_block
      else
        @mutation_object = new_mutation_object
        self.visibility.mutation_configured(@mutation_object)
      end
    else
      @mutation_object = new_mutation_object || lazy_load_block.call
      add_type_and_traverse(@mutation_object, root: true)
    end
    nil
  elsif @mutation_object.is_a?(Proc)
    @mutation_object = @mutation_object.call
    self.visibility&.mutation_configured(@mutation_object)
    @mutation_object
  else
    @mutation_object || find_inherited_value(:mutation)
  end
end

.mutation_execution_strategy(new_mutation_execution_strategy = nil, deprecation_warning: true) ⇒ Object



789
790
791
792
793
794
795
796
797
798
799
# File 'lib/graphql/schema.rb', line 789

def mutation_execution_strategy(new_mutation_execution_strategy = nil, deprecation_warning: true)
  if deprecation_warning
    warn "GraphQL::Schema.mutation_execution_strategy is deprecated without replacement. Use `GraphQL::Query.new` directly to create and execute a custom query instead."
      warn "  #{caller(1, 1).first}"
  end
  if new_mutation_execution_strategy
    @mutation_execution_strategy = new_mutation_execution_strategy
  else
    @mutation_execution_strategy || (superclass.respond_to?(:mutation_execution_strategy) ? superclass.mutation_execution_strategy(deprecation_warning: false) : self.default_execution_strategy)
  end
end

.new_trace(mode: nil, **options) ⇒ Tracing::Trace

Create a trace instance which will include the trace modules specified for the optional mode.

If no mode: is given, then default_trace_mode will be used.

Parameters:

  • mode (Symbol) (defaults to: nil)

    Trace modules for this trade mode will be included

  • options (Hash)

    Keywords that will be passed to the tracing class during #initialize

Returns:



1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
# File 'lib/graphql/schema.rb', line 1430

def new_trace(mode: nil, **options)
  target = options[:query] || options[:multiplex]
  mode ||= target && target.context[:trace_mode]

  trace_mode = if mode
    mode
  elsif target && target.context[:backtrace]
    if default_trace_mode != :default
      raise ArgumentError, "Can't use `context[:backtrace]` with a custom default trace mode (`#{dm.inspect}`)"
    else
      own_trace_modes[:default_backtrace] ||= build_trace_mode(:default_backtrace)
      options_trace_mode = :default
      :default_backtrace
    end
  else
    default_trace_mode
  end

  options_trace_mode ||= trace_mode
  base_trace_options = trace_options_for(options_trace_mode)
  trace_options = base_trace_options.merge(options)
  trace_class_for_mode = trace_class_for(trace_mode, build: true)
  trace_class_for_mode.new(**trace_options)
end

.object_from_id(object_id, context) ⇒ Object?

Fetch an object based on an incoming ID and the current context. This method should return an object from your application, or return nil if there is no object or the object shouldn’t be available to this operation.

Examples:

Fetching an object with Rails’s GlobalID

def self.object_from_id(object_id, _context)
  GlobalID.find(global_id)
  # TODO: use `context[:current_user]` to determine if this object is authorized.
end

Parameters:

  • object_id (String)

    The ID to fetch an object for. This may be client-provided (as in node(id: ...) or loads:) or previously stored by the schema (eg, by the ObjectCache)

  • context (GraphQL::Query::Context)

    The context for the currently-executing operation

Returns:

  • (Object, nil)

    The application which object_id references, or nil if there is no object or the current operation shouldn’t have access to the object

Raises:

See Also:



1199
1200
1201
# File 'lib/graphql/schema.rb', line 1199

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

.orphan_types(*new_orphan_types) ⇒ Array<Class<GraphQL::Schema::Object>>

Tell the schema about these types so that they can be registered as implementations of interfaces in the schema.

This method must be used when an object type is connected to the schema as an interface implementor but not as a return type of a field. In that case, if the object type isn’t registered here, GraphQL-Ruby won’t be able to find it.

Parameters:

  • new_orphan_types (Array<Class<GraphQL::Schema::Object>>)

    Object types to register as implementations of interfaces in the schema.

Returns:



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/graphql/schema.rb', line 989

def orphan_types(*new_orphan_types)
  if new_orphan_types.any?
    new_orphan_types = new_orphan_types.flatten
    non_object_types = new_orphan_types.reject { |ot| ot.is_a?(Class) && ot < GraphQL::Schema::Object }
    if non_object_types.any?
      raise ArgumentError, <<~ERR
        Only object type classes should be added as `orphan_types(...)`.

        - Remove these no-op types from `orphan_types`: #{non_object_types.map { |t| "#{t.inspect} (#{t.kind.name})"}.join(", ")}
        - See https://graphql-ruby.org/type_definitions/interfaces.html#orphan-types

        To add other types to your schema, you might want `extra_types`: https://graphql-ruby.org/schema/definition.html#extra-types
      ERR
    end
    add_type_and_traverse(new_orphan_types, root: false) unless use_visibility_profile?
    own_orphan_types.concat(new_orphan_types.flatten)
    self.visibility&.orphan_types_configured(new_orphan_types)
  end

  inherited_ot = find_inherited_value(:orphan_types, nil)
  if inherited_ot
    if own_orphan_types.any?
      inherited_ot + own_orphan_types
    else
      inherited_ot
    end
  else
    own_orphan_types
  end
end

.own_trace_modesObject



204
205
206
# File 'lib/graphql/schema.rb', line 204

def own_trace_modes
  @own_trace_modes ||= {}
end

.own_trace_modulesObject



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

def own_trace_modules
  @own_trace_modules ||= Hash.new { |h, k| h[k] = [] }
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



1299
1300
1301
# File 'lib/graphql/schema.rb', line 1299

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

.pluginsObject



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

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

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

Parameters:

  • type (Module) (defaults to: nil)

    The type definition whose possible types you want to see

  • context (GraphQL::Query::Context) (defaults to: GraphQL::Query::NullContext.instance)

    used for filtering visible possible types at runtime

  • use_visibility_profile (defaults to: use_visibility_profile?) )

    Private, for migration to Visibility

Returns:

  • (Hash<String, Module>)

    All possible types, if no type is given.

  • (Array<Module>)

    Possible types for type, if it’s given.



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/graphql/schema.rb', line 607

def possible_types(type = nil, context = GraphQL::Query::NullContext.instance, use_visibility_profile = use_visibility_profile?)
  if use_visibility_profile
    if type
      return Visibility::Profile.from_context(context, self).possible_types(type)
    else
      raise "Schema.possible_types is not implemented for `use_visibility_profile?`"
    end
  end
  if type
    # TODO duck-typing `.possible_types` would probably be nicer here
    if type.kind.union?
      type.possible_types(context: context)
    else
      stored_possible_types = own_possible_types[type]
      visible_possible_types = if stored_possible_types && type.kind.interface?
        stored_possible_types.select do |possible_type|
          possible_type.interfaces(context).include?(type)
        end
      else
        stored_possible_types
      end
      visible_possible_types ||
        introspection_system.possible_types[type] ||
        (
          superclass.respond_to?(:possible_types) ?
            superclass.possible_types(type, context, use_visibility_profile) :
            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, &lazy_load_block) ⇒ Class<GraphQL::Schema::Object>?

Get or set the root query { ... } object for this schema.

Examples:

Using Types::Query as the entry-point

query { Types::Query }

Parameters:

  • new_query_object (Class<GraphQL::Schema::Object>) (defaults to: nil)

    The root type to use for queries

  • lazy_load_block

    If a block is given, then it will be called when GraphQL-Ruby needs the root query type.

Returns:



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/graphql/schema.rb', line 442

def query(new_query_object = nil, &lazy_load_block)
  if new_query_object || block_given?
    if @query_object
      dup_defn = new_query_object || yield
      raise GraphQL::Error, "Second definition of `query(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@query_object.inspect}"
    elsif use_visibility_profile?
      if block_given?
        @query_object = lazy_load_block
      else
        @query_object = new_query_object
        self.visibility.query_configured(@query_object)
      end
    else
      @query_object = new_query_object || lazy_load_block.call
      add_type_and_traverse(@query_object, root: true)
    end
    nil
  elsif @query_object.is_a?(Proc)
    @query_object = @query_object.call
    self.visibility&.query_configured(@query_object)
    @query_object
  else
    @query_object || find_inherited_value(:query)
  end
end

.query_analyzer(new_analyzer) ⇒ Object

Parameters:

See Also:



1457
1458
1459
# File 'lib/graphql/schema.rb', line 1457

def query_analyzer(new_analyzer)
  own_query_analyzers << new_analyzer
end

.query_analyzersObject



1461
1462
1463
# File 'lib/graphql/schema.rb', line 1461

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

.query_class(new_query_class = NOT_CONFIGURED) ⇒ Object

Parameters:

  • new_query_class (Class<GraphQL::Query>) (defaults to: NOT_CONFIGURED)

    A subclass to use when executing queries



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

def query_class(new_query_class = NOT_CONFIGURED)
  if NOT_CONFIGURED.equal?(new_query_class)
    @query_class || (superclass.respond_to?(:query_class) ? superclass.query_class : GraphQL::Query)
  else
    @query_class = new_query_class
  end
end

.query_execution_strategy(new_query_execution_strategy = nil, deprecation_warning: true) ⇒ Object



777
778
779
780
781
782
783
784
785
786
787
# File 'lib/graphql/schema.rb', line 777

def query_execution_strategy(new_query_execution_strategy = nil, deprecation_warning: true)
  if deprecation_warning
    warn "GraphQL::Schema.query_execution_strategy is deprecated without replacement. Use `GraphQL::Query.new` directly to create and execute a custom query instead."
    warn "  #{caller(1, 1).first}"
  end
  if new_query_execution_strategy
    @query_execution_strategy = new_query_execution_strategy
  else
    @query_execution_strategy || (superclass.respond_to?(:query_execution_strategy) ? superclass.query_execution_strategy(deprecation_warning: false) : self.default_execution_strategy)
  end
end

.query_stack_error(query, err) ⇒ void

This method returns an undefined value.

Called when execution encounters a SystemStackError. By default, it adds a client-facing error to the response. You could modify this method to report this error to your bug tracker.

Parameters:



1555
1556
1557
# File 'lib/graphql/schema.rb', line 1555

def query_stack_error(query, err)
  query.context.errors.push(GraphQL::ExecutionError.new("This query is too large to execute."))
end

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



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/graphql/schema.rb', line 666

def references_to(to_type = nil, from: nil)
  if to_type
    if from
      refs = own_references_to[to_type] ||= []
      refs << from
    else
      get_references_to(to_type) || EMPTY_ARRAY
    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) {|error, object, arguments, context| ... } ⇒ Object

Register a handler for errors raised during execution. The handlers can return a new value or raise a new error.

Examples:

Handling “not found” with a client-facing error

rescue_from(ActiveRecord::NotFound) { raise GraphQL::ExecutionError, "An object could not be found" }

Parameters:

  • err_classes (Array<StandardError>)

    Classes which should be rescued by handler_block

  • handler_block

    The code to run when one of those errors is raised during execution

Yield Parameters:

  • error (StandardError)

    An instance of one of the configured err_classes

  • object (Object)

    The current application object in the query when the error was raised

  • arguments (GraphQL::Query::Arguments)

    The current field arguments when the error was raised

  • context (GraphQL::Query::Context)

    The context for the currently-running operation

Yield Returns:

  • (Object)

    Some object to use in the place where this error was raised

Raises:

  • (GraphQL::ExecutionError)

    In the handler, raise to add a client-facing error to the response

  • (StandardError)

    In the handler, raise to crash the query with a developer-facing error



1081
1082
1083
1084
1085
# File 'lib/graphql/schema.rb', line 1081

def rescue_from(*err_classes, &handler_block)
  err_classes.each do |err_class|
    Execution::Errors.register_rescue_from(err_class, error_handlers[:subclass_handlers], handler_block)
  end
end

.resolve_type(abstract_type, application_object, context) ⇒ Class<GraphQL::Schema::Object] The Object type definition to use for `obj`

GraphQL-Ruby calls this method during execution when it needs the application to determine the type to use for an object.

Usually, this object was returned from a field whose return type is an Interface or a Union. But this method is called in other cases, too – for example, when GraphQL::Schema::Argument.loads cases an object to be directly loaded from the database.

Examples:

Returning a GraphQL type based on the object’s class name

class MySchema < GraphQL::Schema
  def resolve_type(_abs_type, object, _context)
    graphql_type_name = "Types::#{object.class.name}Type"
    graphql_type_name.constantize # If this raises a NameError, then come implement special cases in this method
  end
end

Parameters:

  • abstract_type (Class, Module, nil)

    The Interface or Union type which is being resolved, if there is one

  • application_object (Object)

    The object returned from a field whose type must be determined

  • context (GraphQL::Query::Context)

    The query context for the currently-executing query

Returns:

Raises:



1163
1164
1165
# File 'lib/graphql/schema.rb', line 1163

def resolve_type(abstract_type, application_object, context)
  raise GraphQL::RequiredImplementationMissingError, "#{self.name}.resolve_type(abstract_type, application_object, context) must be implemented to use Union types, Interface types, or `loads:` (tried to resolve: #{abstract_type.name})"
end

.root_type_for_operation(operation) ⇒ 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.



540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/graphql/schema.rb', line 540

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<Class>

Returns The root types (query, mutation, subscription) defined for this schema.

Returns:

  • (Array<Class>)

    The root types (query, mutation, subscription) defined for this schema



554
555
556
557
558
559
560
# File 'lib/graphql/schema.rb', line 554

def root_types
  if use_visibility_profile?
    [query, mutation, subscription].compact
  else
    @root_types
  end
end

.sanitized_printer(new_sanitized_printer = nil) ⇒ Object



1475
1476
1477
1478
1479
1480
1481
# File 'lib/graphql/schema.rb', line 1475

def sanitized_printer(new_sanitized_printer = nil)
  if new_sanitized_printer
    @own_sanitized_printer = new_sanitized_printer
  else
    @own_sanitized_printer || GraphQL::Language::SanitizedPrinter
  end
end

.schema_directive(dir_class, **options) ⇒ Object



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

def schema_directive(dir_class, **options)
  @own_schema_directives ||= []
  Member::HasDirectives.add_directive(self, @own_schema_directives, dir_class, options)
end

.schema_directivesObject



1229
1230
1231
# File 'lib/graphql/schema.rb', line 1229

def schema_directives
  Member::HasDirectives.get_directives(self, @own_schema_directives, :schema_directives)
end

.static_validatorObject



316
317
318
# File 'lib/graphql/schema.rb', line 316

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

.subscription(new_subscription_object = nil, &lazy_load_block) ⇒ Class<GraphQL::Schema::Object>?

Get or set the root subscription { ... } object for this schema.

Examples:

Using Types::Subscription as the entry-point

subscription { Types::Subscription }

Parameters:

  • new_subscription_object (Class<GraphQL::Schema::Object>) (defaults to: nil)

    The root type to use for subscriptions

  • lazy_load_block

    If a block is given, then it will be called when GraphQL-Ruby needs the root subscription type.

Returns:



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/graphql/schema.rb', line 510

def subscription(new_subscription_object = nil, &lazy_load_block)
  if new_subscription_object || block_given?
    if @subscription_object
      dup_defn = new_subscription_object || yield
      raise GraphQL::Error, "Second definition of `subscription(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@subscription_object.inspect}"
    elsif use_visibility_profile?
      if block_given?
        @subscription_object = lazy_load_block
      else
        @subscription_object = new_subscription_object
        self.visibility.subscription_configured(@subscription_object)
      end
      add_subscription_extension_if_necessary
    else
      @subscription_object = new_subscription_object || lazy_load_block.call
      add_subscription_extension_if_necessary
      add_type_and_traverse(@subscription_object, root: true)
    end
    nil
  elsif @subscription_object.is_a?(Proc)
    @subscription_object = @subscription_object.call
    add_subscription_extension_if_necessary
    self.visibility.subscription_configured(@subscription_object)
    @subscription_object
  else
    @subscription_object || find_inherited_value(:subscription)
  end
end

.subscription_execution_strategy(new_subscription_execution_strategy = nil, deprecation_warning: true) ⇒ Object



801
802
803
804
805
806
807
808
809
810
811
# File 'lib/graphql/schema.rb', line 801

def subscription_execution_strategy(new_subscription_execution_strategy = nil, deprecation_warning: true)
  if deprecation_warning
    warn "GraphQL::Schema.subscription_execution_strategy is deprecated without replacement. Use `GraphQL::Query.new` directly to create and execute a custom query instead."
    warn "  #{caller(1, 1).first}"
  end
  if new_subscription_execution_strategy
    @subscription_execution_strategy = new_subscription_execution_strategy
  else
    @subscription_execution_strategy || (superclass.respond_to?(:subscription_execution_strategy) ? superclass.subscription_execution_strategy(deprecation_warning: false) : self.default_execution_strategy)
  end
end

.subscriptions(inherited: true) ⇒ GraphQL::Subscriptions



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

def subscriptions(inherited: true)
  defined?(@subscriptions) ? @subscriptions : (inherited ? find_inherited_value(:subscriptions, nil) : nil)
end

.subscriptions=(new_implementation) ⇒ Object



142
143
144
# File 'lib/graphql/schema.rb', line 142

def subscriptions=(new_implementation)
  @subscriptions = new_implementation
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.

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

Parameters:

Returns:

  • (Object)

    A GraphQL-ready (non-lazy) object



1579
1580
1581
1582
1583
1584
1585
1586
1587
# File 'lib/graphql/schema.rb', line 1579

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(context: {}) ⇒ String

Return the GraphQL IDL for the schema

Parameters:

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

Returns:

  • (String)


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

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

.to_documentGraphQL::Language::Document

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

Returns:

  • (GraphQL::Language::Document)


293
294
295
# File 'lib/graphql/schema.rb', line 293

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

.to_json(**args) ⇒ String

Returns the JSON response of Introspection::INTROSPECTION_QUERY.

Returns:

  • (String)

See Also:

  • {{#as_json}


258
259
260
# File 'lib/graphql/schema.rb', line 258

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

.trace_class(new_class = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/graphql/schema.rb', line 159

def trace_class(new_class = nil)
  if new_class
    # If any modules were already added for `:default`,
    # re-apply them here
    mods = trace_modules_for(:default)
    mods.each { |mod| new_class.include(mod) }
    new_class.include(DefaultTraceClass)
    trace_mode(:default, new_class)
    backtrace_class = Class.new(new_class)
    backtrace_class.include(GraphQL::Backtrace::Trace)
    trace_mode(:default_backtrace, backtrace_class)
  end
  trace_class_for(:default, build: true)
end

.trace_class_for(mode, build: false) ⇒ Class

Return the trace class to use for this mode, looking one up on the superclass if this Schema doesn’t have one defined.

Returns:

  • (Class)

    Return the trace class to use for this mode, looking one up on the superclass if this Schema doesn’t have one defined.



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

def trace_class_for(mode, build: false)
  if (trace_class = own_trace_modes[mode])
    trace_class
  elsif superclass.respond_to?(:trace_class_for) && (trace_class = superclass.trace_class_for(mode, build: false))
    trace_class
  elsif build
    own_trace_modes[mode] = build_trace_mode(mode)
  else
    nil
  end
end

.trace_mode(mode_name, trace_class) ⇒ Object

Configure trace_class to be used whenever context: { trace_mode: mode_name } is requested. default_trace_mode is used when no trace_mode: ... is requested.

When a trace_class is added this way, it will not receive other modules added with trace_with(...) unless trace_mode is explicitly given. (This class will not receive any default trace modules.)

Subclasses of the schema will use trace_class as a base class for this mode and those subclass also will not receive default tracing modules.

Parameters:

  • mode_name (Symbol)
  • trace_class (Class)

    subclass of GraphQL::Tracing::Trace

Returns:

  • void



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

def trace_mode(mode_name, trace_class)
  own_trace_modes[mode_name] = trace_class
  nil
end

.trace_modules_for(trace_mode) ⇒ Array<Module>

Returns Modules added for tracing in trace_mode, including inherited ones.

Returns:

  • (Array<Module>)

    Modules added for tracing in trace_mode, including inherited ones



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

def trace_modules_for(trace_mode)
  modules = own_trace_modules[trace_mode]
  if superclass.respond_to?(:trace_modules_for)
    modules += superclass.trace_modules_for(trace_mode)
  end
  modules
end

.trace_options_for(mode) ⇒ Hash

The options hash for this trace mode

Returns:

  • (Hash)


1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
# File 'lib/graphql/schema.rb', line 1408

def trace_options_for(mode)
  @trace_options_for_mode ||= {}
  @trace_options_for_mode[mode] ||= begin
    # It may be time to create an options hash for a mode that wasn't registered yet.
    # Mix in the default options in that case.
    default_options = mode == :default ? EMPTY_HASH : trace_options_for(:default)
    # Make sure this returns a new object so that other hashes aren't modified later
    if superclass.respond_to?(:trace_options_for)
      superclass.trace_options_for(mode).merge(default_options)
    else
      default_options.dup
    end
  end
end

.trace_with(trace_mod, mode: :default, **options) ⇒ void

This method returns an undefined value.

Mix trace_mod into this schema’s Trace class so that its methods will be called at runtime.

Parameters:

  • trace_mod (Module)

    A module that implements tracing methods

  • mode (Symbol) (defaults to: :default)

    Trace module will only be used for this trade mode

  • options (Hash)

    Keywords that will be passed to the tracing class during #initialize

See Also:



1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
# File 'lib/graphql/schema.rb', line 1378

def trace_with(trace_mod, mode: :default, **options)
  if mode.is_a?(Array)
    mode.each { |m| trace_with(trace_mod, mode: m, **options) }
  else
    tc = own_trace_modes[mode] ||= build_trace_mode(mode)
    tc.include(trace_mod)
    own_trace_modules[mode] << trace_mod
    add_trace_options_for(mode, options)
    if mode == :default
      # This module is being added as a default tracer. If any other mode classes
      # have already been created, but get their default behavior from a superclass,
      # Then mix this into this schema's subclass.
      # (But don't mix it into mode classes that aren't default-based.)
      own_trace_modes.each do |other_mode_name, other_mode_class|
        if other_mode_class < DefaultTraceClass
          # Don't add it back to the inheritance tree if it's already there
          if !(other_mode_class < trace_mod)
            other_mode_class.include(trace_mod)
          end
          # Add any options so they'll be available
          add_trace_options_for(other_mode_name, options)
        end
      end
    end
  end
  nil
end

.tracer(new_tracer, silence_deprecation_warning: false) ⇒ Object



1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'lib/graphql/schema.rb', line 1353

def tracer(new_tracer, silence_deprecation_warning: false)
  if !silence_deprecation_warning
    warn("`Schema.tracer(#{new_tracer.inspect})` is deprecated; use module-based `trace_with` instead. See: https://graphql-ruby.org/queries/tracing.html")
    warn "  #{caller(1, 1).first}"
  end
  default_trace = trace_class_for(:default, build: true)
  if default_trace.nil? || !(default_trace < GraphQL::Tracing::CallLegacyTracers)
    trace_with(GraphQL::Tracing::CallLegacyTracers)
  end

  own_tracers << new_tracer
end

.tracersObject



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

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

.type_error(type_error, ctx) ⇒ void

This method returns an undefined value.

Called at runtime when GraphQL-Ruby encounters a mismatch between the application behavior and the GraphQL type system.

The default implementation of this method is to follow the GraphQL specification, but you can override this to report errors to your bug tracker or customize error handling.

Parameters:

  • type_error (GraphQL::Error)

    several specific error classes are passed here, see the default implementation for details

  • context (GraphQL::Query::Context)

    the context for the currently-running operation

Raises:



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

def type_error(type_error, ctx)
  case type_error
  when GraphQL::InvalidNullError
    ctx.errors << type_error
  when GraphQL::UnresolvedTypeError, GraphQL::StringEncodingError, GraphQL::IntegerEncodingError
    raise type_error
  when GraphQL::IntegerDecodingError
    nil
  end
end

.type_from_ast(ast_node, context: self.query_class.new(self, "{ __typename }").context) ⇒ Object



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

def type_from_ast(ast_node, context: self.query_class.new(self, "{ __typename }").context)
  GraphQL::Schema::TypeExpression.build_type(context.query.types, ast_node)
end

.types(context = GraphQL::Query::NullContext.instance) ⇒ Hash<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:



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/graphql/schema.rb', line 339

def types(context = GraphQL::Query::NullContext.instance)
  if use_visibility_profile?
    types = Visibility::Profile.from_context(context, self)
    return types.all_types_h
  end
  all_types = non_introspection_types.merge(introspection_system.types)
  visible_types = {}
  all_types.each do |k, v|
    visible_types[k] =if v.is_a?(Array)
      visible_t = nil
      v.each do |t|
        if t.visible?(context)
          if visible_t.nil?
            visible_t = t
          else
            raise DuplicateNamesError.new(
              duplicated_name: k, duplicated_definition_1: visible_t.inspect, duplicated_definition_2: t.inspect
            )
          end
        end
      end
      visible_t
    else
      v
    end
  end
  visible_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



1268
1269
1270
# File 'lib/graphql/schema.rb', line 1268

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



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

def unauthorized_object(unauthorized_error)
  nil
end

.union_memberships(type = nil) ⇒ Object



643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/graphql/schema.rb', line 643

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

Add plugin to this schema

Parameters:

  • plugin (#use)

    A Schema plugin

Returns:

  • void



323
324
325
326
327
328
329
330
# File 'lib/graphql/schema.rb', line 323

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

.use_visibility_profile?Boolean

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.

Returns:

  • (Boolean)


592
593
594
595
596
597
598
599
600
# File 'lib/graphql/schema.rb', line 592

def use_visibility_profile?
  if defined?(@use_visibility_profile)
    @use_visibility_profile
  elsif superclass.respond_to?(:use_visibility_profile?)
    superclass.use_visibility_profile?
  else
    false
  end
end

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

Validate a query string according to this schema.

Parameters:

Returns:



828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/graphql/schema.rb', line 828

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 = query_class.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, timeout: validate_timeout, max_errors: validate_max_errors)
  res[:errors]
end

.visible?(member, ctx) ⇒ Boolean

Returns:

  • (Boolean)


1220
1221
1222
# File 'lib/graphql/schema.rb', line 1220

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