Class: GraphQL::Schema::InputObject

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

Constant Summary

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::HasArguments

add_argument, argument, argument_class, own_arguments

Methods included from Dig

#dig

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

#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

Constructor Details

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

Returns a new instance of InputObject



10
11
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
# File 'lib/graphql/schema/input_object.rb', line 10

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?
        GraphQL::Execution::Lazy.all(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>)


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

def arguments_class
  @arguments_class
end

Instance Attribute Details

#argumentsGraphQL::Query::Arguments (readonly)

Returns The underlying arguments instance

Returns:



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

def arguments
  @arguments
end

#contextGraphQL::Query::Context (readonly)

Returns The context for this query

Returns:



42
43
44
# File 'lib/graphql/schema/input_object.rb', line 42

def context
  @context
end

Class Method Details

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



101
102
103
104
105
106
107
108
# File 'lib/graphql/schema/input_object.rb', line 101

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

.kindObject



126
127
128
# File 'lib/graphql/schema/input_object.rb', line 126

def kind
  GraphQL::TypeKinds::INPUT_OBJECT
end

.to_graphqlObject



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

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

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)


78
79
80
81
82
83
84
85
86
# File 'lib/graphql/schema/input_object.rb', line 78

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

#key?(key) ⇒ Boolean

Returns:



88
89
90
# File 'lib/graphql/schema/input_object.rb', line 88

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

#to_hObject



50
51
52
53
54
# File 'lib/graphql/schema/input_object.rb', line 50

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

#to_hashObject



56
57
58
# File 'lib/graphql/schema/input_object.rb', line 56

def to_hash
  to_h
end

#to_kwargsObject

A copy of the Ruby-style hash



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

def to_kwargs
  @ruby_style_hash.dup
end

#unwrap_value(value) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphql/schema/input_object.rb', line 60

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