Class: GraphQL::Schema
- Inherits:
-
Object
- Object
- GraphQL::Schema
- Extended by:
- Forwardable
- Defined in:
- lib/graphql/schema.rb,
lib/graphql/schema/printer.rb,
lib/graphql/schema/type_map.rb,
lib/graphql/schema/validation.rb,
lib/graphql/schema/reduce_types.rb,
lib/graphql/schema/possible_types.rb,
lib/graphql/schema/type_expression.rb,
lib/graphql/schema/middleware_chain.rb,
lib/graphql/schema/rescue_middleware.rb,
lib/graphql/schema/invalid_type_error.rb,
lib/graphql/schema/catchall_middleware.rb
Overview
A GraphQL schema which may be queried with Query.
Defined Under Namespace
Modules: CatchallMiddleware, Printer, ReduceTypes, TypeExpression Classes: InvalidTypeError, MiddlewareChain, PossibleTypes, RescueMiddleware, TypeMap, Validation
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.
-
#max_depth ⇒ Object
Returns the value of attribute max_depth.
-
#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.
-
#subscription ⇒ Object
readonly
Returns the value of attribute subscription.
-
#subscription_execution_strategy ⇒ Object
Override these if you don’t want the default executor:.
Instance Method Summary collapse
-
#execute(*args) ⇒ Hash
Execute a query on itself.
- #execution_strategy_for_operation(operation) ⇒ Object
-
#get_field(parent_type, field_name) ⇒ Object
Resolve field named ‘field_name` for type `parent_type`.
-
#initialize(query:, mutation: nil, subscription: nil, max_depth: nil, types: []) ⇒ Schema
constructor
A new instance of Schema.
-
#possible_types(type_defn) ⇒ Array<GraphQL::ObjectType>
Types which belong to ‘type_defn` in this schema.
- #root_type_for_operation(operation) ⇒ Object
- #type_from_ast(ast_node) ⇒ Object
-
#types ⇒ GraphQL::Schema::TypeMap
‘{ name => type }` pairs of types in this schema.
Constructor Details
#initialize(query:, mutation: nil, subscription: nil, max_depth: nil, types: []) ⇒ Schema
Returns a new instance of Schema.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/graphql/schema.rb', line 34 def initialize(query:, mutation: nil, subscription: nil, max_depth: nil, types: []) @query = query @mutation = mutation @subscription = subscription @max_depth = max_depth @orphan_types = types @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 self.subscription_execution_strategy = GraphQL::Query::SerialExecution end |
Instance Attribute Details
#directives ⇒ Object (readonly)
Returns the value of attribute directives.
19 20 21 |
# File 'lib/graphql/schema.rb', line 19 def directives @directives end |
#max_depth ⇒ Object
Returns the value of attribute max_depth.
20 21 22 |
# File 'lib/graphql/schema.rb', line 20 def max_depth @max_depth end |
#middleware ⇒ Array<#call> (readonly)
Returns Middlewares suitable for MiddlewareChain, applied to fields during execution.
27 28 29 |
# File 'lib/graphql/schema.rb', line 27 def middleware @middleware end |
#mutation ⇒ Object (readonly)
Returns the value of attribute mutation.
19 20 21 |
# File 'lib/graphql/schema.rb', line 19 def mutation @mutation end |
#mutation_execution_strategy ⇒ Object
Override these if you don’t want the default executor:
22 23 24 |
# File 'lib/graphql/schema.rb', line 22 def mutation_execution_strategy @mutation_execution_strategy end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
19 20 21 |
# File 'lib/graphql/schema.rb', line 19 def query @query end |
#query_execution_strategy ⇒ Object
Override these if you don’t want the default executor:
22 23 24 |
# File 'lib/graphql/schema.rb', line 22 def query_execution_strategy @query_execution_strategy end |
#static_validator ⇒ Object (readonly)
Returns the value of attribute static_validator.
19 20 21 |
# File 'lib/graphql/schema.rb', line 19 def static_validator @static_validator end |
#subscription ⇒ Object (readonly)
Returns the value of attribute subscription.
19 20 21 |
# File 'lib/graphql/schema.rb', line 19 def subscription @subscription end |
#subscription_execution_strategy ⇒ Object
Override these if you don’t want the default executor:
22 23 24 |
# File 'lib/graphql/schema.rb', line 22 def subscription_execution_strategy @subscription_execution_strategy end |
Instance Method Details
#execute(*args) ⇒ Hash
Execute a query on itself. See Query#initialize for arguments.
63 64 65 66 |
# File 'lib/graphql/schema.rb', line 63 def execute(*args) query = GraphQL::Query.new(self, *args) query.result end |
#execution_strategy_for_operation(operation) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/graphql/schema.rb', line 109 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) ⇒ Object
Resolve field named ‘field_name` for type `parent_type`. Handles dynamic fields `__typename`, `__type` and `__schema`, too
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/graphql/schema.rb', line 70 def get_field(parent_type, field_name) defined_field = parent_type.get_field(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 |
#possible_types(type_defn) ⇒ Array<GraphQL::ObjectType>
Returns types which belong to ‘type_defn` in this schema.
91 92 93 94 |
# File 'lib/graphql/schema.rb', line 91 def possible_types(type_defn) @interface_possible_types ||= GraphQL::Schema::PossibleTypes.new(self) @interface_possible_types.possible_types(type_defn) end |
#root_type_for_operation(operation) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/graphql/schema.rb', line 96 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 |
#type_from_ast(ast_node) ⇒ Object
85 86 87 |
# File 'lib/graphql/schema.rb', line 85 def type_from_ast(ast_node) GraphQL::Schema::TypeExpression.build_type(self, ast_node) end |
#types ⇒ GraphQL::Schema::TypeMap
Returns ‘{ name => type }` pairs of types in this schema.
53 54 55 56 57 58 |
# File 'lib/graphql/schema.rb', line 53 def types @types ||= begin all_types = @orphan_types + [query, mutation, subscription, GraphQL::Introspection::SchemaType] GraphQL::Schema::ReduceTypes.reduce(all_types.compact) end end |