Class: GraphQL::Execution::Next::Runner
- Inherits:
-
Object
- Object
- GraphQL::Execution::Next::Runner
- Defined in:
- lib/graphql/execution/next/runner.rb
Instance Attribute Summary collapse
-
#authorization ⇒ Object
readonly
Returns the value of attribute authorization.
-
#authorizes ⇒ Object
readonly
Returns the value of attribute authorizes.
-
#dataloader ⇒ Object
readonly
Returns the value of attribute dataloader.
-
#resolves_lazies ⇒ Object
readonly
Returns the value of attribute resolves_lazies.
-
#runtime_type_at ⇒ Object
readonly
Returns the value of attribute runtime_type_at.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#static_type_at ⇒ Object
readonly
Returns the value of attribute static_type_at.
-
#steps_queue ⇒ Object
readonly
Returns the value of attribute steps_queue.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
-
#add_step(step) ⇒ Object
-
#authorizes?(graphql_definition, query_context) ⇒ Boolean
-
#execute ⇒ Object
-
#gather_selections(type_defn, ast_selections, selections_step, query, prototype_result, into:) ⇒ Object
-
#initialize(multiplex, authorization:) ⇒ Runner
constructor
A new instance of Runner.
-
#lazy?(object) ⇒ Boolean
-
#resolve_type(type, object, query) ⇒ Object
Constructor Details
#initialize(multiplex, authorization:) ⇒ Runner
Returns a new instance of Runner.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/graphql/execution/next/runner.rb', line 6 def initialize(multiplex, authorization:) @multiplex = multiplex @schema = multiplex.schema @steps_queue = [] @runtime_type_at = {}.compare_by_identity @static_type_at = {}.compare_by_identity @selected_operation = nil @dataloader = multiplex.context[:dataloader] ||= @schema.dataloader_class.new @resolves_lazies = @schema.resolves_lazies? @lazy_cache = resolves_lazies ? {}.compare_by_identity : nil @field_resolve_step_class = @schema.uses_raw_value? ? RawValueFieldResolveStep : FieldResolveStep @authorization = if @authorization @authorizes_cache = Hash.new do |h, query_context| h[query_context] = {}.compare_by_identity end.compare_by_identity end end |
Instance Attribute Details
#authorization ⇒ Object (readonly)
Returns the value of attribute authorization.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def @authorization end |
#authorizes ⇒ Object (readonly)
Returns the value of attribute authorizes.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def @authorizes end |
#dataloader ⇒ Object (readonly)
Returns the value of attribute dataloader.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def dataloader @dataloader end |
#resolves_lazies ⇒ Object (readonly)
Returns the value of attribute resolves_lazies.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def resolves_lazies @resolves_lazies end |
#runtime_type_at ⇒ Object (readonly)
Returns the value of attribute runtime_type_at.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def runtime_type_at @runtime_type_at end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def schema @schema end |
#static_type_at ⇒ Object (readonly)
Returns the value of attribute static_type_at.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def static_type_at @static_type_at end |
#steps_queue ⇒ Object (readonly)
Returns the value of attribute steps_queue.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def steps_queue @steps_queue end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
46 47 48 |
# File 'lib/graphql/execution/next/runner.rb', line 46 def variables @variables end |
Instance Method Details
#add_step(step) ⇒ Object
42 43 44 |
# File 'lib/graphql/execution/next/runner.rb', line 42 def add_step(step) @dataloader.append_job(step) end |
#authorizes?(graphql_definition, query_context) ⇒ Boolean
32 33 34 35 36 37 38 39 40 |
# File 'lib/graphql/execution/next/runner.rb', line 32 def (graphql_definition, query_context) auth_cache = @authorizes_cache[query_context] case (auth_res = auth_cache[graphql_definition]) when nil auth_cache[graphql_definition] = graphql_definition.(query_context) else auth_res end end |
#execute ⇒ Object
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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 141 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/graphql/execution/next/runner.rb', line 48 def execute Fiber[:__graphql_current_multiplex] = @multiplex isolated_steps = [[]] trace = @multiplex.current_trace queries = @multiplex.queries multiplex_analyzers = @schema.multiplex_analyzers if @multiplex.max_complexity multiplex_analyzers += [GraphQL::Analysis::MaxQueryComplexity] end trace.execute_multiplex(multiplex: @multiplex) do trace.begin_analyze_multiplex(@multiplex, multiplex_analyzers) @schema.analysis_engine.analyze_multiplex(@multiplex, multiplex_analyzers) trace.end_analyze_multiplex(@multiplex, multiplex_analyzers) results = [] queries.each do |query| if query.validate && !query.valid? results << { "errors" => query.static_errors.map(&:to_h) } next end selected_operation = query.document.definitions.first # TODO select named operation data = {} root_type = case selected_operation.operation_type when nil, "query" @schema.query when "mutation" @schema.mutation when "subscription" @schema.subscription else raise ArgumentError, "Unknown operation type: #{selected_operation.operation_type.inspect}" end if self. && (root_type, query.context) query.current_trace.(root_type, query.root_value, query.context) auth_check = schema.sync_lazy(root_type.(query.root_value, query.context)) query.current_trace.(root_type, query.root_value, query.context, auth_check) root_value = if auth_check query.root_value else begin auth_err = GraphQL::UnauthorizedError.new(object: query.root_value, type: root_type, context: query.context) new_val = schema.(auth_err) if new_val auth_check = true end new_val rescue GraphQL::ExecutionError => ex_err # The old runtime didn't add path and ast_nodes to this query.context.add_error(ex_err) nil end end if !auth_check results << {} next end else root_value = query.root_value end results << { "data" => data } case selected_operation.operation_type when nil, "query" isolated_steps[0] << SelectionsStep.new( parent_type: root_type, selections: selected_operation.selections, objects: [root_value], results: [data], path: EmptyObjects::EMPTY_ARRAY, runner: self, query: query, ) when "mutation" fields = {} gather_selections(root_type, selected_operation.selections, nil, query, {}, into: fields) fields.each_value do |field_resolve_step| isolated_steps << [SelectionsStep.new( parent_type: root_type, selections: field_resolve_step.ast_nodes || Array(field_resolve_step.ast_node), objects: [root_value], results: [data], path: EmptyObjects::EMPTY_ARRAY, runner: self, query: query, )] end when "subscription" raise ArgumentError, "TODO implement subscriptions" else raise ArgumentError, "Unhandled operation type: #{operation.operation_type.inspect}" end @static_type_at[data] = root_type # TODO This is stupid but makes multiplex_spec.rb pass trace.execute_query(query: query) do end end while (next_isolated_steps = isolated_steps.shift) next_isolated_steps.each do |step| add_step(step) end @dataloader.run end # TODO This is stupid but makes multiplex_spec.rb pass trace.execute_query_lazy(query: nil, multiplex: @multiplex) do end queries.each_with_index.map do |query, idx| result = results[idx] fin_result = if query.context.errors.empty? result else data = result["data"] data = propagate_errors(data, query) errors = [] query.context.errors.each do |err| if err.respond_to?(:to_h) errors << err.to_h end end res_h = {} if !errors.empty? res_h["errors"] = errors end res_h["data"] = data res_h end GraphQL::Query::Result.new(query: query, values: fin_result) end end ensure Fiber[:__graphql_current_multiplex] = nil end |
#gather_selections(type_defn, ast_selections, selections_step, query, prototype_result, into:) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/graphql/execution/next/runner.rb', line 194 def gather_selections(type_defn, ast_selections, selections_step, query, prototype_result, into:) ast_selections.each do |ast_selection| next if !directives_include?(query, ast_selection) case ast_selection when GraphQL::Language::Nodes::Field key = ast_selection.alias || ast_selection.name step = into[key] ||= begin prototype_result[key] = nil @field_resolve_step_class.new( selections_step: selections_step, key: key, parent_type: type_defn, runner: self, ) end step.append_selection(ast_selection) when GraphQL::Language::Nodes::InlineFragment type_condition = ast_selection.type&.name if type_condition.nil? || type_condition_applies?(query.context, type_defn, type_condition) gather_selections(type_defn, ast_selection.selections, selections_step, query, prototype_result, into: into) end when GraphQL::Language::Nodes::FragmentSpread fragment_definition = query.document.definitions.find { |defn| defn.is_a?(GraphQL::Language::Nodes::FragmentDefinition) && defn.name == ast_selection.name } type_condition = fragment_definition.type.name if type_condition_applies?(query.context, type_defn, type_condition) gather_selections(type_defn, fragment_definition.selections, selections_step, query, prototype_result, into: into) end else raise ArgumentError, "Unsupported graphql selection node: #{ast_selection.class} (#{ast_selection.inspect})" end end end |
#lazy?(object) ⇒ Boolean
228 229 230 231 232 233 234 235 |
# File 'lib/graphql/execution/next/runner.rb', line 228 def lazy?(object) obj_class = object.class is_lazy = @lazy_cache[obj_class] if is_lazy.nil? is_lazy = @lazy_cache[obj_class] = @schema.lazy?(object) end is_lazy end |
#resolve_type(type, object, query) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/graphql/execution/next/runner.rb', line 25 def resolve_type(type, object, query) query.current_trace.begin_resolve_type(@static_type, object, query.context) resolved_type, _ignored_new_value = query.resolve_type(type, object) query.current_trace.end_resolve_type(@static_type, object, query.context, resolved_type) resolved_type end |