Class: GraphQL::Schema::InputObject

Inherits:
Member
  • Object
show all
Extended by:
Forwardable, Member::AcceptsDefinition, Member::HasArguments, Member::ValidatesInput
Includes:
Dig
Defined in:
lib/graphql/schema/input_object.rb

Constant Summary collapse

INVALID_OBJECT_MESSAGE =

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.

"Expected %{object} to be a key-value object responding to `to_h` or `to_unsafe_h`."

Constants included from Member::GraphQLTypeNames

Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Member::ValidatesInput

coerce_isolated_input, coerce_isolated_result, valid_input?, valid_isolated_input?, validate_input

Methods included from Member::HasArguments

add_argument, argument, argument_class, own_arguments

Methods included from Dig

#dig

Methods included from Member::HasAstNode

#ast_node

Methods included from Member::HasPath

#path

Methods included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Methods included from Member::Scoped

#scope_items

Methods included from Member::TypeSystemHelpers

#kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Member::BaseDSLMethods

#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #introspection?, #mutation, #name, #overridden_graphql_name, #to_graphql, #visible?

Methods included from Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

Methods included from Member::CachedGraphQLDefinition

#graphql_definition, #initialize_copy, #type_class

Constructor Details

#initialize(values = nil, ruby_kwargs: nil, context:, defaults_used:) ⇒ InputObject

Returns a new instance of InputObject.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphql/schema/input_object.rb', line 12

def initialize(values = nil, ruby_kwargs: nil, context:, defaults_used:)
  @context = context
  if ruby_kwargs
    @ruby_style_hash = ruby_kwargs
  else
    @arguments = self.class.arguments_class.new(values, context: context, defaults_used: defaults_used)
    # Symbolized, underscored hash:
    @ruby_style_hash = @arguments.to_kwargs
  end
  # Apply prepares, not great to have it duplicated here.
  @arguments_by_keyword = {}
  self.class.arguments.each do |name, arg_defn|
    @arguments_by_keyword[arg_defn.keyword] = arg_defn
    ruby_kwargs_key = arg_defn.keyword
    loads = arg_defn.loads

    if @ruby_style_hash.key?(ruby_kwargs_key) && loads && !arg_defn.from_resolver?
      value = @ruby_style_hash[ruby_kwargs_key]
      @ruby_style_hash[ruby_kwargs_key] = if arg_defn.type.list?
        value.map { |val| load_application_object(arg_defn, loads, val) }
      else
        load_application_object(arg_defn, loads, value)
      end
    end

    if @ruby_style_hash.key?(ruby_kwargs_key) && arg_defn.prepare
      @ruby_style_hash[ruby_kwargs_key] = arg_defn.prepare_value(self, @ruby_style_hash[ruby_kwargs_key])
    end
  end
end

Class Attribute Details

.arguments_classClass<GraphQL::Arguments>

Returns:

  • (Class<GraphQL::Arguments>)


105
106
107
# File 'lib/graphql/schema/input_object.rb', line 105

def arguments_class
  @arguments_class
end

Instance Attribute Details

#argumentsGraphQL::Query::Arguments (readonly)

Returns The underlying arguments instance.

Returns:



47
48
49
# File 'lib/graphql/schema/input_object.rb', line 47

def arguments
  @arguments
end

#contextGraphQL::Query::Context (readonly)

Returns The context for this query.

Returns:



44
45
46
# File 'lib/graphql/schema/input_object.rb', line 44

def context
  @context
end

Class Method Details

.argument(*args, **kwargs, &block) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/graphql/schema/input_object.rb', line 107

def argument(*args, **kwargs, &block)
  argument_defn = super(*args, **kwargs, &block)
  # Add a method access
  method_name = argument_defn.keyword
  define_method(method_name) do
    self[method_name]
  end
end

.coerce_input(value, ctx) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/graphql/schema/input_object.rb', line 187

def coerce_input(value, ctx)
  if value.nil?
    return nil
  end
  input_values = {}

  arguments.each do |name, argument_defn|
    arg_key = argument_defn.keyword
    has_value = false

    # Accept either string or symbol
    field_value = if value.key?(name)
      has_value = true
      value[name]
    elsif value.key?(arg_key)
      has_value = true
      value[arg_key]
    elsif argument_defn.default_value?
      has_value = true
      argument_defn.default_value
    else
      nil
    end
    # Only continue if some value was found for this argument
    if has_value
      coerced_value = argument_defn.type.coerce_input(field_value, ctx)
      prepared_value = argument_defn.prepare_value(nil, coerced_value, context: ctx)
      input_values[arg_key] = prepared_value
    end
  end

  input_obj_instance = self.new(ruby_kwargs: input_values, context: ctx, defaults_used: nil)
  input_obj_instance.prepare
