Method: GraphQL::Schema::Argument#load_and_authorize_value

Defined in:
lib/graphql/schema/argument.rb

#load_and_authorize_value(load_method_owner, coerced_value, context) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/graphql/schema/argument.rb', line 323

def load_and_authorize_value(load_method_owner, coerced_value, context)
  if coerced_value.nil?
    return nil
  end
  arg_load_method = "load_#{keyword}"
  if load_method_owner.respond_to?(arg_load_method)
    custom_loaded_value = if load_method_owner.is_a?(Class)
      load_method_owner.public_send(arg_load_method, coerced_value, context)
    else
      load_method_owner.public_send(arg_load_method, coerced_value)
    end
    context.query.after_lazy(custom_loaded_value) do |custom_value|
      if loads
        if type.list?
          loaded_values = []
          context.dataloader.run_isolated do
            custom_value.each_with_index.map { |custom_val, idx|
              id = coerced_value[idx]
              context.dataloader.append_job do
                loaded_values[idx] = load_method_owner.authorize_application_object(self, id, context, custom_val)
              end
            }
          end
          context.schema.after_any_lazies(loaded_values, &:itself)
        else
          load_method_owner.authorize_application_object(self, coerced_value, context, custom_loaded_value)
        end
      else
        custom_value
      end
    end
  elsif loads
    if type.list?
      loaded_values = []
      # We want to run these list items all together,
      # but we also need to wait for the result so we can return it :S
      context.dataloader.run_isolated do
        coerced_value.each_with_index { |val, idx|
          context.dataloader.append_job do
            loaded_values[idx] = load_method_owner.load_and_authorize_application_object(self, val, context)
          end
        }
      end
      context.schema.after_any_lazies(loaded_values, &:itself)
    else
      load_method_owner.load_and_authorize_application_object(self, coerced_value, context)
    end
  else
    coerced_value
  end
end