graphql-1.0.2.0: Haskell GraphQL implementation
Safe HaskellNone
LanguageHaskell2010

Language.GraphQL.Execute.Coerce

Description

Types and functions used for input and result coercion.

Synopsis

Documentation

data Output a Source #

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.

Instances

Instances details
Eq a => Eq (Output a) Source # 
Instance details

Defined in Language.GraphQL.Execute.Coerce

Methods

(==) :: Output a -> Output a -> Bool #

(/=) :: Output a -> Output a -> Bool #

Show a => Show (Output a) Source # 
Instance details

Defined in Language.GraphQL.Execute.Coerce

Methods

showsPrec :: Int -> Output a -> ShowS #

show :: Output a -> String #

showList :: [Output a] -> ShowS #

IsString (Output a) Source # 
Instance details

Defined in Language.GraphQL.Execute.Coerce

Methods

fromString :: String -> Output a #

class Serialize a where Source #

Serialize describes how a GraphQL value should be serialized.

Methods

serialize Source #

Arguments

:: forall m. Type m

Expected output type.

-> Output a

The value to be serialized.

-> Maybe a

Serialized value on success or Nothing. | null representation in the given serialization format.

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.

null :: a Source #

Instances

Instances details
Serialize Value Source # 
Instance details

Defined in Language.GraphQL.Execute.Coerce

Methods

serialize :: forall (m :: Type -> Type). Type m -> Output Value -> Maybe Value Source #

null :: Value Source #

Serialize Value Source # 
Instance details

Defined in Language.GraphQL.Execute.Coerce

Methods

serialize :: forall (m :: Type -> Type). Type m -> Output Value -> Maybe Value Source #

null :: Value Source #

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.

Methods

coerceVariableValue Source #

Arguments

:: Type

Expected type (variable type given in the query).

-> a

Variable value being coerced.

-> Maybe Value

Coerced value on success, Nothing otherwise.

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 IDs, 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

Instances details
VariableValue Value Source # 
Instance details

Defined in Language.GraphQL.Execute.Coerce

VariableValue Value Source # 
Instance details

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.