end

.coerce_result(value, ctx) ⇒ Object

It’s funny to think of a result of an input object. This is used for rendering the default value in introspection responses.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/graphql/schema/input_object.rb', line 224

def coerce_result(value, ctx)
  # Allow the application to provide values as :symbols, and convert them to the strings
  value = value.reduce({}) { |memo, (k, v)| memo[k.to_s] = v; memo }

  result = {}

  arguments.each do |input_key, input_field_defn|
    input_value = value[input_key]
    if value.key?(input_key)
      result[input_key] = if input_value.nil?
        nil
      else
        input_field_defn.type.coerce_result(input_value, ctx)
      end
    end
  end

  result
end

.kindObject



133
134
135
# File 'lib/graphql/schema/input_object.rb', line 133

def kind
  GraphQL::TypeKinds::INPUT_OBJECT
end

.to_graphqlObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/graphql/schema/input_object.rb', line 116

def to_graphql
  type_defn = GraphQL::InputObjectType.new
  type_defn.name = graphql_name
  type_defn.description = description
  type_defn.[:type_class] = self
  type_defn.mutation = mutation
  type_defn.ast_node = ast_node
  arguments.each do |name, arg|
    type_defn.arguments[arg.graphql_definition.name] = arg.graphql_definition
  end
  # Make a reference to a classic-style Arguments class
  self.arguments_class = GraphQL::Query::Arguments.construct_arguments_class(type_defn)
  # But use this InputObject class at runtime
  type_defn.arguments_class = self
  type_defn
end

.validate_non_null_input(input, ctx) ⇒ Object



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
179
180
181
182
183
184
185
# File 'lib/graphql/schema/input_object.rb', line 141

def validate_non_null_input(input, ctx)
  result = GraphQL::Query::InputValidationResult.new

  warden = ctx.warden

  if input.is_a?(Array)
    result.add_problem(INVALID_OBJECT_MESSAGE % { object: JSON.generate(input, quirks_mode: true) })
    return result
  end

  # We're not actually _using_ the coerced result, we're just
  # using these methods to make sure that the object will
  # behave like a hash below, when we call `each` on it.
  begin
    input.to_h
  rescue
    begin
      # Handle ActionController::Parameters:
      input.to_unsafe_h
    rescue
      # We're not sure it'll act like a hash, so reject it:
      result.add_problem(INVALID_OBJECT_MESSAGE % { object: JSON.generate(input, quirks_mode: true) })
      return result
    end
  end

  visible_arguments_map = warden.arguments(self).reduce({}) { |m, f| m[f.name] = f; m}

  # Items in the input that are unexpected
  input.each do |name, value|
    if visible_arguments_map[name].nil?
      result.add_problem("Argument is not defined on #{self.graphql_name}", [name])
    end
  end

  # Items in the input that are expected, but have invalid values
  visible_arguments_map.map do |name, argument|
    argument_result = argument.type.validate_input(input[name], ctx)
    if !argument_result.valid?
      result.merge_result!(name, argument_result)
    end
  end

  result
end

Instance Method Details

#[](key) ⇒ Object

Lookup a key on this object, it accepts new-style underscored symbols Or old-style camelized identifiers.

Parameters:

  • key (Symbol, String)


84
85
86
87
88
89
90
91
92
# File 'lib/graphql/schema/input_object.rb', line 84

def [](key)
  if @ruby_style_hash.key?(key)
    @ruby_style_hash[key]
  elsif @arguments
    @arguments[key]
  else
    nil
  end
end

#key?(key) ⇒ Boolean

Returns:



94
95
96
# File 'lib/graphql/schema/input_object.rb', line 94

def key?(key)
  @ruby_style_hash.key?(key) || (@arguments && @arguments.key?(key)) || false
end

#prepareObject



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

def prepare
  self
end

#to_hObject



52
53
54
55
56
# File 'lib/graphql/schema/input_object.rb', line 52

def to_h
  @ruby_style_hash.inject({}) do |h, (key, value)|
    h.merge(key => unwrap_value(value))
  end
end

#to_hashObject



58
59
60
# File 'lib/graphql/schema/input_object.rb', line 58

def to_hash
  to_h
end

#to_kwargsObject

A copy of the Ruby-style hash



99
100
101
# File 'lib/graphql/schema/input_object.rb', line 99

def to_kwargs
  @ruby_style_hash.dup
end

#unwrap_value(value) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/graphql/schema/input_object.rb', line 66

def unwrap_value(value)
  case value
  when Array
    value.map { |item| unwrap_value(item) }
  when Hash
    value.inject({}) do |h, (key, value)|
      h.merge(key => unwrap_value(value))
    end
  when InputObject
    value.to_h
  else
    value
  end
end