Safe Haskell | None |
---|---|
Language | Haskell2010 |
Types and functions used for input and result coercion.
Synopsis
- data Output a
- class Serialize a where
- class VariableValue a where
- coerceVariableValue :: Type -> a -> Maybe Value
- coerceInputLiteral :: Type -> Value -> Maybe Value
- matchFieldValues :: forall a. (Type -> a -> Maybe Value) -> HashMap Name a -> Name -> Type -> Maybe Value -> Maybe (HashMap Name Value) -> Maybe (HashMap Name Value)
Documentation
Intermediate type used to serialize a GraphQL
value.
The serialization is done during the execution, and Output
contains
already serialized data (in List
and Object
) as well as the new layer
that has to be serialized in the current step. So Output
is parameterized
by the serialization format.
class Serialize a where Source #
Serialize
describes how a GraphQL
value should be serialized.
:: forall m. Type m | Expected output type. |
-> Output a | The value to be serialized. |
-> Maybe a | Serialized value on success or |
Serializes a GraphQL
value according to the given serialization
format.
Type infomration is given as a hint, e.g. if you need to know what type
is being serialized to serialize it properly. Don't do any validation for
GraphQL
built-in types here.
If the value cannot be serialized without losing information, return
Nothing
— it will cause a field error.
class VariableValue a where Source #
Since variables are passed separately from the query, in an independent format, they should be first coerced to the internal representation used by this implementation.
:: Type | Expected type (variable type given in the query). |
-> a | Variable value being coerced. |
-> Maybe Value | Coerced value on success, |
Only a basic, format-specific, coercion must be done here. Type correctness or nullability shouldn't be validated here, they will be validated later. The type information is provided only as a hint.
For example GraphQL
prohibits the coercion from a 't:Float' to an
't:Int', but JSON
doesn't have integers, so whole numbers should be
coerced to 't:Int` when receiving variables as a JSON object. The same
holds for 't:Enum'. There are formats that support enumerations, JSON
doesn't, so the type information is given and coerceVariableValue
can
check that an 't:Enum' is expected and treat the given value
appropriately. Even checking whether this value is a proper member of the
corresponding 't:Enum' type isn't required here, since this can be
checked independently.
Another example is an ID
. GraphQL
explicitly allows to coerce
integers and strings to ID
s, so if an ID
is received as an integer,
it can be left as is and will be coerced later.
If a value cannot be coerced without losing information, Nothing
should
be returned, the coercion will fail then and the query won't be executed.
Instances
VariableValue Value Source # | |
Defined in Language.GraphQL.Execute.Coerce |
coerceInputLiteral :: Type -> Value -> Maybe Value Source #
Coerces operation arguments according to the input coercion rules for the corresponding types.
matchFieldValues :: forall a. (Type -> a -> Maybe Value) -> HashMap Name a -> Name -> Type -> Maybe Value -> Maybe (HashMap Name Value) -> Maybe (HashMap Name Value) Source #
Looks up a value by name in the given map, coerces it and inserts into the
result map. If the coercion fails, returns Nothing
. If the value isn't
given, but a default value is known, inserts the default value into the
result map. Otherwise it fails with Nothing
if the Input Type is a
Non-Nullable type, or returns the unchanged, original map.