Class: GraphQL::Schema
- Inherits:
-
Object
- Object
- GraphQL::Schema
- Extended by:
- Forwardable
- Defined in:
- lib/graphql/schema.rb,
lib/graphql/schema/type_map.rb,
lib/graphql/schema/type_expression.rb,
lib/graphql/schema/middleware_chain.rb,
lib/graphql/schema/rescue_middleware.rb
Overview
A GraphQL schema which may be queried with Query.
Defined Under Namespace
Modules: Printer Classes: EachItemValidator, FieldValidator, ImplementationValidator, InvalidTypeError, MiddlewareChain, RescueMiddleware, TypeExpression, TypeMap, TypeReducer, TypeValidator
Constant Summary collapse
- DIRECTIVES =
[GraphQL::Directive::SkipDirective, GraphQL::Directive::IncludeDirective]
- DYNAMIC_FIELDS =
["__type", "__typename", "__schema"]
Instance Attribute Summary collapse
-
#directives ⇒ Object
readonly
Returns the value of attribute directives.
-
#middleware ⇒ Array<#call>
readonly
Middlewares suitable for MiddlewareChain, applied to fields during execution.
-
#mutation ⇒ Object
readonly
Returns the value of attribute mutation.
-
#mutation_execution_strategy ⇒ Object
Override these if you don’t want the default executor:.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#query_execution_strategy ⇒ Object
Override these if you don’t want the default executor:.
-
#static_validator ⇒ Object
readonly
Returns the value of attribute static_validator.
Instance Method Summary collapse
-
#execute(*args) ⇒ Hash
Execute a query on itself.
-
#get_field(parent_type, field_name) ⇒ Object
Resolve field named ‘field_name` for type `parent_type`.
-
#initialize(query:, mutation: nil) ⇒ Schema
constructor
A new instance of Schema.
- #type_from_ast(ast_node) ⇒ Object
-
#types ⇒ GraphQL::Schema::TypeMap
‘{ name => type }` pairs of types in this schema.
Constructor Details
#initialize(query:, mutation: nil) ⇒ Schema
Returns a new instance of Schema.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/graphql/schema.rb', line 17 def initialize(query:, mutation: nil) @query = query @mutation = mutation @directives = DIRECTIVES.reduce({}) { |m, d| m[d.name] = d; m } @static_validator = GraphQL::StaticValidation::Validator.new(schema: self) @rescue_middleware = GraphQL::Schema::RescueMiddleware.new @middleware = [@rescue_middleware] # Default to the built-in execution strategy: self.query_execution_strategy = GraphQL::Query::SerialExecution self.mutation_execution_strategy = GraphQL::Query::SerialExecution end |
Instance Attribute Details
#directives ⇒ Object (readonly)
Returns the value of attribute directives.
8 9 10 |
# File 'lib/graphql/schema.rb', line 8 def directives @directives end |
#middleware ⇒ Array<#call> (readonly)
Returns Middlewares suitable for MiddlewareChain, applied to fields during execution.
13 14 15 |
# File 'lib/graphql/schema.rb', line 13 def middleware @middleware end |
#mutation ⇒ Object (readonly)
Returns the value of attribute mutation.
8 9 10 |
# File 'lib/graphql/schema.rb', line 8 def mutation @mutation end |
#mutation_execution_strategy ⇒ Object
Override these if you don’t want the default executor:
10 11 12 |
# File 'lib/graphql/schema.rb', line 10 def mutation_execution_strategy @mutation_execution_strategy end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
8 9 10 |
# File 'lib/graphql/schema.rb', line 8 def query @query end |
#query_execution_strategy ⇒ Object
Override these if you don’t want the default executor:
10 11 12 |
# File 'lib/graphql/schema.rb', line 10 def query_execution_strategy @query_execution_strategy end |
#static_validator ⇒ Object (readonly)
Returns the value of attribute static_validator.
8 9 10 |
# File 'lib/graphql/schema.rb', line 8 def static_validator @static_validator end |
Instance Method Details
#execute(*args) ⇒ Hash
Execute a query on itself. See Query#initialize for arguments.
39 40 41 42 |
# File 'lib/graphql/schema.rb', line 39 def execute(*args) query = GraphQL::Query.new(self, *args) query.result end |
#get_field(parent_type, field_name) ⇒ Object
Resolve field named ‘field_name` for type `parent_type`. Handles dynamic fields `__typename`, `__type` and `__schema`, too
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/graphql/schema.rb', line 46 def get_field(parent_type, field_name) defined_field = parent_type.fields[field_name] if defined_field defined_field elsif field_name == "__typename" GraphQL::Introspection::TypenameField.create(parent_type) elsif field_name == "__schema" && parent_type == query GraphQL::Introspection::SchemaField.create(self) elsif field_name == "__type" && parent_type == query GraphQL::Introspection::TypeByNameField.create(self.types) else nil end end |
#type_from_ast(ast_node) ⇒ Object
61 62 63 |
# File 'lib/graphql/schema.rb', line 61 def type_from_ast(ast_node) GraphQL::Schema::TypeExpression.new(self, ast_node).type end |
#types ⇒ GraphQL::Schema::TypeMap
Returns ‘{ name => type }` pairs of types in this schema.
32 33 34 |
# File 'lib/graphql/schema.rb', line 32 def types @types ||= TypeReducer.find_all([query, mutation, GraphQL::Introspection::SchemaType].compact) end |