Class: Rails::GraphQL::Type::Input
- Inherits:
-
Rails::GraphQL::Type
- Object
- Rails::GraphQL::Type
- Rails::GraphQL::Type::Input
- Extended by:
- Helpers::WithAssignment, Helpers::WithFields
- Defined in:
- lib/rails/graphql/type/input.rb
Overview
GraphQL InputType
Input defines a set of input fields; the input fields are either scalars, enums, or other input objects. See spec.graphql.org/June2018/#InputObjectTypeDefinition
Constant Summary
Constants inherited from Rails::GraphQL::Type
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#assignment_error ⇒ Object
readonly
Returns the value of attribute assignment_error.
-
#resource(*args, **xargs, &block) ⇒ Object
If the input is assigned to a class, then initialize it with the received arguments.
Class Method Summary collapse
-
.as_json(value) ⇒ Object
Transforms the given value to its representation in a JSON string.
-
.build_defaults ⇒ Object
Build a hash with the default values for each of the given fields.
-
.deserialize(value = nil, **value_as_hash) ⇒ Object
(also: build)
Turn the given value into an instance of the input object.
-
.gql_name ⇒ Object
A little override on the name of the object due to the suffix config.
- .inspect ⇒ Object
-
.to_json(value) ⇒ Object
Transforms the given value to its representation in a Hash object.
-
.valid_input?(value) ⇒ Boolean
Check if a given value is a valid non-deserialized input.
Instance Method Summary collapse
-
#args_as_json ⇒ Object
(also: #as_json)
Correctly turn all the arguments into their
as_json
version and return a hash of them. -
#args_to_json ⇒ Object
(also: #to_json)
Correctly turn all the arguments into their
to_json
version and return a hash of them. -
#initialize(args = nil, **xargs) ⇒ Input
constructor
A new instance of Input.
-
#params ⇒ Object
Just return the arguments as an hash.
-
#safe_assigned_class ⇒ Object
Override this method to save any errors that could happen with loading the assigned class.
-
#validate! ⇒ Object
Checks if all the values provided to the input instance are valid.
Methods included from Helpers::WithAssignment
assigned?, assigned_class, assigned_to, assigned_to=, extended, register!, valid_assignment?
Methods included from Helpers::WithFields
change_field, configure_field, disable_fields, enable_fields, enabled_fields, extended, field, field_names, find_by_gid, find_field, find_field!, has_field?, import, import_all, proxy_field, safe_field
Methods inherited from Rails::GraphQL::Type
=~, base_type, create!, decorate, find_by_gid, gid_base_class, input_type?, kind, kind_enum, leaf_type?, operational?, output_type?, to_gql_backtrace
Methods included from Helpers::WithDirectives
#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, included, #initialize_copy, #use, #using?
Methods included from Helpers::WithGlobalID
Methods included from Helpers::Registerable
#aliases, extended, #inherited, #register!, #registered?
Constructor Details
#initialize(args = nil, **xargs) ⇒ Input
Returns a new instance of Input.
116 117 118 119 120 121 |
# File 'lib/rails/graphql/type/input.rb', line 116 def initialize(args = nil, **xargs) @args = args || build_ostruct(xargs) @args.freeze validate! if args.nil? end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
108 109 110 |
# File 'lib/rails/graphql/type/input.rb', line 108 def args @args end |
#assignment_error ⇒ Object (readonly)
Returns the value of attribute assignment_error.
108 109 110 |
# File 'lib/rails/graphql/type/input.rb', line 108 def assignment_error @assignment_error end |
#resource(*args, **xargs, &block) ⇒ Object
If the input is assigned to a class, then initialize it with the received arguments. It also accepts extra arguments for inheritance purposes
126 127 128 129 130 131 132 133 134 |
# File 'lib/rails/graphql/type/input.rb', line 126 def resource(*args, **xargs, &block) return @resource if defined?(@resource) return if (klass = safe_assigned_class).nil? @resource = begin xargs = xargs.reverse_merge(params) klass.new(*args, **xargs, &block) end end |
Class Method Details
.as_json(value) ⇒ Object
Transforms the given value to its representation in a JSON string
38 39 40 |
# File 'lib/rails/graphql/type/input.rb', line 38 def as_json(value) parse_arguments(value, using: :as_json, key: :gql_name) end |
.build_defaults ⇒ Object
Build a hash with the default values for each of the given fields
73 74 75 76 77 78 |
# File 'lib/rails/graphql/type/input.rb', line 73 def build_defaults return {} unless fields? enabled_fields.each.with_object({}) do |field, hash| hash[field.gql_name] = field.default end end |
.deserialize(value = nil, **value_as_hash) ⇒ Object Also known as: build
Turn the given value into an instance of the input object
65 66 67 68 |
# File 'lib/rails/graphql/type/input.rb', line 65 def deserialize(value = nil, **value_as_hash) value = value_as_hash if value.nil? new(OpenStruct.new(parse_arguments(value, using: :deserialize))) end |
.gql_name ⇒ Object
A little override on the name of the object due to the suffix config
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rails/graphql/type/input.rb', line 26 def gql_name return @gql_name if defined?(@gql_name) suffix = GraphQL.config.auto_suffix_input_objects return super if suffix.blank? result = super result += suffix if result && !result.end_with?(suffix) @gql_name = result end |
.inspect ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/rails/graphql/type/input.rb', line 80 def inspect return super if self.eql?(Type::Input) args = fields.values.map(&:inspect) args = args.presence && +"(#{args.join(', ')})" directives = inspect_directives directives.prepend(' ') if directives.present? +"#<GraphQL::Input #{gql_name}#{args}#{directives}>" end |
.to_json(value) ⇒ Object
Transforms the given value to its representation in a Hash object
43 44 45 |
# File 'lib/rails/graphql/type/input.rb', line 43 def to_json(value) as_json(value).to_json end |
.valid_input?(value) ⇒ Boolean
Check if a given value is a valid non-deserialized input
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rails/graphql/type/input.rb', line 48 def valid_input?(value) value = GraphQL.config.literal_input_parser.call(value) \ if valid_token?(value, :hash) value = value.to_h if value.respond_to?(:to_h) return false unless value.is_a?(::Hash) fields = enabled_fields value = value.transform_keys { |key| key.to_s.camelize(:lower) } value = build_defaults.merge(value) return false unless value.size.eql?(fields&.count || 0) fields&.all? { |item| item.valid_input?(value[item.gql_name]) } end |
Instance Method Details
#args_as_json ⇒ Object Also known as: as_json
Correctly turn all the arguments into their as_json
version and return a hash of them
143 144 145 |
# File 'lib/rails/graphql/type/input.rb', line 143 def args_as_json self.class.as_json(@args.to_h) end |
#args_to_json ⇒ Object Also known as: to_json
Correctly turn all the arguments into their to_json
version and return a hash of them
151 152 153 |
# File 'lib/rails/graphql/type/input.rb', line 151 def args_to_json self.class.to_json(@args.to_h) end |
#params ⇒ Object
Just return the arguments as an hash
137 138 139 |
# File 'lib/rails/graphql/type/input.rb', line 137 def params parametrize(@args.to_h) end |
#safe_assigned_class ⇒ Object
Override this method to save any errors that could happen with loading the assigned class
174 175 176 177 178 179 |
# File 'lib/rails/graphql/type/input.rb', line 174 def safe_assigned_class assigned_class rescue ::ArgumentError, ::NameError => error @assignment_error = error nil end |
#validate! ⇒ Object
Checks if all the values provided to the input instance are valid
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/rails/graphql/type/input.rb', line 158 def validate!(*) errors = [] fields.each do |name, field| field.validate_output!(@args[name.to_s]) rescue InvalidValueError => error errors << error. end return if errors.empty? raise InvalidValueError, (+<<~MSG).squish Invalid value provided to #{gql_name} field: #{errors.to_sentence}. MSG end |