Module: Rails::GraphQL::Source::ScopedArguments

Included in:
ActiveRecordSource
Defined in:
lib/rails/graphql/source/scoped_arguments.rb

Overview

This is a helper class that allows sources to have scoped-based arguments, meaning that when an argument is present, it triggers the underlying block on the fields where the argument was attached to

Defined Under Namespace

Modules: ClassMethods Classes: Argument

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object



9
10
11
# File 'lib/rails/graphql/source/scoped_arguments.rb', line 9

def self.included(other)
  other.extend(ClassMethods)
end

Instance Method Details

#inject_scopes(object, assigned_to = nil) ⇒ Object

Find all the executable arguments attached to the running field and call them with the given object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rails/graphql/source/scoped_arguments.rb', line 85

def inject_scopes(object, assigned_to = nil)
  return object if event.field.nil? || (field_args = event.field.all_arguments).blank?

  args_source = event.send(:args_source)
  event.data[assigned_to] ||= object unless assigned_to.nil?
  field_args.each_value.inject(object) do |result, argument|
    arg_value = args_source.key?(argument.name) \
      ? args_source[argument.name] \
      : argument.default

    next result if arg_value.nil? || !argument.is_a?(Argument)

    value = argument.apply_to(result, arg_value)
    value = value.nil? ? result : value

    assigned_to.nil? ? value : event.data[assigned_to] = value
  end
end