Class: GraphQL::Schema
- Inherits:
-
Object
- Object
- GraphQL::Schema
- Includes:
- Define::InstanceDefinable
- Defined in:
- lib/graphql/schema.rb,
lib/graphql/schema/loader.rb,
lib/graphql/schema/warden.rb,
lib/graphql/schema/printer.rb,
lib/graphql/schema/null_mask.rb,
lib/graphql/schema/traversal.rb,
lib/graphql/schema/validation.rb,
lib/graphql/schema/possible_types.rb,
lib/graphql/schema/base_64_encoder.rb,
lib/graphql/schema/type_expression.rb,
lib/graphql/schema/middleware_chain.rb,
lib/graphql/schema/rescue_middleware.rb,
lib/graphql/schema/default_type_error.rb,
lib/graphql/schema/invalid_type_error.rb,
lib/graphql/schema/timeout_middleware.rb,
lib/graphql/schema/unique_within_type.rb,
lib/graphql/schema/catchall_middleware.rb,
lib/graphql/schema/default_parse_error.rb,
lib/graphql/schema/build_from_definition.rb,
lib/graphql/schema/build_from_definition/resolve_map.rb,
lib/graphql/schema/build_from_definition/resolve_map/default_resolve.rb more...
Overview
A GraphQL schema which may be queried with Query.
The Schema contains:
- types for exposing your application
- query analyzers for assessing incoming queries (including max depth & max complexity restrictions)
- execution strategies for running incoming queries
- middleware for interacting with execution
Schemas start with root types, #query, #mutation and #subscription.
The schema will traverse the tree of fields & types, using those as starting points.
Any undiscoverable types may be provided with the types
configuration.
Schemas can restrict large incoming queries with max_depth
and max_complexity
configurations.
(These configurations can be overridden by specific calls to #execute)
Schemas can specify how queries should be executed against them.
query_execution_strategy
, mutation_execution_strategy
and subscription_execution_strategy
each apply to corresponding root types.
A schema accepts a Relay::GlobalNodeIdentification
instance for use with Relay IDs.
Defined Under Namespace
Modules: Base64Encoder, BuildFromDefinition, CatchallMiddleware, DefaultParseError, DefaultTypeError, Loader, NullMask, TypeExpression, UniqueWithinType Classes: CyclicalDefinitionError, InvalidDocumentError, InvalidTypeError, MiddlewareChain, PossibleTypes, Printer, RescueMiddleware, TimeoutMiddleware, Traversal, Validation, Warden
Constant Summary collapse
- BUILT_IN_TYPES =
- DIRECTIVES =
[GraphQL::Directive::IncludeDirective, GraphQL::Directive::SkipDirective, GraphQL::Directive::DeprecatedDirective]
- DYNAMIC_FIELDS =
["__type", "__typename", "__schema"]
Class Attribute Summary collapse
-
.default_execution_strategy ⇒ Object
Returns the value of attribute default_execution_strategy.
Instance Attribute Summary collapse
-
#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.
-
#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
-
.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.
Instance Method Summary collapse
-
#as_json(only: nil, except: nil, context: {}) ⇒ Hash
Return the Hash response of Introspection::INTROSPECTION_QUERY.
- #default_filter ⇒ Object
- #define(**kwargs, &block) ⇒ Object
-
#execute(query_str = nil, **kwargs) ⇒ Hash
Execute a query on itself.
- #execution_strategy_for_operation(operation) ⇒ Object
-
#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
. -
#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
permalink #initialize ⇒ Schema
Returns a new instance of Schema.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/graphql/schema.rb', line 116 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::Relay::ConnectionResolve::LazyNodesWrapper, :never_called) @cursor_encoder = Base64Encoder # Default to the built-in execution strategy: @query_execution_strategy = self.class.default_execution_strategy @mutation_execution_strategy = self.class.default_execution_strategy @subscription_execution_strategy = self.class.default_execution_strategy @default_mask = GraphQL::Schema::NullMask @rebuilding_artifacts = false end |
Class Attribute Details
permalink .default_execution_strategy ⇒ Object
Returns the value of attribute default_execution_strategy.
97 98 99 |
# File 'lib/graphql/schema.rb', line 97 def default_execution_strategy @default_execution_strategy end |
Instance Attribute Details
permalink #cursor_encoder ⇒ Object
Returns the value of attribute cursor_encoder.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def cursor_encoder @cursor_encoder end |
permalink #default_mask ⇒ <#call(member, ctx)>
Returns A callable for filtering members of the schema.
94 95 96 |
# File 'lib/graphql/schema.rb', line 94 def default_mask @default_mask end |
permalink #default_max_page_size ⇒ Object
Returns the value of attribute default_max_page_size.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def default_max_page_size @default_max_page_size end |
permalink #directives ⇒ Object
Returns the value of attribute directives.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def directives @directives end |
permalink #id_from_object_proc ⇒ Object (readonly)
Returns the value of attribute id_from_object_proc.
114 115 116 |
# File 'lib/graphql/schema.rb', line 114 def id_from_object_proc @id_from_object_proc end |
permalink #instrumenters ⇒ Object
Returns the value of attribute instrumenters.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def instrumenters @instrumenters end |
permalink #lazy_methods ⇒ Object
Returns the value of attribute lazy_methods.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def lazy_methods @lazy_methods end |
permalink #max_complexity ⇒ Object
Returns the value of attribute max_complexity.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def max_complexity @max_complexity end |
permalink #max_depth ⇒ Object
Returns the value of attribute max_depth.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def max_depth @max_depth end |
permalink #middleware ⇒ MiddlewareChain
Returns MiddlewareChain which is applied to fields during execution.
90 91 92 |
# File 'lib/graphql/schema.rb', line 90 def middleware @middleware end |
permalink #multiplex_analyzers ⇒ Object
Returns the value of attribute multiplex_analyzers.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def multiplex_analyzers @multiplex_analyzers end |
permalink #mutation ⇒ Object
Returns the value of attribute mutation.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def mutation @mutation end |
permalink #mutation_execution_strategy ⇒ Object
Returns the value of attribute mutation_execution_strategy.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def mutation_execution_strategy @mutation_execution_strategy end |
permalink #object_from_id_proc ⇒ Object (readonly)
Returns the value of attribute object_from_id_proc.
114 115 116 |
# File 'lib/graphql/schema.rb', line 114 def object_from_id_proc @object_from_id_proc end |
permalink #orphan_types ⇒ Object
Returns the value of attribute orphan_types.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def orphan_types @orphan_types end |
permalink #query ⇒ Object
Returns the value of attribute query.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def query @query end |
permalink #query_analyzers ⇒ Object
Returns the value of attribute query_analyzers.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def query_analyzers @query_analyzers end |
permalink #query_execution_strategy ⇒ Object
Returns the value of attribute query_execution_strategy.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def query_execution_strategy @query_execution_strategy end |
permalink #raise_definition_error ⇒ Object
Returns the value of attribute raise_definition_error.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def raise_definition_error @raise_definition_error end |
permalink #resolve_type_proc ⇒ Object (readonly)
Returns the value of attribute resolve_type_proc.
114 115 116 |
# File 'lib/graphql/schema.rb', line 114 def resolve_type_proc @resolve_type_proc end |
permalink #static_validator ⇒ Object (readonly)
Returns the value of attribute static_validator.
114 115 116 |
# File 'lib/graphql/schema.rb', line 114 def static_validator @static_validator end |
permalink #subscription ⇒ Object
Returns the value of attribute subscription.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def subscription @subscription end |
permalink #subscription_execution_strategy ⇒ Object
Returns the value of attribute subscription_execution_strategy.
76 77 78 |
# File 'lib/graphql/schema.rb', line 76 def subscription_execution_strategy @subscription_execution_strategy end |
permalink #subscriptions ⇒ GraphQL::Subscriptions
Single, long-lived instance of the provided subscriptions class, if there is one.
87 88 89 |
# File 'lib/graphql/schema.rb', line 87 def subscriptions @subscriptions end |
permalink #tracers ⇒ Array<#trace(key, data)> (readonly)
Returns Tracers applied to every query.
106 107 108 |
# File 'lib/graphql/schema.rb', line 106 def tracers @tracers end |
Class Method Details
permalink .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.
513 514 515 516 517 518 519 520 521 |
# File 'lib/graphql/schema.rb', line 513 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 |
permalink .from_introspection(introspection_result) ⇒ GraphQL::Schema
Create schema with the result of an introspection query.
504 505 506 |
# File 'lib/graphql/schema.rb', line 504 def self.from_introspection(introspection_result) GraphQL::Schema::Loader.load(introspection_result) end |
Instance Method Details
permalink #as_json(only: nil, except: nil, context: {}) ⇒ Hash
Return the Hash response of Introspection::INTROSPECTION_QUERY.
556 557 558 |
# File 'lib/graphql/schema.rb', line 556 def as_json(only: nil, except: nil, context: {}) execute(Introspection::INTROSPECTION_QUERY, only: only, except: except, context: context) end |
permalink #default_filter ⇒ Object
[View source]
100 101 102 |
# File 'lib/graphql/schema.rb', line 100 def default_filter GraphQL::Filter.new(except: default_mask) end |
permalink #define(**kwargs, &block) ⇒ Object
[View source]
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/graphql/schema.rb', line 194 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 |
permalink #execute(query_str = nil, **kwargs) ⇒ Hash
Execute a query on itself. Raises an error if the schema definition is invalid.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/graphql/schema.rb', line 261 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 |
permalink #execution_strategy_for_operation(operation) ⇒ Object
[View source]
368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/graphql/schema.rb', line 368 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 |
permalink #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
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/graphql/schema.rb', line 309 def get_field(parent_type, field_name) with_definition_error_check do parent_type_name = case parent_type when GraphQL::BaseType parent_type.name when String parent_type else raise "Unexpected parent_type: #{parent_type}" end defined_field = @instrumented_field_map[parent_type_name][field_name] if defined_field defined_field elsif field_name == "__typename" GraphQL::Introspection::TypenameField elsif field_name == "__schema" && parent_type == query GraphQL::Introspection::SchemaField elsif field_name == "__type" && parent_type == query GraphQL::Introspection::TypeByNameField else nil end end end |
permalink #get_fields(type) ⇒ Hash<String, GraphQL::Field>
Fields for this type, after instrumentation is applied
337 338 339 |
# File 'lib/graphql/schema.rb', line 337 def get_fields(type) @instrumented_field_map[type.name] end |
permalink #id_from_object(object, type, ctx) ⇒ String
Get a unique identifier from this object
488 489 490 491 492 493 494 |
# File 'lib/graphql/schema.rb', line 488 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 |
permalink #id_from_object=(new_proc) ⇒ Object
497 498 499 |
# File 'lib/graphql/schema.rb', line 497 def id_from_object=(new_proc) @id_from_object_proc = new_proc end |
permalink #initialize_copy(other) ⇒ Object
[View source]
142 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 |
# File 'lib/graphql/schema.rb', line 142 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 end |
permalink #instrument(instrumentation_type, instrumenter) ⇒ void
This method returns an undefined value.
Attach instrumenter
to this schema for instrumenting events of instrumentation_type
.
218 219 220 221 222 223 |
# File 'lib/graphql/schema.rb', line 218 def instrument(instrumentation_type, instrumenter) @instrumenters[instrumentation_type] << instrumenter if instrumentation_type == :field rebuild_artifacts end end |
permalink #lazy?(obj) ⇒ Boolean
Returns True if this object should be lazily resolved.
532 533 534 |
# File 'lib/graphql/schema.rb', line 532 def lazy?(obj) !!lazy_method_name(obj) end |
permalink #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.
527 528 529 |
# File 'lib/graphql/schema.rb', line 527 def lazy_method_name(obj) @lazy_methods.get(obj) end |
permalink #multiplex(queries, **kwargs) ⇒ Array<Hash>
Execute several queries on itself. Raises an error if the schema definition is invalid.
297 298 299 300 301 |
# File 'lib/graphql/schema.rb', line 297 def multiplex(queries, **kwargs) with_definition_error_check { GraphQL::Execution::Multiplex.run_all(self, queries, **kwargs) } end |
permalink #object_from_id(id, ctx) ⇒ Any
Fetch an application object by its unique id
426 427 428 429 430 431 432 |
# File 'lib/graphql/schema.rb', line 426 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 |
permalink #object_from_id=(new_proc) ⇒ Object
435 436 437 |
# File 'lib/graphql/schema.rb', line 435 def object_from_id=(new_proc) @object_from_id_proc = new_proc end |
permalink #parse_error(err, ctx) ⇒ Object
A function to call when #execute receives an invalid query string
474 475 476 |
# File 'lib/graphql/schema.rb', line 474 def parse_error(err, ctx) @parse_error_proc.call(err, ctx) end |
permalink #parse_error=(new_proc) ⇒ Object
479 480 481 |
# File 'lib/graphql/schema.rb', line 479 def parse_error=(new_proc) @parse_error_proc = new_proc end |
permalink #possible_types(type_defn) ⇒ Array<GraphQL::ObjectType>
Returns types which belong to type_defn
in this schema.
348 349 350 351 |
# File 'lib/graphql/schema.rb', line 348 def possible_types(type_defn) @possible_types ||= GraphQL::Schema::PossibleTypes.new(self) @possible_types.possible_types(type_defn) end |
permalink #references_to(type_name) ⇒ Hash
Returns a list of Arguments and Fields referencing a certain type
245 246 247 248 |
# File 'lib/graphql/schema.rb', line 245 def references_to(type_name) rebuild_artifacts unless defined?(@type_reference_map) @type_reference_map.fetch(type_name, []) end |
permalink #remove_handler(*args, &block) ⇒ Object
[View source]
173 174 175 |
# File 'lib/graphql/schema.rb', line 173 def remove_handler(*args, &block) rescue_middleware.remove_handler(*args, &block) end |
permalink #rescue_from(*args, &block) ⇒ Object
[View source]
169 170 171 |
# File 'lib/graphql/schema.rb', line 169 def rescue_from(*args, &block) rescue_middleware.rescue_from(*args, &block) end |
permalink #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)
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/graphql/schema.rb', line 388 def resolve_type(type, object, ctx = :__undefined__) if ctx == :__undefined__ # Old method signature ctx = object object = type type = nil end # Prefer a type-local function; fall back to the schema-level function type_proc = type && type.resolve_type_proc type_result = if type_proc type_proc.call(object, ctx) else if @resolve_type_proc.nil? raise(NotImplementedError, "Can't determine GraphQL type for: #{object.inspect}, define `resolve_type (type, obj, ctx) -> { ... }` inside `Schema.define`.") end @resolve_type_proc.call(type, object, ctx) end if type_result.nil? nil elsif !type_result.is_a?(GraphQL::BaseType) type_str = "#{type_result} (#{type_result.class.name})" raise "resolve_type(#{object}) returned #{type_str}, but it should return a GraphQL type" else type_result end end |
permalink #resolve_type=(new_resolve_type_proc) ⇒ Object
[View source]
417 418 419 420 |
# File 'lib/graphql/schema.rb', line 417 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 |
permalink #root_type_for_operation(operation) ⇒ GraphQL::ObjectType?
355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/graphql/schema.rb', line 355 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 |
permalink #root_types ⇒ Array<GraphQL::BaseType>
Returns The root types of this schema.
226 227 228 229 230 231 |
# File 'lib/graphql/schema.rb', line 226 def root_types @root_types ||= begin rebuild_artifacts @root_types end end |
permalink #to_definition(only: nil, except: nil, context: {}) ⇒ String
Return the GraphQL IDL for the schema
541 542 543 |
# File 'lib/graphql/schema.rb', line 541 def to_definition(only: nil, except: nil, context: {}) GraphQL::Schema::Printer.print_schema(self, only: only, except: except, context: context) end |
permalink #to_document ⇒ GraphQL::Language::Document
Return the GraphQL::Language::Document IDL AST for the schema
547 548 549 |
# File 'lib/graphql/schema.rb', line 547 def to_document GraphQL::Language::DocumentFromSchemaDefinition.new(self).document end |
permalink #to_json(*args) ⇒ String
Returns the JSON response of Introspection::INTROSPECTION_QUERY.
563 564 565 |
# File 'lib/graphql/schema.rb', line 563 def to_json(*args) JSON.pretty_generate(as_json(*args)) end |
permalink #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.
459 460 461 |
# File 'lib/graphql/schema.rb', line 459 def type_error(err, ctx) @type_error_proc.call(err, ctx) end |
permalink #type_error=(new_proc) ⇒ Object
464 465 466 |
# File 'lib/graphql/schema.rb', line 464 def type_error=(new_proc) @type_error_proc = new_proc end |
permalink #type_from_ast(ast_node) ⇒ Object
[View source]
341 342 343 |
# File 'lib/graphql/schema.rb', line 341 def type_from_ast(ast_node) GraphQL::Schema::TypeExpression.build_type(self.types, ast_node) end |
permalink #types ⇒ GraphQL::Schema::TypeMap
Returns { name => type }
pairs of types in this schema.
235 236 237 238 239 240 |
# File 'lib/graphql/schema.rb', line 235 def types @types ||= begin rebuild_artifacts @types end end |
permalink #union_memberships(type) ⇒ Array<GraphQL::UnionType>
Returns a list of Union types in which a type is a member
253 254 255 256 |
# File 'lib/graphql/schema.rb', line 253 def union_memberships(type) rebuild_artifacts unless defined?(@union_memberships) @union_memberships.fetch(type.name, []) end |
permalink #validate(string_or_document, rules: nil) ⇒ Array<GraphQL::StaticValidation::Message>
Validate a query string according to this schema.
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/graphql/schema.rb', line 180 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 |