Class: GraphQL::Execution::Batching::FieldResolveStep
- Inherits:
-
Object
- Object
- GraphQL::Execution::Batching::FieldResolveStep
- Defined in:
- lib/graphql/execution/batching/field_resolve_step.rb
Direct Known Subclasses
Defined Under Namespace
Modules: AlwaysAuthorized
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#ast_node ⇒ Object
readonly
Returns the value of attribute ast_node.
-
#field_definition ⇒ Object
readonly
Returns the value of attribute field_definition.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#object_is_authorized ⇒ Object
readonly
Returns the value of attribute object_is_authorized.
-
#parent_type ⇒ Object
readonly
Returns the value of attribute parent_type.
-
#runner ⇒ Object
readonly
Returns the value of attribute runner.
-
#selections_step ⇒ Object
readonly
Returns the value of attribute selections_step.
Instance Method Summary collapse
-
#add_graphql_error(err) ⇒ Object
-
#add_non_null_error(is_from_array) ⇒ Object
-
#append_selection(ast_node) ⇒ Object
-
#ast_nodes ⇒ Object
-
#authorized_finished ⇒ Object
-
#build_results ⇒ Object
-
#call ⇒ Object
-
#coerce_argument_value(arg_t, arg_value) ⇒ Object
-
#coerce_arguments(argument_owner, ast_arguments_or_hash) ⇒ Object
-
#enqueue_next_steps ⇒ Object
-
#execute_field ⇒ Object
-
#initialize(parent_type:, runner:, key:, selections_step:) ⇒ FieldResolveStep
constructor
A new instance of FieldResolveStep.
-
#path ⇒ Object
-
#sync(lazy) ⇒ Object
-
#value ⇒ Object
Implement that Lazy API.
Constructor Details
#initialize(parent_type:, runner:, key:, selections_step:) ⇒ FieldResolveStep
Returns a new instance of FieldResolveStep.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 6 def initialize(parent_type:, runner:, key:, selections_step:) @selections_step = selections_step @key = key @parent_type = parent_type @ast_node = @ast_nodes = nil @runner = runner @field_definition = nil @arguments = nil @field_results = nil @path = nil @enqueued_authorization = false @pending_authorize_steps_count = 0 @all_next_objects = nil @all_next_results = nil @static_type = nil @next_selections = nil @object_is_authorized = nil end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
25 26 27 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 25 def arguments @arguments end |
#ast_node ⇒ Object (readonly)
Returns the value of attribute ast_node.
25 26 27 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 25 def ast_node @ast_node end |
#field_definition ⇒ Object (readonly)
Returns the value of attribute field_definition.
25 26 27 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 25 def field_definition @field_definition end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
25 26 27 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 25 def key @key end |
#object_is_authorized ⇒ Object (readonly)
Returns the value of attribute object_is_authorized.
25 26 27 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 25 def @object_is_authorized end |
#parent_type ⇒ Object (readonly)
Returns the value of attribute parent_type.
25 26 27 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 25 def parent_type @parent_type end |
#runner ⇒ Object (readonly)
Returns the value of attribute runner.
25 26 27 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 25 def runner @runner end |
#selections_step ⇒ Object (readonly)
Returns the value of attribute selections_step.
25 26 27 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 25 def selections_step @selections_step end |
Instance Method Details
#add_graphql_error(err) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 154 def add_graphql_error(err) err.path = path err.ast_nodes = ast_nodes @selections_step.query.context.add_error(err) err end |
#add_non_null_error(is_from_array) ⇒ Object
346 347 348 349 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 346 def add_non_null_error(is_from_array) err = InvalidNullError.new(@parent_type, @field_definition, ast_nodes, is_from_array: is_from_array, path: path) @runner.schema.type_error(err, @selections_step.query.context) end |
#append_selection(ast_node) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 35 def append_selection(ast_node) if @ast_node.nil? @ast_node = ast_node elsif @ast_nodes.nil? @ast_nodes = [@ast_node, ast_node] else @ast_nodes << ast_node end nil end |
#ast_nodes ⇒ Object
31 32 33 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 31 def ast_nodes @ast_nodes ||= [@ast_node] end |
#authorized_finished ⇒ Object
339 340 341 342 343 344 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 339 def remaining = @pending_authorize_steps_count -= 1 if @enqueued_authorization && remaining == 0 @runner.add_step(self) end end |
#build_results ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 228 def build_results return_type = @field_definition.type return_result_type = return_type.unwrap if return_result_type.kind.composite? @static_type = return_result_type if @ast_nodes @next_selections = [] @ast_nodes.each do |ast_node| @next_selections.concat(ast_node.selections) end else @next_selections = @ast_node.selections end @all_next_objects = [] @all_next_results = [] is_list = return_type.list? is_non_null = return_type.non_null? results = @selections_step.results field_result_idx = 0 results.each_with_index do |result_h, i| if @object_is_authorized[i] result = @field_results[field_result_idx] field_result_idx += 1 else result = nil end build_graphql_result(result_h, @key, result, return_type, is_non_null, is_list, false) end @enqueued_authorization = true if @pending_authorize_steps_count == 0 enqueue_next_steps else # Do nothing -- it will enqueue itself later end else results = @selections_step.results ctx = @selections_step.query.context field_result_idx = 0 results.each_with_index do |result_h, i| if @object_is_authorized[i] field_result = @field_results[field_result_idx] field_result_idx += 1 else field_result = nil end result_h[@key] = if field_result.nil? if return_type.non_null? add_non_null_error(false) else nil end elsif field_result.is_a?(GraphQL::Error) add_graphql_error(field_result) else # TODO `nil`s in [T!] types aren't handled return_type.coerce_result(field_result, ctx) end end end end |
#call ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 144 def call if @enqueued_authorization && @pending_authorize_steps_count == 0 enqueue_next_steps elsif @field_results build_results else execute_field end end |
#coerce_argument_value(arg_t, arg_value) ⇒ Object
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 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 79 def coerce_argument_value(arg_t, arg_value) if arg_t.non_null? arg_t = arg_t.of_type end if arg_value.is_a?(Language::Nodes::VariableIdentifier) vars = @selections_step.query.variables arg_value = if vars.key?(arg_value.name) vars[arg_value.name] elsif vars.key?(arg_value.name.to_sym) vars[arg_value.name.to_sym] end elsif arg_value.is_a?(Language::Nodes::NullValue) arg_value = nil elsif arg_value.is_a?(Language::Nodes::Enum) arg_value = arg_value.name elsif arg_value.is_a?(Language::Nodes::InputObject) arg_value = arg_value.arguments # rubocop:disable Development/ContextIsPassedCop end if arg_t.list? if arg_value.nil? arg_value else arg_value = Array(arg_value) inner_t = arg_t.of_type arg_value.map { |v| coerce_argument_value(inner_t, v) } end elsif arg_t.kind.leaf? begin ctx = @selections_step.query.context arg_t.coerce_input(arg_value, ctx) rescue GraphQL::UnauthorizedEnumValueError => enum_err begin @runner.schema.(enum_err) rescue GraphQL::ExecutionError => ex_err ex_err end end elsif arg_t.kind.input_object? coerce_arguments(arg_t, arg_value) else raise "Unsupported argument value: #{arg_t.to_type_signature} / #{arg_value.class} (#{arg_value.inspect})" end end |
#coerce_arguments(argument_owner, ast_arguments_or_hash) ⇒ Object
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 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 46 def coerce_arguments(argument_owner, ast_arguments_or_hash) arg_defns = argument_owner.arguments(@selections_step.query.context) if arg_defns.empty? return EmptyObjects::EMPTY_HASH end args_hash = {} if ast_arguments_or_hash.is_a?(Hash) ast_arguments_or_hash.each do |key, value| key_s = nil arg_defn = arg_defns.each_value.find { |a| a.keyword == key || a.graphql_name == (key_s ||= String(key)) } arg_value = coerce_argument_value(arg_defn.type, value) args_hash[arg_defn.keyword] = arg_value end else ast_arguments_or_hash.each { |arg_node| arg_defn = arg_defns[arg_node.name] arg_value = coerce_argument_value(arg_defn.type, arg_node.value) arg_key = arg_defn.keyword args_hash[arg_key] = arg_value } end arg_defns.each do |arg_graphql_name, arg_defn| if arg_defn.default_value? && !args_hash.key?(arg_defn.keyword) args_hash[arg_defn.keyword] = arg_defn.default_value end end args_hash end |
#enqueue_next_steps ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 293 def enqueue_next_steps if !@all_next_results.empty? @all_next_objects.compact! if @static_type.kind.abstract? next_objects_by_type = Hash.new { |h, obj_t| h[obj_t] = [] }.compare_by_identity next_results_by_type = Hash.new { |h, obj_t| h[obj_t] = [] }.compare_by_identity ctx = nil @all_next_objects.each_with_index do |next_object, i| result = @all_next_results[i] if (object_type = @runner.runtime_types_at_result[result]) # OK else ctx ||= @selections_step.query.context object_type, _unused_new_value = @runner.schema.resolve_type(@static_type, next_object, ctx) end next_objects_by_type[object_type] << next_object next_results_by_type[object_type] << result end next_objects_by_type.each do |obj_type, next_objects| @runner.add_step(SelectionsStep.new( path: path, parent_type: obj_type, selections: @next_selections, objects: next_objects, results: next_results_by_type[obj_type], runner: @runner, query: @selections_step.query, )) end else @runner.add_step(SelectionsStep.new( path: path, parent_type: @static_type, selections: @next_selections, objects: @all_next_objects, results: @all_next_results, runner: @runner, query: @selections_step.query, )) end end end |
#execute_field ⇒ Object
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 193 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/batching/field_resolve_step.rb', line 167 def execute_field field_name = @ast_node.name @field_definition = @selections_step.query.get_field(@parent_type, field_name) || raise("Invariant: no field found for #{@parent_type.to_type_signature}.#{ast_node.name}") objects = @selections_step.objects if field_name == "__typename" # TODO handle custom introspection @field_results = Array.new(objects.size, @parent_type.graphql_name) @object_is_authorized = AlwaysAuthorized build_results return end @arguments = coerce_arguments(@field_definition, @ast_node.arguments) # rubocop:disable Development/ContextIsPassedCop ctx = @selections_step.query.context if (@runner..fetch(@field_definition) { @runner.[@field_definition] = @field_definition.(ctx) }) = [] @object_is_authorized = objects.map { |o| is_authed = @field_definition.(o, @arguments, ctx) if is_authed << o end is_authed } else = objects @object_is_authorized = AlwaysAuthorized end @field_results = @field_definition.resolve_batch(self, , ctx, @arguments) if @runner.resolves_lazies # TODO extract this lazies = false @field_results.each do |field_result| if @runner.schema.lazy?(field_result) lazies = true break elsif field_result.is_a?(Array) field_result.each do |inner_fr| if @runner.schema.lazy?(inner_fr) break lazies = true end end if lazies break end end end if lazies @runner.dataloader.lazy_at_depth(path.size, self) else build_results end else build_results end end |
#path ⇒ Object
27 28 29 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 27 def path @path ||= [*@selections_step.path, @key].freeze end |
#sync(lazy) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 132 def sync(lazy) if lazy.is_a?(Array) lazy.map! { |l| sync(l)} else @runner.schema.sync_lazy(lazy) end rescue GraphQL::UnauthorizedError => auth_err @runner.schema.(auth_err) rescue GraphQL::ExecutionError => err err end |
#value ⇒ Object
Implement that Lazy API
126 127 128 129 130 |
# File 'lib/graphql/execution/batching/field_resolve_step.rb', line 126 def value @field_results = sync(@field_results) @runner.add_step(self) true end |