Module: GraphQL::Schema::Member::HasArguments Private

Included in:
Directive, Field, InputObject, Resolver
Defined in:
lib/graphql/schema/member/has_arguments.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Modules: ArgumentClassAccessor, ArgumentObjectLoader

Constant Summary collapse

NO_ARGUMENTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{}.freeze

Instance Method Summary collapse

Instance Method Details

#add_argument(arg_defn) ⇒ GraphQL::Schema::Argument

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register this argument with the class.

Parameters:

Returns:



79
80
81
82
83
# File 'lib/graphql/schema/member/has_arguments.rb', line 79

def add_argument(arg_defn)
  @own_arguments ||= {}
  own_arguments[arg_defn.name] = arg_defn
  arg_defn
end

#argument(*args, **kwargs, &block) ⇒ GraphQL::Schema::Argument

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns An instance of arguments_class, created from *args.

Returns:

See Also:

  • for parameters


18
19
20
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
# File 'lib/graphql/schema/member/has_arguments.rb', line 18

def argument(*args, **kwargs, &block)
  kwargs[:owner] = self
  loads = kwargs[:loads]
  if loads
    name = args[0]
    name_as_string = name.to_s

    inferred_arg_name = case name_as_string
    when /_id$/
      name_as_string.sub(/_id$/, "").to_sym
    when /_ids$/
      name_as_string.sub(/_ids$/, "")
        .sub(/([^s])$/, "\\1s")
        .to_sym
    else
      name
    end

    kwargs[:as] ||= inferred_arg_name
  end
  arg_defn = self.argument_class.new(*args, **kwargs, &block)
  add_argument(arg_defn)

  if self.is_a?(Class) && !method_defined?(:"load_#{arg_defn.keyword}")
    method_owner = if self < GraphQL::Schema::InputObject || self < GraphQL::Schema::Directive
      "self."
    elsif self < GraphQL::Schema::Resolver
      ""
    else
      raise "Unexpected argument owner: #{self}"
    end
    if loads && arg_defn.type.list?
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{method_owner}load_#{arg_defn.keyword}(values, context = nil)
        argument = get_argument("#{arg_defn.graphql_name}")
        (context || self.context).schema.after_lazy(values) do |values2|
          GraphQL::Execution::Lazy.all(values2.map { |value| load_application_object(argument, value, context || self.context) })
        end
      end
      RUBY
    elsif loads
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{method_owner}load_#{arg_defn.keyword}(value, context = nil)
        argument = get_argument("#{arg_defn.graphql_name}")
        load_application_object(argument, value, context || self.context)
      end
      RUBY
    else
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{method_owner}load_#{arg_defn.keyword}(value, _context = nil)
        value
      end
      RUBY
    end
  end
  arg_defn
end

#argument_class(new_arg_class = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • new_arg_class (Class) (defaults to: nil)

    A class to use for building argument definitions



113
114
115
# File 'lib/graphql/schema/member/has_arguments.rb', line 113

def argument_class(new_arg_class = nil)
  self.class.argument_class(new_arg_class)
end

#argumentsHash<String => GraphQL::Schema::Argument] Arguments defined on this thing, keyed by name. Includes inherited definitions

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Hash<String => GraphQL::Schema::Argument] Arguments defined on this thing, keyed by name. Includes inherited definitions.

Returns:

  • (Hash<String => GraphQL::Schema::Argument] Arguments defined on this thing, keyed by name. Includes inherited definitions)

    Hash<String => GraphQL::Schema::Argument] Arguments defined on this thing, keyed by name. Includes inherited definitions



86
87
88
89
90
91
92
93
94
# File 'lib/graphql/schema/member/has_arguments.rb', line 86

def arguments
  inherited_arguments = ((self.is_a?(Class) && superclass.respond_to?(:arguments)) ? superclass.arguments : nil)
  # Local definitions override inherited ones
  if inherited_arguments
    inherited_arguments.merge(own_arguments)
  else
    own_arguments
  end
end

#arguments_statically_coercible?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



191
192
193
194
195
# File 'lib/graphql/schema/member/has_arguments.rb', line 191

def arguments_statically_coercible?
  return @arguments_statically_coercible if defined?(@arguments_statically_coercible)

  @arguments_statically_coercible = arguments.each_value.all?(&:statically_coercible?)
end

#coerce_arguments(parent_object, values, context) {|Interpreter::Arguments, Execution::Lazy<Interpeter::Arguments>| ... } ⇒ Interpreter::Arguments, Execution::Lazy<Interpeter::Arguments>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If given a block, it will eventually yield the loaded args to the block.

If no block is given, it will immediately dataload (but might return a Lazy).

Parameters:

Yields:

Returns:



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
# File 'lib/graphql/schema/member/has_arguments.rb', line 126

def coerce_arguments(parent_object, values, context, &block)
  # Cache this hash to avoid re-merging it
  arg_defns = self.arguments
  total_args_count = arg_defns.size

  finished_args = nil
  prepare_finished_args = -> {
    if total_args_count == 0
      finished_args = GraphQL::Execution::Interpreter::Arguments::EMPTY
      if block_given?
        block.call(finished_args)
      end
    else
      argument_values = {}
      resolved_args_count = 0
      raised_error = false
      arg_defns.each do |arg_name, arg_defn|
        context.dataloader.append_job do
          begin
            arg_defn.coerce_into_values(parent_object, values, context, argument_values)
          rescue GraphQL::ExecutionError, GraphQL::UnauthorizedError => err
            raised_error = true
            finished_args = err
            if block_given?
              block.call(finished_args)
            end
          end

          resolved_args_count += 1
          if resolved_args_count == total_args_count && !raised_error
            finished_args = context.schema.after_any_lazies(argument_values.values) {
              GraphQL::Execution::Interpreter::Arguments.new(
                argument_values: argument_values,
              )
            }
            if block_given?
              block.call(finished_args)
            end
          end
        end
      end
    end
  }

  if block_given?
    prepare_finished_args.call
    nil
  else
    # This API returns eagerly, gotta run it now
    context.dataloader.run_isolated(&prepare_finished_args)
    finished_args
  end
end

#get_argument(argument_name) ⇒ GraphQL::Schema::Argument?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Argument defined on this thing, fetched by name.

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/graphql/schema/member/has_arguments.rb', line 97

def get_argument(argument_name)
  a = own_arguments[argument_name]

  if a || !self.is_a?(Class)
    a
  else
    for ancestor in ancestors
      if ancestor.respond_to?(:own_arguments) && a = ancestor.own_arguments[argument_name]
        return a
      end
    end
    nil
  end
end

#own_argumentsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



284
285
286
# File 'lib/graphql/schema/member/has_arguments.rb', line 284

def own_arguments
  @own_arguments || NO_ARGUMENTS
end

#validate_directive_argument(arg_defn, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Usually, this is validated statically by RequiredArgumentsArePresent, but not for directives. TODO apply static validations on schema definitions?



183
184
185
186
187
188
189
# File 'lib/graphql/schema/member/has_arguments.rb', line 183

def validate_directive_argument(arg_defn, value)
  if arg_defn.owner.is_a?(Class) && arg_defn.owner < GraphQL::Schema::Directive
    if value.nil? && arg_defn.type.non_null?
      raise ArgumentError, "#{arg_defn.path} is required, but no value was given"
    end
  end
end