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, ClassConfigured, FieldConfigured, HasDirectiveArguments

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.

GraphQL::EmptyObjects::EMPTY_HASH

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:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/schema/member/has_arguments.rb', line 47

def add_argument(arg_defn)
  @own_arguments ||= {}
  prev_defn = @own_arguments[arg_defn.name]
  case prev_defn
  when nil
    @own_arguments[arg_defn.name] = arg_defn
  when Array
    prev_defn << arg_defn
  when GraphQL::Schema::Argument
    @own_arguments[arg_defn.name] = [prev_defn, arg_defn]
  else
    raise "Invariant: unexpected `@own_arguments[#{arg_defn.name.inspect}]`: #{prev_defn.inspect}"
  end
  arg_defn
end

#all_argument_definitionsObject

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.



200
201
202
203
204
205
206
207
208
# File 'lib/graphql/schema/member/has_arguments.rb', line 200

def all_argument_definitions
  if !own_arguments.empty?
    all_defns = own_arguments.values
    all_defns.flatten!
    all_defns
  else
    EmptyObjects::EMPTY_ARRAY
  end
end

#any_arguments?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:



92
93
94
# File 'lib/graphql/schema/member/has_arguments.rb', line 92

def any_arguments?
  !own_arguments.empty?
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 #argument_class, created from *args.

Returns:

See Also:

  • for parameters


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/graphql/schema/member/has_arguments.rb', line 19

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)
  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



223
224
225
# File 'lib/graphql/schema/member/has_arguments.rb', line 223

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

#arguments(context = GraphQL::Query::NullContext.instance, _require_defined_arguments = nil) ⇒ Hash<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 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 GraphQL::Schema::Argument] Arguments defined on this thing, keyed by name. Includes inherited definitions



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/graphql/schema/member/has_arguments.rb', line 79

def arguments(context = GraphQL::Query::NullContext.instance, _require_defined_arguments = nil)
  if !own_arguments.empty?
    own_arguments_that_apply = {}
    own_arguments.each do |name, args_entry|
      if (visible_defn = Warden.visible_entry?(:visible_argument?, args_entry, context))
        own_arguments_that_apply[visible_defn.graphql_name] = visible_defn
      end
    end
  end
  # might be nil if there are actually no arguments
  own_arguments_that_apply || own_arguments
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:



306
307
308
309
310
311
312
# File 'lib/graphql/schema/member/has_arguments.rb', line 306

def arguments_statically_coercible?
  if defined?(@arguments_statically_coercible) && !@arguments_statically_coercible.nil?
    @arguments_statically_coercible
  else
    @arguments_statically_coercible = all_argument_definitions.all?(&:statically_coercible?)
  end
end

#coerce_arguments(parent_object, values, context) {|Interpreter::Arguments, Execution::Lazy<Interpreter::Arguments>| ... } ⇒ Interpreter::Arguments, Execution::Lazy<Interpreter::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:



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

def coerce_arguments(parent_object, values, context, &block)
  # Cache this hash to avoid re-merging it
  arg_defns = context.types.arguments(self)
  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_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, context = GraphQL::Query::NullContext.instance) ⇒ 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:



211
212
213
214
215
216
217
218
219
220
# File 'lib/graphql/schema/member/has_arguments.rb', line 211

def get_argument(argument_name, context = GraphQL::Query::NullContext.instance)
  warden = Warden.from_context(context)
  if (arg_config = own_arguments[argument_name]) && ((context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)) || (visible_arg = Warden.visible_entry?(:visible_argument?, arg_config, context, warden)))
    visible_arg || arg_config
  elsif defined?(@resolver_class) && @resolver_class
    @resolver_class.get_field_argument(argument_name, context)
  else
    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.



422
423
424
# File 'lib/graphql/schema/member/has_arguments.rb', line 422

def own_arguments
  @own_arguments || NO_ARGUMENTS
end

#remove_argument(arg_defn) ⇒ 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.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/graphql/schema/member/has_arguments.rb', line 63

def remove_argument(arg_defn)
  prev_defn = @own_arguments[arg_defn.name]
  case prev_defn
  when nil
    # done
  when Array
    prev_defn.delete(arg_defn)
  when GraphQL::Schema::Argument
    @own_arguments.delete(arg_defn.name)
  else
    raise "Invariant: unexpected `@own_arguments[#{arg_defn.name.inspect}]`: #{prev_defn.inspect}"
  end
  nil
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?



293
294
295
296
# File 'lib/graphql/schema/member/has_arguments.rb', line 293

def validate_directive_argument(arg_defn, value)
  # this is only implemented on directives.
  nil
end