Class: GraphQL::Query::Partial
- Inherits:
-
Object
- Object
- GraphQL::Query::Partial
- Includes:
- Runnable
- Defined in:
- lib/graphql/query/partial.rb
Overview
This class is like a GraphQL::Query, except it can run on an arbitrary path within a query string.
It depends on a “parent” GraphQL::Query.
During execution, it calls query-related tracing hooks but passes itself as query:
.
The Partial will use your Schema.resolve_type hook to find the right GraphQL type to use for
object
in some cases.
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#ast_nodes ⇒ Object
readonly
Returns the value of attribute ast_nodes.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#field_definition ⇒ Object
readonly
Returns the value of attribute field_definition.
-
#multiplex ⇒ Object
Returns the value of attribute multiplex.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#parent_type ⇒ Object
readonly
Returns the value of attribute parent_type.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#result_values ⇒ Object
Returns the value of attribute result_values.
-
#root_type ⇒ Object
readonly
Returns the value of attribute root_type.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
-
#analysis_errors=(_ignored) ⇒ Object
-
#analyzers ⇒ Object
-
#current_trace ⇒ Object
-
#fragments ⇒ Object
-
#initialize(path:, object:, query:, context: nil) ⇒ Partial
constructor
A new instance of Partial.
-
#leaf? ⇒ Boolean
-
#resolve_type ⇒ Object
-
#result ⇒ Object
-
#selected_operation ⇒ Object
-
#static_errors ⇒ Object
-
#subscription? ⇒ Boolean
-
#types ⇒ Object
-
#valid? ⇒ Boolean
-
#variables ⇒ Object
Methods included from Runnable
#after_lazy, #arguments_cache, #arguments_for, #handle_or_reraise
Constructor Details
#initialize(path:, object:, query:, context: nil) ⇒ Partial
Returns a new instance of Partial.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/graphql/query/partial.rb', line 21 def initialize(path:, object:, query:, context: nil) @path = path @object = object @query = query @schema = query.schema context_vals = @query.context.to_h if context context_vals = context_vals.merge(context) end @context = GraphQL::Query::Context.new(query: self, schema: @query.schema, values: context_vals) @multiplex = nil @result_values = nil @result = nil selections = [@query.selected_operation] type = @query.root_type parent_type = nil field_defn = nil @path.each do |name_in_doc| if name_in_doc.is_a?(Integer) if type.list? type = type.unwrap next else raise ArgumentError, "Received path with index `#{name_in_doc}`, but type wasn't a list. Type: #{type.to_type_signature}, path: #{@path}" end end next_selections = [] selections.each do |selection| selections_to_check = [] selections_to_check.concat(selection.selections) while (sel = selections_to_check.shift) case sel when GraphQL::Language::Nodes::InlineFragment selections_to_check.concat(sel.selections) when GraphQL::Language::Nodes::FragmentSpread fragment = @query.fragments[sel.name] selections_to_check.concat(fragment.selections) when GraphQL::Language::Nodes::Field if sel.alias == name_in_doc || sel.name == name_in_doc next_selections << sel end else raise "Unexpected selection in partial path: #{sel.class}, #{sel.inspect}" end end end if next_selections.empty? raise ArgumentError, "Path `#{@path.inspect}` is not present in this query. `#{name_in_doc.inspect}` was not found. Try a different path or rewrite the query to include it." end field_name = next_selections.first.name field_defn = @schema.get_field(type, field_name, @query.context) || raise("Invariant: no field called #{field_name} on #{type.graphql_name}") parent_type = type type = field_defn.type if type.non_null? type = type.of_type end selections = next_selections end @parent_type = parent_type @ast_nodes = selections @root_type = type @field_definition = field_defn @leaf = @root_type.unwrap.kind.leaf? end |
Instance Attribute Details
#ast_nodes ⇒ Object (readonly)
Returns the value of attribute ast_nodes.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def ast_nodes @ast_nodes end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def context @context end |
#field_definition ⇒ Object (readonly)
Returns the value of attribute field_definition.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def field_definition @field_definition end |
#multiplex ⇒ Object
Returns the value of attribute multiplex.
94 95 96 |
# File 'lib/graphql/query/partial.rb', line 94 def multiplex @multiplex end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def object @object end |
#parent_type ⇒ Object (readonly)
Returns the value of attribute parent_type.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def parent_type @parent_type end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def path @path end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def query @query end |
#result_values ⇒ Object
Returns the value of attribute result_values.
94 95 96 |
# File 'lib/graphql/query/partial.rb', line 94 def result_values @result_values end |
#root_type ⇒ Object (readonly)
Returns the value of attribute root_type.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def root_type @root_type end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
92 93 94 |
# File 'lib/graphql/query/partial.rb', line 92 def schema @schema end |
Instance Method Details
#analysis_errors=(_ignored) ⇒ Object
139 140 141 |
# File 'lib/graphql/query/partial.rb', line 139 def analysis_errors=(_ignored) # pass end |
#analyzers ⇒ Object
135 136 137 |
# File 'lib/graphql/query/partial.rb', line 135 def analyzers EmptyObjects::EMPTY_ARRAY end |
#current_trace ⇒ Object
111 112 113 |
# File 'lib/graphql/query/partial.rb', line 111 def current_trace @query.current_trace end |
#fragments ⇒ Object
127 128 129 |
# File 'lib/graphql/query/partial.rb', line 127 def fragments @query.fragments end |
#leaf? ⇒ Boolean
88 89 90 |
# File 'lib/graphql/query/partial.rb', line 88 def leaf? @leaf end |
#resolve_type ⇒ Object
119 120 121 |
# File 'lib/graphql/query/partial.rb', line 119 def resolve_type(...) @query.resolve_type(...) end |
#result ⇒ Object
107 108 109 |
# File 'lib/graphql/query/partial.rb', line 107 def result @result ||= Result.new(query: self, values: result_values) end |
#selected_operation ⇒ Object
147 148 149 |
# File 'lib/graphql/query/partial.rb', line 147 def selected_operation ast_nodes.first end |
#static_errors ⇒ Object
151 152 153 |
# File 'lib/graphql/query/partial.rb', line 151 def static_errors @query.static_errors end |
#subscription? ⇒ Boolean
143 144 145 |
# File 'lib/graphql/query/partial.rb', line 143 def subscription? @query.subscription? end |
#types ⇒ Object
115 116 117 |
# File 'lib/graphql/query/partial.rb', line 115 def types @query.types end |
#valid? ⇒ Boolean
131 132 133 |
# File 'lib/graphql/query/partial.rb', line 131 def valid? @query.valid? end |
#variables ⇒ Object
123 124 125 |
# File 'lib/graphql/query/partial.rb', line 123 def variables @query.variables end |