Class: GraphQL::ScalarType
- Defined in:
- lib/graphql/scalar_type.rb
Overview
GraphQL::ScalarType
Scalars are plain values. They are leaf nodes in a GraphQL query tree.
Built-in Scalars
GraphQL
comes with standard built-in scalars:
Constant | .define helper |
---|---|
GraphQL::STRING_TYPE |
types.String |
GraphQL::INT_TYPE |
types.Int |
GraphQL::FLOAT_TYPE |
types.Float |
GraphQL::ID_TYPE |
types.ID |
GraphQL::BOOLEAN_TYPE |
types.Boolean |
(types
is an instance of GraphQL::Definition::TypeDefiner
; .String
, .Float
, etc are methods which return built-in scalars.)
Custom Scalars
You can define custom scalars for your GraphQL server. It requires some special functions:
coerce_input
is used to prepare incoming values for GraphQL execution. (Incoming values come from variables or literal values in the query string.)coerce_result
is used to turn Ruby values back into serializable values for query responses.
Instance Attribute Summary
Attributes inherited from BaseType
#default_relay, #default_scalar, #description, #introspection, #name
Instance Method Summary collapse
- #coerce=(proc) ⇒ Object
- #coerce_input=(proc) ⇒ Object
- #coerce_non_null_input(value) ⇒ Object
- #coerce_result(value) ⇒ Object
- #coerce_result=(proc) ⇒ Object
- #kind ⇒ Object
- #validate_non_null_input(value, warden) ⇒ Object
Methods inherited from BaseType
#==, #coerce_input, #connection_type, #default_relay?, #default_scalar?, #define_connection, #define_edge, #edge_type, #get_field, #initialize, #initialize_copy, #introspection?, resolve_related_type, #resolve_type, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #validate_input
Methods included from Define::InstanceDefinable
#define, #initialize_copy, #metadata, #redefine
Methods included from Define::NonNullWithBang
Constructor Details
This class inherits a constructor from GraphQL::BaseType
Instance Method Details
#coerce=(proc) ⇒ Object
41 42 43 44 |
# File 'lib/graphql/scalar_type.rb', line 41 def coerce=(proc) self.coerce_input = proc self.coerce_result = proc end |
#coerce_input=(proc) ⇒ Object
58 59 60 61 62 |
# File 'lib/graphql/scalar_type.rb', line 58 def coerce_input=(proc) if !proc.nil? @coerce_input_proc = proc end end |
#coerce_non_null_input(value) ⇒ Object
54 55 56 |
# File 'lib/graphql/scalar_type.rb', line 54 def coerce_non_null_input(value) @coerce_input_proc.call(value) end |
#coerce_result(value) ⇒ Object
64 65 66 |
# File 'lib/graphql/scalar_type.rb', line 64 def coerce_result(value) @coerce_result_proc ? @coerce_result_proc.call(value) : value end |
#coerce_result=(proc) ⇒ Object
68 69 70 71 72 |
# File 'lib/graphql/scalar_type.rb', line 68 def coerce_result=(proc) if !proc.nil? @coerce_result_proc = proc end end |
#kind ⇒ Object
74 75 76 |
# File 'lib/graphql/scalar_type.rb', line 74 def kind GraphQL::TypeKinds::SCALAR end |
#validate_non_null_input(value, warden) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/graphql/scalar_type.rb', line 46 def validate_non_null_input(value, warden) result = Query::InputValidationResult.new if coerce_non_null_input(value).nil? result.add_problem("Could not coerce value #{GraphQL::Language.serialize(value)} to #{name}") end result end |