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

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

Overview

-- @requires_ancestor: Kernel

Defined Under Namespace

Modules: ArgumentClassAccessor, ArgumentObjectLoader, ClassConfigured, FieldConfigured, HasDirectiveArguments

Constant Summary collapse

NO_ARGUMENTS =
GraphQL::EmptyObjects::EMPTY_HASH

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(cls) ⇒ Object



15
16
17
18
19
# File 'lib/graphql/schema/member/has_arguments.rb', line 15

def self.extended(cls)
  cls.extend(ArgumentClassAccessor)
  cls.include(ArgumentObjectLoader)
  cls.extend(ClassConfigured)
end

.included(cls) ⇒ Object



10
11
12
13
# File 'lib/graphql/schema/member/has_arguments.rb', line 10

def self.included(cls)
  cls.extend(ArgumentClassAccessor)
  cls.include(ArgumentObjectLoader)
end

Instance Method Details

#add_argument(arg_defn) ⇒ Object

Register this argument with the class.

Parameters

  • arg_defn (GraphQL::Schema::Argument)

Returns

  • GraphQL::Schema::Argument

:call-seq:

add_argument(GraphQL::Schema::Argument arg_defn) -> GraphQL::Schema::Argument


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

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_definitions ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/graphql/schema/member/has_arguments.rb', line 257

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

Returns:



144
145
146
# File 'lib/graphql/schema/member/has_arguments.rb', line 144

def any_arguments?
  !own_arguments.empty?
end

#argument(arg_name = nil, type_expr = nil, desc = nil, **kwargs, &definition_block) ⇒ Object

Parameters

  • arg_name (Symbol) — The underscore-cased name of this argument, name: keyword also accepted
  • type_expr — The GraphQL type of this argument; type: keyword also accepted
  • desc (String) — Argument description, description: keyword also accepted
  • definition_block (Proc) — Called with the newly-created Argument
  • kwargs (Hash) — Keywords for defining an argument. Any keywords not documented here must be handled by your base Argument class.

Options

  • kwargs.:required (Boolean, :nullable) — if true, this argument is non-null; if false, this argument is nullable. If :nullable, then the argument must be provided, though it may be null.
  • kwargs.:description (String) — Positional argument also accepted
  • kwargs.:type (Class, Array<Class>) — Input type; positional argument also accepted
  • kwargs.:name (Symbol) — positional argument also accepted
  • kwargs.:default_value (Object)
  • kwargs.:loads (Class, Array<Class>) — A GraphQL type to load for the given ID when one is present
  • kwargs.:as (Symbol) — Override the keyword name when passed to a method
  • kwargs.:prepare (Symbol) — A method to call to transform this argument's valuebefore sending it to field resolution
  • kwargs.:camelize (Boolean) — if true, the name will be camelized when building the schema
  • kwargs.:from_resolver (Boolean) — if true, a Resolver class defined this argument
  • kwargs.:directives (Hash{Class => Hash})
  • kwargs.:deprecation_reason (String)
  • kwargs.:comment (String) — Private, used by GraphQL-Ruby when parsing GraphQL schema files
  • kwargs.:ast_node (GraphQL::Language::Nodes::InputValueDefinition) — Private, used by GraphQL-Ruby when parsing schema files
  • kwargs.:validates (Hash, nil) — Options for building validators, if any should be applied
  • kwargs.:replace_null_with_default (Boolean) — if true, incoming values of null will be replaced with the configured default_value

Returns

  • GraphQL::Schema::Argument — An instance of argument_class created from these arguments

:call-seq:

argument(Symbol arg_name, type_expr, String desc, Hash **kwargs, Proc &definition_block) -> GraphQL::Schema::Argument


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/graphql/schema/member/has_arguments.rb', line 54

def argument(arg_name = nil, type_expr = nil, desc = nil, **kwargs, &definition_block)
  if kwargs[:loads]
    loads_name = arg_name || kwargs[:name]
    loads_name_as_string = loads_name.to_s

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

    kwargs[:as] ||= inferred_arg_name
  end
  kwargs[:owner] = self
  arg_defn = self.argument_class.new(
    arg_name, type_expr, desc,
    **kwargs,
    &definition_block
  )
  add_argument(arg_defn)
  arg_defn
end

#argument_class(new_arg_class = nil) ⇒ Object

Parameters

  • new_arg_class (Class) — A class to use for building argument definitions

:call-seq:

argument_class(Class new_arg_class)


290
291
292
# File 'lib/graphql/schema/member/has_arguments.rb', line 290

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

Returns

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

:call-seq:

arguments(context:, _require_defined_arguments) -> Hash[String, GraphQL::Schema::Argument]


131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/graphql/schema/member/has_arguments.rb', line 131

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

Returns:



376
377
378
379
380
381
382
# File 'lib/graphql/schema/member/has_arguments.rb', line 376

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, &block) ⇒ Object

Yields: [Interpreter::Arguments, Execution::LazyInterpreter::Arguments]

Parameters

  • values (Hash<String, Object>)
  • context (GraphQL::Query::Context)

Returns

  • Interpreter::Arguments, Execution::Lazy<Interpreter::Arguments>


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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/graphql/schema/member/has_arguments.rb', line 304

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

  finished_args = nil #: untyped
  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 #: bool
      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) ⇒ Object

Returns

  • GraphQL::Schema::Argument, nil — Argument defined on this thing, fetched by name.

:call-seq:

get_argument(argument_name, context:) -> GraphQL::Schema::Argument | nil


273
274
275
276
277
278
279
280
281
282
# File 'lib/graphql/schema/member/has_arguments.rb', line 273

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_arguments ⇒ Object



515
516
517
# File 'lib/graphql/schema/member/has_arguments.rb', line 515

def own_arguments
  @own_arguments || NO_ARGUMENTS
end

#remove_argument(arg_defn) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/graphql/schema/member/has_arguments.rb', line 110

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

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



361
362
363
364
# File 'lib/graphql/schema/member/has_arguments.rb', line 361

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