Class: GraphQL::Schema Private
- Inherits:
-
Object
- Object
- GraphQL::Schema
- Extended by:
- Forwardable, Member::AcceptsDefinition
- 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/argument.rb,
lib/graphql/schema/mutation.rb,
lib/graphql/schema/non_null.rb,
lib/graphql/schema/resolver.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/built_in_types.rb,
lib/graphql/schema/possible_types.rb,
lib/graphql/schema/base_64_encoder.rb,
lib/graphql/schema/late_bound_type.rb,
lib/graphql/schema/type_expression.rb,
lib/graphql/schema/middleware_chain.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/introspection_system.rb,
lib/graphql/schema/member/has_arguments.rb,
lib/graphql/schema/build_from_definition.rb,
lib/graphql/schema/member/instrumentation.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/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
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The base class for things that make up the schema, eg objects, enums, scalars.
Defined Under Namespace
Modules: Base64Encoder, BuildFromDefinition, CatchallMiddleware, DefaultParseError, DefaultTypeError, Interface, Loader, MethodWrappers, NullMask, TypeExpression, UniqueWithinType Classes: Argument, CyclicalDefinitionError, Enum, EnumValue, Field, Finder, InputObject, IntrospectionSystem, InvalidDocumentError, InvalidTypeError, LateBoundType, List, Member, MiddlewareChain, Mutation, NonNull, Object, PossibleTypes, Printer, RelayClassicMutation, RescueMiddleware, Resolver, Scalar, TimeoutMiddleware, Traversal, Union, Validation, Warden
Constant Summary
- DIRECTIVES =
[GraphQL::Directive::IncludeDirective, GraphQL::Directive::SkipDirective, GraphQL::Directive::DeprecatedDirective]
- DYNAMIC_FIELDS =
["__type", "__typename", "__schema"]
- Context =
GraphQL::Query::Context
- BUILT_IN_TYPES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
{ "Int" => INT_TYPE, "String" => STRING_TYPE, "Float" => FLOAT_TYPE, "Boolean" => BOOLEAN_TYPE, "ID" => ID_TYPE, }
Class Attribute Summary collapse
-
.default_execution_strategy ⇒ Object
Returns the value of attribute default_execution_strategy.
Instance Attribute Summary collapse
-
#ast_node ⇒ Object
Returns the value of attribute ast_node.
-
#context_class ⇒ Class
Instantiated for each query.
-
#cursor_encoder ⇒ Object
Returns the value of attribute cursor_encoder.
-
#default_mask ⇒ <#call(member, ctx)>
A callable for filtering members of the schema.
-
#default_max_page_size ⇒ Object
Returns the value of attribute default_max_page_size.
-
#directives ⇒ Object
Returns the value of attribute directives.
-
#id_from_object_proc ⇒ Object
readonly
Returns the value of attribute id_from_object_proc.
-
#instrumenters ⇒ Object
Returns the value of attribute instrumenters.
-
#introspection_namespace ⇒ Object
Returns the value of attribute introspection_namespace.
-
#lazy_methods ⇒ Object
Returns the value of attribute lazy_methods.
-
#max_complexity ⇒ Object
Returns the value of attribute max_complexity.
-
#max_depth ⇒ Object
Returns the value of attribute max_depth.
-
#middleware ⇒ MiddlewareChain
MiddlewareChain which is applied to fields during execution.
-
#multiplex_analyzers ⇒ Object
Returns the value of attribute multiplex_analyzers.
-
#mutation ⇒ Object
Returns the value of attribute mutation.
-
#mutation_execution_strategy ⇒ Object
Returns the value of attribute mutation_execution_strategy.
-
#object_from_id_proc ⇒ Object
readonly
Returns the value of attribute object_from_id_proc.
-
#orphan_types ⇒ Object
Returns the value of attribute orphan_types.
-
#query ⇒ Object
Returns the value of attribute query.
-
#query_analyzers ⇒ Object
Returns the value of attribute query_analyzers.
-
#query_execution_strategy ⇒ Object
Returns the value of attribute query_execution_strategy.
-
#raise_definition_error ⇒ Object
Returns the value of attribute raise_definition_error.
-
#resolve_type_proc ⇒ Object
readonly
Returns the value of attribute resolve_type_proc.
-
#static_validator ⇒ Object
readonly
Returns the value of attribute static_validator.
-
#subscription ⇒ Object
Returns the value of attribute subscription.
-
#subscription_execution_strategy ⇒ Object
Returns the value of attribute subscription_execution_strategy.
-
#subscriptions ⇒ GraphQL::Subscriptions
Single, long-lived instance of the provided subscriptions class, if there is one.
-
#tracers ⇒ Array<#trace(key, data)>
readonly
Tracers applied to every query.
Class Method Summary collapse
-
.accessible?(member, context) ⇒ Boolean
-
.context_class(new_context_class = nil) ⇒ Object
-
.cursor_encoder(new_encoder = nil) ⇒ Object
-
.default_max_page_size(new_default_max_page_size = nil) ⇒ Object
-
.directives(new_directives = nil) ⇒ Object
-
.from_definition(definition_or_path, default_resolve: BuildFromDefinition::DefaultResolve, parser: BuildFromDefinition::DefaultParser) ⇒ GraphQL::Schema
Create schema from an IDL schema or file containing an IDL definition.
-
.from_introspection(introspection_result) ⇒ GraphQL::Schema
Create schema with the result of an introspection query.
-
.graphql_definition ⇒ Object
-
.id_from_object(object, type, ctx) ⇒ Object
-
.inaccessible_fields(error) ⇒ AnalysisError?
This hook is called when a client tries to access one or more fields that fail the
accessible?
check. -
.instrument(instrument_step, instrumenter, options = {}) ⇒ Object
-
.introspection(new_introspection_namespace = nil) ⇒ Object
-
.lazy_resolve(lazy_class, value_method) ⇒ Object
-
.max_complexity(max_complexity = nil) ⇒ Object
-
.max_depth(new_max_depth = nil) ⇒ Object
-
.middleware(new_middleware = nil) ⇒ Object
-
.multiplex_analyzer(new_analyzer) ⇒ Object
-
.mutation(new_mutation_object = nil) ⇒ Object
-
.object_from_id(node_id, ctx) ⇒ Object
-
.orphan_types(*new_orphan_types) ⇒ Object
-
.plugins ⇒ Object
-
.query(new_query_object = nil) ⇒ Object
-
.query_analyzer(new_analyzer) ⇒ Object
-
.rescue_from(err_class, &handler_block) ⇒ Object
-
.resolve_type(type, obj, ctx) ⇒ Object
-
.subscription(new_subscription_object = nil) ⇒ Object
-
.to_graphql ⇒ Object
-
.tracer(new_tracer) ⇒ Object
-
.type_error(type_err, ctx) ⇒ Object
-
.unauthorized_object(unauthorized_error) ⇒ Object
This hook is called when an object fails an
authorized?
check. -
.use(plugin, options = {}) ⇒ Object
-
.visible?(member, context) ⇒ Boolean
Instance Method Summary collapse
-
#after_lazy(value) ⇒ Object
private
Call the given block at the right time, either: - Right away, if
value
is not registered withlazy_resolve
- After resolvingvalue
, if it’s registered withlazy_resolve
(eg,Promise
). -
#as_json(only: nil, except: nil, context: {}) ⇒ Hash
Return the Hash response of Introspection::INTROSPECTION_QUERY.
-
#check_resolved_type(type, object, ctx = :__undefined__) ⇒ Object
private
This is a compatibility hack so that instance-level and class-level methods can get correctness checks without calling one another.
-
#default_filter ⇒ Object
-
#define(**kwargs, &block) ⇒ Object
-
#execute(query_str = nil, **kwargs) ⇒ Hash
Execute a query on itself.
-
#execution_strategy_for_operation(operation) ⇒ Object
-
#find(path) ⇒ GraphQL::BaseType, ...
Search for a schema member using a string path Schema.find(“Ensemble.musicians”).
-
#get_field(parent_type, field_name) ⇒ GraphQL::Field?
Resolve field named
field_name
for typeparent_type
. -
#get_fields(type) ⇒ Hash<String, GraphQL::Field>
Fields for this type, after instrumentation is applied.
-
#id_from_object(object, type, ctx) ⇒ String
Get a unique identifier from this object.
-
#id_from_object=(new_proc) ⇒ Object
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
-
#initialize_copy(other) ⇒ Object
-
#instrument(instrumentation_type, instrumenter) ⇒ void
Attach
instrumenter
to this schema for instrumenting events ofinstrumentation_type
. -
#introspection_system ⇒ Object
private
-
#lazy?(obj) ⇒ Boolean
True if this object should be lazily resolved.
-
#lazy_method_name(obj) ⇒ Symbol?
The method name to lazily resolve
obj
, or nil ifobj
’s class wasn’t registered wtih #lazy_resolve. -
#multiplex(queries, **kwargs) ⇒ Array<Hash>
Execute several queries on itself.
-
#object_from_id(id, ctx) ⇒ Any
Fetch an application object by its unique id.
-
#object_from_id=(new_proc) ⇒ Object
-
#parse_error(err, ctx) ⇒ Object
A function to call when #execute receives an invalid query string.
-
#parse_error=(new_proc) ⇒ Object
-
#possible_types(type_defn) ⇒ Array<GraphQL::ObjectType>
Types which belong to
type_defn
in this schema. -
#references_to(type_name) ⇒ Hash
Returns a list of Arguments and Fields referencing a certain type.
-
#remove_handler(*args, &block) ⇒ Object
-
#rescue_from(*args, &block) ⇒ Object
-
#resolve_type(type, object, ctx = :__undefined__) ⇒ GraphQL::ObjectType
Determine the GraphQL type for a given object.
-
#resolve_type=(new_resolve_type_proc) ⇒ Object
-
#root_type_for_operation(operation) ⇒ GraphQL::ObjectType?
-
#root_types ⇒ Array<GraphQL::BaseType>
The root types of this schema.
-
#to_definition(only: nil, except: nil, context: {}) ⇒ String
Return the GraphQL IDL for the schema.
-
#to_document ⇒ GraphQL::Language::Document
Return the GraphQL::Language::Document IDL AST for the schema.
-
#to_json(*args) ⇒ String
Returns the JSON response of Introspection::INTROSPECTION_QUERY.
-
#type_error(err, ctx) ⇒ Object
When we encounter a type error during query execution, we call this hook.
-
#type_error=(new_proc) ⇒ Object
-
#type_from_ast(ast_node) ⇒ Object
-
#types ⇒ GraphQL::Schema::TypeMap
{ name => type }
pairs of types in this schema. -
#union_memberships(type) ⇒ Array<GraphQL::UnionType>
Returns a list of Union types in which a type is a member.
-
#validate(string_or_document, rules: nil) ⇒ Array<GraphQL::StaticValidation::Message>
Validate a query string according to this schema.
Methods included from Define::InstanceDefinable
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/graphql/schema.rb', line 143 def initialize @tracers = [] @definition_error = nil @orphan_types = [] @directives = DIRECTIVES.reduce({}) { |m, d| m[d.name] = d; m } @static_validator = GraphQL::StaticValidation::Validator.new(schema: self) @middleware = MiddlewareChain.new(final_step: GraphQL::Execution::Execute::FieldResolveStep) @query_analyzers = [] @multiplex_analyzers = [] @resolve_type_proc = nil @object_from_id_proc = nil @id_from_object_proc = nil @type_error_proc = DefaultTypeError @parse_error_proc = DefaultParseError @instrumenters = Hash.new { |h, k| h[k] = [] } @lazy_methods = GraphQL::Execution::Lazy::LazyMethodMap.new @lazy_methods.set(GraphQL::Execution::Lazy, :value) @cursor_encoder = Base64Encoder # Default to the built-in execution strategy: @query_execution_strategy = self.class.default_execution_strategy @mutation_execution_strategy = self.class.default_execution_strategy @subscription_execution_strategy = self.class.default_execution_strategy @default_mask = GraphQL::Schema::NullMask @rebuilding_artifacts = false @context_class = GraphQL::Query::Context @introspection_namespace = nil @introspection_system = nil end |
Class Attribute Details
.default_execution_strategy ⇒ Object
Returns the value of attribute default_execution_strategy
125 126 127 |
# File 'lib/graphql/schema.rb', line 125 def default_execution_strategy @default_execution_strategy end |
Instance Attribute Details
#ast_node ⇒ Object
Returns the value of attribute ast_node
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def ast_node @ast_node end |
#context_class ⇒ Class
Returns Instantiated for each query
122 123 124 |
# File 'lib/graphql/schema.rb', line 122 def context_class @context_class end |
#cursor_encoder ⇒ Object
Returns the value of attribute cursor_encoder
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def cursor_encoder @cursor_encoder end |
#default_mask ⇒ <#call(member, ctx)>
Returns A callable for filtering members of the schema
118 119 120 |
# File 'lib/graphql/schema.rb', line 118 def default_mask @default_mask end |
#default_max_page_size ⇒ Object
Returns the value of attribute default_max_page_size
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def default_max_page_size @default_max_page_size end |
#directives ⇒ Object
Returns the value of attribute directives
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def directives @directives end |
#id_from_object_proc ⇒ Object (readonly)
Returns the value of attribute id_from_object_proc
141 142 143 |
# File 'lib/graphql/schema.rb', line 141 def id_from_object_proc @id_from_object_proc end |
#instrumenters ⇒ Object
Returns the value of attribute instrumenters
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def instrumenters @instrumenters end |
#introspection_namespace ⇒ Object
Returns the value of attribute introspection_namespace
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def introspection_namespace @introspection_namespace end |
#lazy_methods ⇒ Object
Returns the value of attribute lazy_methods
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def lazy_methods @lazy_methods end |
#max_complexity ⇒ Object
Returns the value of attribute max_complexity
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def max_complexity @max_complexity end |
#max_depth ⇒ Object
Returns the value of attribute max_depth
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def max_depth @max_depth end |
#middleware ⇒ MiddlewareChain
Returns MiddlewareChain which is applied to fields during execution
114 115 116 |
# File 'lib/graphql/schema.rb', line 114 def middleware @middleware end |
#multiplex_analyzers ⇒ Object
Returns the value of attribute multiplex_analyzers
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def multiplex_analyzers @multiplex_analyzers end |
#mutation ⇒ Object
Returns the value of attribute mutation
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def mutation @mutation end |
#mutation_execution_strategy ⇒ Object
Returns the value of attribute mutation_execution_strategy
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def mutation_execution_strategy @mutation_execution_strategy end |
#object_from_id_proc ⇒ Object (readonly)
Returns the value of attribute object_from_id_proc
141 142 143 |
# File 'lib/graphql/schema.rb', line 141 def object_from_id_proc @object_from_id_proc end |
#orphan_types ⇒ Object
Returns the value of attribute orphan_types
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def orphan_types @orphan_types end |
#query ⇒ Object
Returns the value of attribute query
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def query @query end |
#query_analyzers ⇒ Object
Returns the value of attribute query_analyzers
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def query_analyzers @query_analyzers end |
#query_execution_strategy ⇒ Object
Returns the value of attribute query_execution_strategy
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def query_execution_strategy @query_execution_strategy end |
#raise_definition_error ⇒ Object
Returns the value of attribute raise_definition_error
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def raise_definition_error @raise_definition_error end |
#resolve_type_proc ⇒ Object (readonly)
Returns the value of attribute resolve_type_proc
141 142 143 |
# File 'lib/graphql/schema.rb', line 141 def resolve_type_proc @resolve_type_proc end |
#static_validator ⇒ Object (readonly)
Returns the value of attribute static_validator
141 142 143 |
# File 'lib/graphql/schema.rb', line 141 def static_validator @static_validator end |
#subscription ⇒ Object
Returns the value of attribute subscription
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def subscription @subscription end |
#subscription_execution_strategy ⇒ Object
Returns the value of attribute subscription_execution_strategy
98 99 100 |
# File 'lib/graphql/schema.rb', line 98 def subscription_execution_strategy @subscription_execution_strategy end |
#subscriptions ⇒ GraphQL::Subscriptions
Single, long-lived instance of the provided subscriptions class, if there is one.
111 112 113 |
# File 'lib/graphql/schema.rb', line 111 def subscriptions @subscriptions end |
#tracers ⇒ Array<#trace(key, data)> (readonly)
Returns Tracers applied to every query
134 135 136 |
# File 'lib/graphql/schema.rb', line 134 def tracers @tracers end |
Class Method Details
.accessible?(member, context) ⇒ Boolean
852 853 854 |
# File 'lib/graphql/schema.rb', line 852 def accessible?(member, context) call_on_type_class(member, :accessible?, context, default: true) end |
.context_class(new_context_class = nil) ⇒ Object
823 824 825 826 827 828 829 |
# File 'lib/graphql/schema.rb', line 823 def context_class(new_context_class = nil) if new_context_class @context_class = new_context_class else @context_class || GraphQL::Query::Context end end |
.cursor_encoder(new_encoder = nil) ⇒ Object
776 777 778 779 780 781 |
# File 'lib/graphql/schema.rb', line 776 def cursor_encoder(new_encoder = nil) if new_encoder @cursor_encoder = new_encoder end @cursor_encoder || Base64Encoder end |
.default_max_page_size(new_default_max_page_size = nil) ⇒ Object
783 784 785 786 787 788 789 |
# File 'lib/graphql/schema.rb', line 783 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 end end |
.directives(new_directives = nil) ⇒ Object
904 905 906 907 908 909 910 |
# File 'lib/graphql/schema.rb', line 904 def directives(new_directives = nil) if new_directives @directives = new_directives.reduce({}) { |m, d| m[d.name] = d; m } end @directives ||= directives(DIRECTIVES) end |
.from_definition(definition_or_path, default_resolve: BuildFromDefinition::DefaultResolve, parser: BuildFromDefinition::DefaultParser) ⇒ GraphQL::Schema
Create schema from an IDL schema or file containing an IDL definition.
591 592 593 594 595 596 597 598 599 |
# File 'lib/graphql/schema.rb', line 591 def self.from_definition(definition_or_path, default_resolve: BuildFromDefinition::DefaultResolve, parser: BuildFromDefinition::DefaultParser) # If the file ends in `.graphql`, treat it like a filepath definition = if definition_or_path.end_with?(".graphql") File.read(definition_or_path) else definition_or_path end GraphQL::Schema::BuildFromDefinition.from_definition(definition, default_resolve: default_resolve, parser: parser) end |
.from_introspection(introspection_result) ⇒ GraphQL::Schema
Create schema with the result of an introspection query.
582 583 584 |
# File 'lib/graphql/schema.rb', line 582 def self.from_introspection(introspection_result) GraphQL::Schema::Loader.load(introspection_result) end |
.graphql_definition ⇒ Object
676 677 678 |
# File 'lib/graphql/schema.rb', line 676 def graphql_definition @graphql_definition ||= to_graphql end |
.id_from_object(object, type, ctx) ⇒ Object
844 845 846 |
# File 'lib/graphql/schema.rb', line 844 def id_from_object(object, type, ctx) raise NotImplementedError, "#{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.
864 865 866 |
# File 'lib/graphql/schema.rb', line 864 def inaccessible_fields(error) error end |
.instrument(instrument_step, instrumenter, options = {}) ⇒ Object
895 896 897 898 899 900 901 902 |
# File 'lib/graphql/schema.rb', line 895 def instrument(instrument_step, instrumenter, = {}) step = if instrument_step == :field && [:after_built_ins] :field_after_built_ins else instrument_step end defined_instrumenters[step] << instrumenter end |
.introspection(new_introspection_namespace = nil) ⇒ Object
768 769 770 771 772 773 774 |
# File 'lib/graphql/schema.rb', line 768 def introspection(new_introspection_namespace = nil) if new_introspection_namespace @introspection = new_introspection_namespace else @introspection end end |
.lazy_resolve(lazy_class, value_method) ⇒ Object
891 892 893 |
# File 'lib/graphql/schema.rb', line 891 def lazy_resolve(lazy_class, value_method) lazy_classes[lazy_class] = value_method end |
.max_complexity(max_complexity = nil) ⇒ Object
791 792 793 794 795 796 797 |
# File 'lib/graphql/schema.rb', line 791 def max_complexity(max_complexity = nil) if max_complexity @max_complexity = max_complexity else @max_complexity end end |
.max_depth(new_max_depth = nil) ⇒ Object
799 800 801 802 803 804 805 |
# File 'lib/graphql/schema.rb', line 799 def max_depth(new_max_depth = nil) if new_max_depth @max_depth = new_max_depth else @max_depth end end |
.middleware(new_middleware = nil) ⇒ Object
920 921 922 923 924 925 926 |
# File 'lib/graphql/schema.rb', line 920 def middleware(new_middleware = nil) if new_middleware defined_middleware << new_middleware else graphql_definition.middleware end end |
.multiplex_analyzer(new_analyzer) ⇒ Object
928 929 930 |
# File 'lib/graphql/schema.rb', line 928 def multiplex_analyzer(new_analyzer) defined_multiplex_analyzers << new_analyzer end |
.mutation(new_mutation_object = nil) ⇒ Object
752 753 754 755 756 757 758 |
# File 'lib/graphql/schema.rb', line 752 def mutation(new_mutation_object = nil) if new_mutation_object @mutation_object = new_mutation_object else @mutation_object.respond_to?(:graphql_definition) ? @mutation_object.graphql_definition : @mutation_object end end |
.object_from_id(node_id, ctx) ⇒ Object
840 841 842 |
# File 'lib/graphql/schema.rb', line 840 def object_from_id(node_id, ctx) raise NotImplementedError, "#{self.name}.object_from_id(node_id, ctx) must be implemented to use the `node` field (tried to load from id `#{node_id}`)" end |
.orphan_types(*new_orphan_types) ⇒ Object
807 808 809 810 811 812 813 |
# File 'lib/graphql/schema.rb', line 807 def orphan_types(*new_orphan_types) if new_orphan_types.any? @orphan_types = new_orphan_types.flatten else @orphan_types || [] end end |
.plugins ⇒ Object
684 685 686 |
# File 'lib/graphql/schema.rb', line 684 def plugins @plugins ||= [] end |
.query(new_query_object = nil) ⇒ Object
744 745 746 747 748 749 750 |
# File 'lib/graphql/schema.rb', line 744 def query(new_query_object = nil) if new_query_object @query_object = new_query_object else @query_object.respond_to?(:graphql_definition) ? @query_object.graphql_definition : @query_object end end |
.query_analyzer(new_analyzer) ⇒ Object
916 917 918 |
# File 'lib/graphql/schema.rb', line 916 def query_analyzer(new_analyzer) defined_query_analyzers << new_analyzer end |
.rescue_from(err_class, &handler_block) ⇒ Object
831 832 833 834 |
# File 'lib/graphql/schema.rb', line 831 def rescue_from(err_class, &handler_block) @rescues ||= {} @rescues[err_class] = handler_block end |
.resolve_type(type, obj, ctx) ⇒ Object
836 837 838 |
# File 'lib/graphql/schema.rb', line 836 def resolve_type(type, obj, ctx) raise NotImplementedError, "#{self.name}.resolve_type(type, obj, ctx) must be implemented to use Union types or Interface types (tried to resolve: #{type.name})" end |
.subscription(new_subscription_object = nil) ⇒ Object
760 761 762 763 764 765 766 |
# File 'lib/graphql/schema.rb', line 760 def subscription(new_subscription_object = nil) if new_subscription_object @subscription_object = new_subscription_object else @subscription_object.respond_to?(:graphql_definition) ? @subscription_object.graphql_definition : @subscription_object end end |
.to_graphql ⇒ Object
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 |
# File 'lib/graphql/schema.rb', line 688 def to_graphql schema_defn = self.new schema_defn.raise_definition_error = true schema_defn.query = query schema_defn.mutation = mutation schema_defn.subscription = subscription schema_defn.max_complexity = max_complexity schema_defn.max_depth = max_depth schema_defn.default_max_page_size = default_max_page_size schema_defn.orphan_types = orphan_types schema_defn.directives = directives 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(defined_tracers) schema_defn.query_analyzers.concat(defined_query_analyzers) schema_defn.query_analyzers << GraphQL::Authorization::Analyzer schema_defn.middleware.concat(defined_middleware) schema_defn.multiplex_analyzers.concat(defined_multiplex_analyzers) defined_instrumenters.each do |step, insts| insts.each do |inst| schema_defn.instrumenters[step] << inst end end schema_defn.instrumenters[:query] << GraphQL::Schema::Member::Instrumentation lazy_classes.each do |lazy_class, value_method| schema_defn.lazy_methods.set(lazy_class, value_method) end if @rescues @rescues.each do |err_class, handler| schema_defn.rescue_from(err_class, &handler) end end if plugins.any? schema_plugins = plugins # TODO don't depend on .define schema_defn = schema_defn.redefine do schema_plugins.each do |plugin, | if .any? use(plugin, **) else use(plugin) end end end end schema_defn.send(:rebuild_artifacts) schema_defn end |
.tracer(new_tracer) ⇒ Object
912 913 914 |
# File 'lib/graphql/schema.rb', line 912 def tracer(new_tracer) defined_tracers << new_tracer end |
.type_error(type_err, ctx) ⇒ Object
887 888 889 |
# File 'lib/graphql/schema.rb', line 887 def type_error(type_err, ctx) DefaultTypeError.call(type_err, ctx) 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 ass 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.
883 884 885 |
# File 'lib/graphql/schema.rb', line 883 def () nil end |
.use(plugin, options = {}) ⇒ Object
680 681 682 |
# File 'lib/graphql/schema.rb', line 680 def use(plugin, = {}) plugins << [plugin, ] end |
.visible?(member, context) ⇒ Boolean
848 849 850 |
# File 'lib/graphql/schema.rb', line 848 def visible?(member, context) call_on_type_class(member, :visible?, context, default: true) end |
Instance Method Details
#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
)
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 |
# File 'lib/graphql/schema.rb', line 1002 def after_lazy(value) if (lazy_method = lazy_method_name(value)) GraphQL::Execution::Lazy.new do result = value.public_send(lazy_method) # 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.
634 635 636 |
# File 'lib/graphql/schema.rb', line 634 def as_json(only: nil, except: nil, context: {}) execute(Introspection::INTROSPECTION_QUERY, only: only, except: except, context: context).to_h 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
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/graphql/schema.rb', line 453 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.respond_to?(:graphql_definition) type_result = type_result.graphql_definition end if type_result.nil? nil elsif !type_result.is_a?(GraphQL::BaseType) type_str = "#{type_result} (#{type_result.class.name})" raise "resolve_type(#{object}) returned #{type_str}, but it should return a GraphQL type" else type_result end end |
#default_filter ⇒ Object
128 129 130 |
# File 'lib/graphql/schema.rb', line 128 def default_filter GraphQL::Filter.new(except: default_mask) end |
#define(**kwargs, &block) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/graphql/schema.rb', line 228 def define(**kwargs, &block) super ensure_defined # Assert that all necessary configs are present: validation_error = Validation.validate(self) validation_error && raise(NotImplementedError, validation_error) rebuild_artifacts @definition_error = nil nil rescue StandardError => err if @raise_definition_error || err.is_a?(CyclicalDefinitionError) raise else # Raise this error _later_ to avoid messing with Rails constant loading @definition_error = err end nil end |
#execute(query_str = nil, **kwargs) ⇒ Hash
Execute a query on itself. Raises an error if the schema definition is invalid.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/graphql/schema.rb', line 303 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
421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/graphql/schema.rb', line 421 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”)
353 354 355 356 |
# File 'lib/graphql/schema.rb', line 353 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
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/graphql/schema.rb', line 364 def get_field(parent_type, field_name) with_definition_error_check do parent_type_name = case parent_type when GraphQL::BaseType parent_type.name when String parent_type else raise "Unexpected parent_type: #{parent_type}" end defined_field = @instrumented_field_map[parent_type_name][field_name] if defined_field defined_field elsif 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
390 391 392 |
# File 'lib/graphql/schema.rb', line 390 def get_fields(type) @instrumented_field_map[type.name] end |
#id_from_object(object, type, ctx) ⇒ String
Get a unique identifier from this object
566 567 568 569 570 571 572 |
# File 'lib/graphql/schema.rb', line 566 def id_from_object(object, type, ctx) if @id_from_object_proc.nil? raise(NotImplementedError, "Can't generate an ID for #{object.inspect} of type #{type}, schema's `id_from_object` must be defined") else @id_from_object_proc.call(object, type, ctx) end end |
#id_from_object=(new_proc) ⇒ Object
575 576 577 |
# File 'lib/graphql/schema.rb', line 575 def id_from_object=(new_proc) @id_from_object_proc = new_proc end |
#initialize_copy(other) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/graphql/schema.rb', line 172 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 |
#instrument(instrumentation_type, instrumenter) ⇒ void
This method returns an undefined value.
Attach instrumenter
to this schema for instrumenting events of instrumentation_type
.
252 253 254 255 256 257 |
# File 'lib/graphql/schema.rb', line 252 def instrument(instrumentation_type, instrumenter) @instrumenters[instrumentation_type] << instrumenter if instrumentation_type == :field rebuild_artifacts end end |
#introspection_system ⇒ 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.
277 278 279 280 281 282 |
# File 'lib/graphql/schema.rb', line 277 def introspection_system @introspection_system ||= begin rebuild_artifacts @introspection_system end end |
#lazy?(obj) ⇒ Boolean
Returns True if this object should be lazily resolved
610 611 612 |
# File 'lib/graphql/schema.rb', line 610 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.
605 606 607 |
# File 'lib/graphql/schema.rb', line 605 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.
339 340 341 342 343 |
# File 'lib/graphql/schema.rb', line 339 def multiplex(queries, **kwargs) with_definition_error_check { GraphQL::Execution::Multiplex.run_all(self, queries, **kwargs) } end |
#object_from_id(id, ctx) ⇒ Any
Fetch an application object by its unique id
500 501 502 503 504 505 506 |
# File 'lib/graphql/schema.rb', line 500 def object_from_id(id, ctx) if @object_from_id_proc.nil? raise(NotImplementedError, "Can't fetch an object for id \"#{id}\" because the schema's `object_from_id (id, ctx) -> { ... }` function is not defined") else @object_from_id_proc.call(id, ctx) end end |
#object_from_id=(new_proc) ⇒ Object
509 510 511 |
# File 'lib/graphql/schema.rb', line 509 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
552 553 554 |
# File 'lib/graphql/schema.rb', line 552 def parse_error(err, ctx) @parse_error_proc.call(err, ctx) end |
#parse_error=(new_proc) ⇒ Object
557 558 559 |
# File 'lib/graphql/schema.rb', line 557 def parse_error=(new_proc) @parse_error_proc = new_proc end |
#possible_types(type_defn) ⇒ Array<GraphQL::ObjectType>
Returns types which belong to type_defn
in this schema
401 402 403 404 |
# File 'lib/graphql/schema.rb', line 401 def possible_types(type_defn) @possible_types ||= GraphQL::Schema::PossibleTypes.new(self) @possible_types.possible_types(type_defn) end |
#references_to(type_name) ⇒ Hash
Returns a list of Arguments and Fields referencing a certain type
287 288 289 290 |
# File 'lib/graphql/schema.rb', line 287 def references_to(type_name) rebuild_artifacts unless defined?(@type_reference_map) @type_reference_map.fetch(type_name, []) end |
#remove_handler(*args, &block) ⇒ Object
204 205 206 |
# File 'lib/graphql/schema.rb', line 204 def remove_handler(*args, &block) rescue_middleware.remove_handler(*args, &block) end |
#rescue_from(*args, &block) ⇒ Object
200 201 202 |
# File 'lib/graphql/schema.rb', line 200 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)
441 442 443 444 445 446 447 448 |
# File 'lib/graphql/schema.rb', line 441 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(NotImplementedError, "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
491 492 493 494 |
# File 'lib/graphql/schema.rb', line 491 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?
408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/graphql/schema.rb', line 408 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_types ⇒ Array<GraphQL::BaseType>
Returns The root types of this schema
260 261 262 263 264 265 |
# File 'lib/graphql/schema.rb', line 260 def root_types @root_types ||= begin rebuild_artifacts @root_types end end |
#to_definition(only: nil, except: nil, context: {}) ⇒ String
Return the GraphQL IDL for the schema
619 620 621 |
# File 'lib/graphql/schema.rb', line 619 def to_definition(only: nil, except: nil, context: {}) GraphQL::Schema::Printer.print_schema(self, only: only, except: except, context: context) end |
#to_document ⇒ GraphQL::Language::Document
Return the GraphQL::Language::Document IDL AST for the schema
625 626 627 |
# File 'lib/graphql/schema.rb', line 625 def to_document GraphQL::Language::DocumentFromSchemaDefinition.new(self).document end |
#to_json(*args) ⇒ String
Returns the JSON response of Introspection::INTROSPECTION_QUERY.
641 642 643 |
# File 'lib/graphql/schema.rb', line 641 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.
533 534 535 |
# File 'lib/graphql/schema.rb', line 533 def type_error(err, ctx) @type_error_proc.call(err, ctx) end |
#type_error=(new_proc) ⇒ Object
538 539 540 |
# File 'lib/graphql/schema.rb', line 538 def type_error=(new_proc) @type_error_proc = new_proc end |
#type_from_ast(ast_node) ⇒ Object
394 395 396 |
# File 'lib/graphql/schema.rb', line 394 def type_from_ast(ast_node) GraphQL::Schema::TypeExpression.build_type(self.types, ast_node) end |
#types ⇒ GraphQL::Schema::TypeMap
Returns { name => type }
pairs of types in this schema
269 270 271 272 273 274 |
# File 'lib/graphql/schema.rb', line 269 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
295 296 297 298 |
# File 'lib/graphql/schema.rb', line 295 def union_memberships(type) rebuild_artifacts unless defined?(@union_memberships) @union_memberships.fetch(type.name, []) end |
#validate(string_or_document, rules: nil) ⇒ Array<GraphQL::StaticValidation::Message>
Validate a query string according to this schema.
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/graphql/schema.rb', line 214 def validate(string_or_document, rules: nil) doc = if string_or_document.is_a?(String) GraphQL.parse(string_or_document) else string_or_document end query = GraphQL::Query.new(self, document: doc) validator_opts = { schema: self } rules && (validator_opts[:rules] = rules) validator = GraphQL::StaticValidation::Validator.new(validator_opts) res = validator.validate(query) res[:errors] end |