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
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) ⇒ Object
Methods inherited from BaseType
#==, #coerce_input, #connection_type, #define_connection, #define_edge, #edge_type, #get_field, 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, #definition_proc=, included, #metadata
Methods included from Define::NonNullWithBang
Instance Method Details
#coerce=(proc) ⇒ Object
39 40 41 42 |
# File 'lib/graphql/scalar_type.rb', line 39 def coerce=(proc) self.coerce_input = proc self.coerce_result = proc end |
#coerce_input=(proc) ⇒ Object
57 58 59 60 61 |
# File 'lib/graphql/scalar_type.rb', line 57 def coerce_input=(proc) if !proc.nil? @coerce_input_proc = proc end end |
#coerce_non_null_input(value) ⇒ Object
52 53 54 55 |
# File 'lib/graphql/scalar_type.rb', line 52 def coerce_non_null_input(value) ensure_defined @coerce_input_proc.call(value) end |
#coerce_result(value) ⇒ Object
63 64 65 66 |
# File 'lib/graphql/scalar_type.rb', line 63 def coerce_result(value) ensure_defined @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) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/graphql/scalar_type.rb', line 44 def validate_non_null_input(value) result = Query::InputValidationResult.new if coerce_non_null_input(value).nil? result.add_problem("Could not coerce value #{JSON.generate(value, quirks_mode: true)} to #{name}") end result end |