Class: SolidusGraphqlApi::Types::Json
- Inherits:
-
GraphQL::Types::JSON
- Object
- GraphQL::Types::JSON
- SolidusGraphqlApi::Types::Json
- Defined in:
- lib/solidus_graphql_api/types/json.rb
Class Method Summary collapse
- .class_exists?(value) ⇒ Boolean
- .coerce_input(value, ctx) ⇒ Object
- .decode(value) ⇒ Object
- .decode_array(array, ctx) ⇒ Object
- .decode_if_relay_id(value) ⇒ Object
- .to_i_or_nil(value) ⇒ Object
Class Method Details
.class_exists?(value) ⇒ Boolean
49 50 51 52 53 54 55 |
# File 'lib/solidus_graphql_api/types/json.rb', line 49 def self.class_exists?(value) return false if value.nil? Object.const_defined?(value) rescue NameError false end |
.coerce_input(value, ctx) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/solidus_graphql_api/types/json.rb', line 6 def self.coerce_input(value, ctx) value = value.is_a?(ActionController::Parameters) ? value.permit!.to_h : value.to_h value.each do |key, field| case field when String value[key] = decode(field) when Array value[key] = decode_array(field, ctx) when Hash, ActionController::Parameters value[key] = coerce_input(field, ctx) end end value end |
.decode(value) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/solidus_graphql_api/types/json.rb', line 21 def self.decode(value) class_name, item_id = decode_if_relay_id(value) return value unless class_exists?(class_name) to_i_or_nil(item_id) || value end |
.decode_array(array, ctx) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/solidus_graphql_api/types/json.rb', line 29 def self.decode_array(array, ctx) array.map do |value| case value when Hash, ActionController::Parameters coerce_input(value, ctx) when String decode(value) else value end end end |
.decode_if_relay_id(value) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/solidus_graphql_api/types/json.rb', line 42 def self.decode_if_relay_id(value) GraphQL::Schema::UniqueWithinType.decode(value) # As of graphql v1.10.8 a GraphQL::ExecutionError is raised instead of an ArgumentError rescue ArgumentError, GraphQL::ExecutionError [nil, nil] end |
.to_i_or_nil(value) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/solidus_graphql_api/types/json.rb', line 57 def self.to_i_or_nil(value) return value if value.nil? Integer(value) rescue ArgumentError nil end |