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

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:



45
46
47
48
# File 'lib/graphql/schema/member/has_arguments.rb', line 45

def add_argument(arg_defn)
  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
# 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)
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



62
63
64
# File 'lib/graphql/schema/member/has_arguments.rb', line 62

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



51
52
53
54
55
56
57
58
59
# File 'lib/graphql/schema/member/has_arguments.rb', line 51

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

#coerce_arguments(parent_object, values, context) ⇒ Hash<Symbol, Object>, Execution::Lazy<Hash>

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:

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/graphql/schema/member/has_arguments.rb', line 70

def coerce_arguments(parent_object, values, context)
  argument_values = {}
  kwarg_arguments = {}
  # Cache this hash to avoid re-merging it
  arg_defns = self.arguments

  maybe_lazies = []
  arg_lazies = arg_defns.map do |arg_name, arg_defn|
    arg_key = arg_defn.keyword
    has_value = false
    default_used = false
    if values.key?(arg_name)
      has_value = true
      value = values[arg_name]
    elsif values.key?(arg_key)
      has_value = true
      value = values[arg_key]
    elsif arg_defn.default_value?
      has_value = true
      value = arg_defn.default_value
      default_used = true
    end

    if has_value
      loads = arg_defn.loads
      loaded_value = nil
      if loads && !arg_defn.from_resolver?
        loaded_value = if arg_defn.type.list?
          loaded_values = value.map { |val| load_application_object(arg_defn, loads, val, context) }
          context.schema.after_any_lazies(loaded_values) { |result| result }
        else
          load_application_object(arg_defn, loads, value, context)
        end
      end

      coerced_value = if loaded_value
        loaded_value
      else
        context.schema.error_handler.with_error_handling(context) do
          arg_defn.type.coerce_input(value, context)
        end
      end

      context.schema.after_lazy(coerced_value) do |coerced_value|
        prepared_value = context.schema.error_handler.with_error_handling(context) do
          arg_defn.prepare_value(parent_object, coerced_value, context: context)
        end

        kwarg_arguments[arg_key] = prepared_value
        # TODO code smell to access such a deeply-nested constant in a distant module
        argument_values[arg_key] = GraphQL::Execution::Interpreter::ArgumentValue.new(
          value: prepared_value,
          definition: arg_defn,
          default_used: default_used,
        )
      end
    end
  end

  maybe_lazies.concat(arg_lazies)
  context.schema.after_any_lazies(maybe_lazies) do
    GraphQL::Execution::Interpreter::Arguments.new(
      keyword_arguments: kwarg_arguments,
      argument_values: argument_values,
    )
  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.



210
211
212
# File 'lib/graphql/schema/member/has_arguments.rb', line 210

def own_arguments
  @own_arguments ||= {}
end