Class: GraphQL::Schema
- Inherits:
-
Object
- Object
- GraphQL::Schema
- Includes:
- Define::InstanceDefinable
- Defined in:
- lib/graphql/schema.rb,
lib/graphql/schema/mask.rb,
lib/graphql/schema/loader.rb,
lib/graphql/schema/warden.rb,
lib/graphql/schema/printer.rb,
lib/graphql/schema/type_map.rb,
lib/graphql/schema/null_mask.rb,
lib/graphql/schema/validation.rb,
lib/graphql/schema/reduce_types.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/instrumented_field_map.rb
Overview
A GraphQL schema which may be queried with Query.
The Schema contains:
- types for exposing your application
- query analyzers for assessing incoming queries (including max depth & max complexity restrictions)
- execution strategies for running incoming queries
- middleware for interacting with execution
Schemas start with root types, #query, #mutation and #subscription.
The schema will traverse the tree of fields & types, using those as starting points.
Any undiscoverable types may be provided with the types
configuration.
Schemas can restrict large incoming queries with max_depth
and max_complexity
configurations.
(These configurations can be overridden by specific calls to #execute)
Schemas can specify how queries should be executed against them.
query_execution_strategy
, mutation_execution_strategy
and subscription_execution_strategy
each apply to corresponding root types.
A schema accepts a Relay::GlobalNodeIdentification
instance for use with Relay IDs.
Defined Under Namespace
Modules: Base64Encoder, BuildFromDefinition, CatchallMiddleware, DefaultParseError, DefaultTypeError, Loader, Mask, NullMask, ReduceTypes, TypeExpression, UniqueWithinType Classes: InstrumentedFieldMap, InvalidDocumentError, InvalidTypeError, MiddlewareChain, PossibleTypes, Printer, RescueMiddleware, TimeoutMiddleware, TypeMap, 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.
-
#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.
-
#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.
-
#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.
Class Method Summary collapse
-
.from_definition(string, default_resolve: BuildFromDefinition::DefaultResolve) ⇒ GraphQL::Schema
Create schema from an IDL schema.
-
.from_introspection(introspection_result) ⇒ GraphQL::Schema
Create schema with the result of an introspection query.
Instance Method Summary collapse
- #define(**kwargs, &block) ⇒ Object
-
#execute(*args) ⇒ 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
. -
#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. -
#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. - #remove_handler(*args, &block) ⇒ Object
- #rescue_from(*args, &block) ⇒ Object
-
#resolve_type(object, ctx) ⇒ GraphQL::ObjectType
Determine the GraphQL type for a given object.
- #resolve_type=(new_resolve_type_proc) ⇒ Object
- #root_type_for_operation(operation) ⇒ GraphQL::ObjectType?
-
#to_definition(only: nil, except: nil, context: {}) ⇒ Object
Return a GraphQL schema string for the defined types in the schema.
-
#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. -
#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.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/graphql/schema.rb', line 98 def initialize @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 = [] @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 @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 end |
Class Attribute Details
.default_execution_strategy ⇒ Object
Returns the value of attribute default_execution_strategy.
87 88 89 |
# File 'lib/graphql/schema.rb', line 87 def default_execution_strategy @default_execution_strategy end |
Instance Attribute Details
#cursor_encoder ⇒ Object
Returns the value of attribute cursor_encoder.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def cursor_encoder @cursor_encoder end |
#default_mask ⇒ <#call(member, ctx)>
Returns A callable for filtering members of the schema.
84 85 86 |
# File 'lib/graphql/schema.rb', line 84 def default_mask @default_mask end |
#directives ⇒ Object
Returns the value of attribute directives.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def directives @directives end |
#id_from_object_proc ⇒ Object (readonly)
Returns the value of attribute id_from_object_proc.
96 97 98 |
# File 'lib/graphql/schema.rb', line 96 def id_from_object_proc @id_from_object_proc end |
#instrumenters ⇒ Object
Returns the value of attribute instrumenters.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def instrumenters @instrumenters end |
#lazy_methods ⇒ Object
Returns the value of attribute lazy_methods.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def lazy_methods @lazy_methods end |
#max_complexity ⇒ Object
Returns the value of attribute max_complexity.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def max_complexity @max_complexity end |
#max_depth ⇒ Object
Returns the value of attribute max_depth.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def max_depth @max_depth end |
#middleware ⇒ MiddlewareChain
Returns MiddlewareChain which is applied to fields during execution.
80 81 82 |
# File 'lib/graphql/schema.rb', line 80 def middleware @middleware end |
#mutation ⇒ Object
Returns the value of attribute mutation.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def mutation @mutation end |
#mutation_execution_strategy ⇒ Object
Returns the value of attribute mutation_execution_strategy.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def mutation_execution_strategy @mutation_execution_strategy end |
#object_from_id_proc ⇒ Object (readonly)
Returns the value of attribute object_from_id_proc.
96 97 98 |
# File 'lib/graphql/schema.rb', line 96 def object_from_id_proc @object_from_id_proc end |
#orphan_types ⇒ Object
Returns the value of attribute orphan_types.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def orphan_types @orphan_types end |
#query ⇒ Object
Returns the value of attribute query.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def query @query end |
#query_analyzers ⇒ Object
Returns the value of attribute query_analyzers.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def query_analyzers @query_analyzers end |
#query_execution_strategy ⇒ Object
Returns the value of attribute query_execution_strategy.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def query_execution_strategy @query_execution_strategy end |
#resolve_type_proc ⇒ Object (readonly)
Returns the value of attribute resolve_type_proc.
96 97 98 |
# File 'lib/graphql/schema.rb', line 96 def resolve_type_proc @resolve_type_proc end |
#static_validator ⇒ Object (readonly)
Returns the value of attribute static_validator.
96 97 98 |
# File 'lib/graphql/schema.rb', line 96 def static_validator @static_validator end |
#subscription ⇒ Object
Returns the value of attribute subscription.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def subscription @subscription end |
#subscription_execution_strategy ⇒ Object
Returns the value of attribute subscription_execution_strategy.
71 72 73 |
# File 'lib/graphql/schema.rb', line 71 def subscription_execution_strategy @subscription_execution_strategy end |
Class Method Details
.from_definition(string, default_resolve: BuildFromDefinition::DefaultResolve) ⇒ GraphQL::Schema
Create schema from an IDL schema.
383 384 385 |
# File 'lib/graphql/schema.rb', line 383 def self.from_definition(string, default_resolve: BuildFromDefinition::DefaultResolve) GraphQL::Schema::BuildFromDefinition.from_definition(string, default_resolve: default_resolve) end |
.from_introspection(introspection_result) ⇒ GraphQL::Schema
Create schema with the result of an introspection query.
375 376 377 |
# File 'lib/graphql/schema.rb', line 375 def self.from_introspection(introspection_result) GraphQL::Schema::Loader.load(introspection_result) end |
Instance Method Details
#define(**kwargs, &block) ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/graphql/schema.rb', line 171 def define(**kwargs, &block) super ensure_defined build_types_map # Assert that all necessary configs are present: validation_error = Validation.validate(self) validation_error && raise(NotImplementedError, validation_error) build_instrumented_field_map nil end |
#execute(*args) ⇒ Hash
Execute a query on itself. See Query#initialize for arguments.
202 203 204 205 |
# File 'lib/graphql/schema.rb', line 202 def execute(*args) query_obj = GraphQL::Query.new(self, *args) query_obj.result end |
#execution_strategy_for_operation(operation) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/graphql/schema.rb', line 254 def execution_strategy_for_operation(operation) case operation when "query" query_execution_strategy when "mutation" mutation_execution_strategy when "subscription" subscription_execution_strategy else raise ArgumentError, "unknown operation type: #{operation}" end end |
#get_field(parent_type, field_name) ⇒ GraphQL::Field?
Resolve field named field_name
for type parent_type
.
Handles dynamic fields __typename
, __type
and __schema
, too
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/graphql/schema.rb', line 211 def get_field(parent_type, field_name) defined_field = @instrumented_field_map.get(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 |
#id_from_object(object, type, ctx) ⇒ String
Get a unique identifier from this object
359 360 361 362 363 364 365 |
# File 'lib/graphql/schema.rb', line 359 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
368 369 370 |
# File 'lib/graphql/schema.rb', line 368 def id_from_object=(new_proc) @id_from_object_proc = new_proc end |
#initialize_copy(other) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/graphql/schema.rb', line 119 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 @possible_types = GraphQL::Schema::PossibleTypes.new(self) @lazy_methods = GraphQL::Execution::Lazy::LazyMethodMap.new other.lazy_methods.each { |lazy_class, lazy_method| @lazy_methods.set(lazy_class, lazy_method) } @instrumenters = Hash.new { |h, k| h[k] = [] } other.instrumenters.each do |key, insts| @instrumenters[key].concat(insts) end if other.rescues? @rescue_middleware = other.rescue_middleware end # This will be rebuilt when it's requested # or during a later `define` call @types = nil end |
#instrument(instrumentation_type, instrumenter) ⇒ void
This method returns an undefined value.
Attach instrumenter
to this schema for instrumenting events of instrumentation_type
.
186 187 188 189 190 191 |
# File 'lib/graphql/schema.rb', line 186 def instrument(instrumentation_type, instrumenter) @instrumenters[instrumentation_type] << instrumenter if instrumentation_type == :field build_instrumented_field_map end end |
#lazy?(obj) ⇒ Boolean
Returns True if this object should be lazily resolved.
396 397 398 |
# File 'lib/graphql/schema.rb', line 396 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.
391 392 393 |
# File 'lib/graphql/schema.rb', line 391 def lazy_method_name(obj) @lazy_methods.get(obj) end |
#object_from_id(id, ctx) ⇒ Any
Fetch an application object by its unique id
297 298 299 300 301 302 303 |
# File 'lib/graphql/schema.rb', line 297 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
306 307 308 |
# File 'lib/graphql/schema.rb', line 306 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
345 346 347 |
# File 'lib/graphql/schema.rb', line 345 def parse_error(err, ctx) @parse_error_proc.call(err, ctx) end |
#parse_error=(new_proc) ⇒ Object
350 351 352 |
# File 'lib/graphql/schema.rb', line 350 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.
233 234 235 236 |
# File 'lib/graphql/schema.rb', line 233 def possible_types(type_defn) @possible_types ||= GraphQL::Schema::PossibleTypes.new(self) @possible_types.possible_types(type_defn) end |
#remove_handler(*args, &block) ⇒ Object
150 151 152 |
# File 'lib/graphql/schema.rb', line 150 def remove_handler(*args, &block) rescue_middleware.remove_handler(*args, &block) end |
#rescue_from(*args, &block) ⇒ Object
146 147 148 |
# File 'lib/graphql/schema.rb', line 146 def rescue_from(*args, &block) rescue_middleware.rescue_from(*args, &block) end |
#resolve_type(object, ctx) ⇒ GraphQL::ObjectType
Determine the GraphQL type for a given object.
This is required for unions and interfaces (including Relay's Node
interface)
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/graphql/schema.rb', line 273 def resolve_type(object, ctx) if @resolve_type_proc.nil? raise(NotImplementedError, "Can't determine GraphQL type for: #{object.inspect}, define `resolve_type (obj, ctx) -> { ... }` inside `Schema.define`.") end type_result = @resolve_type_proc.call(object, ctx) if type_result.nil? nil elsif !type_result.is_a?(GraphQL::BaseType) type_str = "#{type_result} (#{type_result.class.name})" raise "resolve_type(#{object}) returned #{type_str}, but it should return a GraphQL type" else type_result end end |
#resolve_type=(new_resolve_type_proc) ⇒ Object
289 290 291 |
# File 'lib/graphql/schema.rb', line 289 def resolve_type=(new_resolve_type_proc) @resolve_type_proc = new_resolve_type_proc end |
#root_type_for_operation(operation) ⇒ GraphQL::ObjectType?
241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/graphql/schema.rb', line 241 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 |
#to_definition(only: nil, except: nil, context: {}) ⇒ Object
Return a GraphQL schema string for the defined types in the schema
404 405 406 |
# File 'lib/graphql/schema.rb', line 404 def to_definition(only: nil, except: nil, context: {}) GraphQL::Schema::Printer.print_schema(self, only: only, except: except, context: context) 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.
330 331 332 |
# File 'lib/graphql/schema.rb', line 330 def type_error(err, ctx) @type_error_proc.call(err, ctx) end |
#type_error=(new_proc) ⇒ Object
335 336 337 |
# File 'lib/graphql/schema.rb', line 335 def type_error=(new_proc) @type_error_proc = new_proc end |
#type_from_ast(ast_node) ⇒ Object
226 227 228 |
# File 'lib/graphql/schema.rb', line 226 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.
195 196 197 |
# File 'lib/graphql/schema.rb', line 195 def types @types ||= build_types_map end |
#validate(string_or_document, rules: nil) ⇒ Array<GraphQL::StaticValidation::Message>
Validate a query string according to this schema.
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/graphql/schema.rb', line 157 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 |