{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Morpheus.Server.Deriving.Utils.Decode
( withInputObject,
withEnum,
withInputUnion,
decodeFieldWith,
withScalar,
handleEither,
)
where
import Control.Monad.Except (MonadError (throwError))
import Data.Morpheus.Internal.Utils
( selectOr,
)
import Data.Morpheus.Types.GQLScalar
( toScalar,
)
import Data.Morpheus.Types.Internal.AST
( FieldName,
GQLError,
Msg (msg),
ObjectEntry (..),
ScalarValue,
Token,
TypeName,
VALID,
ValidObject,
ValidValue,
Value (..),
getInputUnionValue,
internal,
)
import Relude
withInputObject ::
MonadError GQLError m =>
(ValidObject -> m a) ->
ValidValue ->
m a
withInputObject :: forall (m :: * -> *) a.
MonadError GQLError m =>
(ValidObject -> m a) -> ValidValue -> m a
withInputObject ValidObject -> m a
f (Object ValidObject
object) = ValidObject -> m a
f ValidObject
object
withInputObject ValidObject -> m a
_ ValidValue
isType = forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (forall (s :: Stage). GQLError -> Value s -> GQLError
typeMismatch GQLError
"InputObject" ValidValue
isType)
withEnum :: MonadError GQLError m => (TypeName -> m a) -> Value VALID -> m a
withEnum :: forall (m :: * -> *) a.
MonadError GQLError m =>
(TypeName -> m a) -> ValidValue -> m a
withEnum TypeName -> m a
decode (Enum TypeName
value) = TypeName -> m a
decode TypeName
value
withEnum TypeName -> m a
_ ValidValue
isType = forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (forall (s :: Stage). GQLError -> Value s -> GQLError
typeMismatch GQLError
"Enum" ValidValue
isType)
withInputUnion ::
(MonadError GQLError m, Monad m) =>
(TypeName -> ValidObject -> ValidObject -> m a) ->
ValidObject ->
m a
withInputUnion :: forall (m :: * -> *) a.
(MonadError GQLError m, Monad m) =>
(TypeName -> ValidObject -> ValidObject -> m a)
-> ValidObject -> m a
withInputUnion TypeName -> ValidObject -> ValidObject -> m a
decoder ValidObject
unions =
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall {a}. GQLError -> m a
onFail (TypeName, ValidValue) -> m a
onSuccess (forall (stage :: Stage).
Object stage -> Either GQLError (TypeName, Value stage)
getInputUnionValue ValidObject
unions)
where
onSuccess :: (TypeName, ValidValue) -> m a
onSuccess (TypeName
name, ValidValue
value) = forall (m :: * -> *) a.
MonadError GQLError m =>
(ValidObject -> m a) -> ValidValue -> m a
withInputObject (TypeName -> ValidObject -> ValidObject -> m a
decoder TypeName
name ValidObject
unions) ValidValue
value
onFail :: GQLError -> m a
onFail = forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall b c a. (b -> c) -> (a -> b) -> a -> c
. GQLError -> GQLError
internal forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Msg a => a -> GQLError
msg
withScalar ::
(Applicative m, MonadError GQLError m) =>
TypeName ->
(ScalarValue -> Either Token a) ->
Value VALID ->
m a
withScalar :: forall (m :: * -> *) a.
(Applicative m, MonadError GQLError m) =>
TypeName -> (ScalarValue -> Either Token a) -> ValidValue -> m a
withScalar TypeName
typename ScalarValue -> Either Token a
decodeScalar ValidValue
value = case ValidValue -> Either Token ScalarValue
toScalar ValidValue
value forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ScalarValue -> Either Token a
decodeScalar of
Right a
scalar -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
scalar
Left Token
message ->
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
( forall (s :: Stage). GQLError -> Value s -> GQLError
typeMismatch
(GQLError
"SCALAR(" forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg TypeName
typename forall a. Semigroup a => a -> a -> a
<> GQLError
")" forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg Token
message)
ValidValue
value
)
decodeFieldWith :: (Value VALID -> m a) -> FieldName -> ValidObject -> m a
decodeFieldWith :: forall (m :: * -> *) a.
(ValidValue -> m a) -> FieldName -> ValidObject -> m a
decodeFieldWith ValidValue -> m a
decoder = forall k (c :: * -> *) d a.
IsMap k c =>
d -> (a -> d) -> k -> c a -> d
selectOr (ValidValue -> m a
decoder forall (stage :: Stage). Value stage
Null) (ValidValue -> m a
decoder forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: Stage). ObjectEntry s -> Value s
entryValue)
handleEither :: MonadError GQLError m => Either GQLError a -> m a
handleEither :: forall (m :: * -> *) a.
MonadError GQLError m =>
Either GQLError a -> m a
handleEither = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall (f :: * -> *) a. Applicative f => a -> f a
pure
typeMismatch :: GQLError -> Value s -> GQLError
typeMismatch :: forall (s :: Stage). GQLError -> Value s -> GQLError
typeMismatch GQLError
text Value s
jsType =
GQLError -> GQLError
internal forall a b. (a -> b) -> a -> b
$
GQLError
"Type mismatch! expected:" forall a. Semigroup a => a -> a -> a
<> GQLError
text forall a. Semigroup a => a -> a -> a
<> GQLError
", got: "
forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg Value s
jsType