{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Morpheus.Server.Deriving.Utils.Decode
( withInputObject,
withEnum,
withInputUnion,
decodeFieldWith,
withScalar,
handleEither,
getFieldName,
DecoderT,
setVariantRef,
Context (..),
getUnionInfos,
DescribeCons,
DescribeFields (countFields),
)
where
import Control.Monad.Except (MonadError (throwError))
import Data.Morpheus.App.Internal.Resolving (ResolverState)
import Data.Morpheus.Internal.Utils
( selectOr,
)
import Data.Morpheus.Server.Deriving.Utils (conNameProxy)
import Data.Morpheus.Server.Deriving.Utils.Kinded (KindedProxy (..))
import Data.Morpheus.Server.Types.GQLType
( GQLType,
deriveTypename,
)
import Data.Morpheus.Types.GQLScalar
( toScalar,
)
import Data.Morpheus.Types.Internal.AST
( FieldName,
GQLError,
IN,
Msg (msg),
ObjectEntry (..),
ScalarValue,
Token,
TypeName,
VALID,
ValidObject,
ValidValue,
Value (..),
getInputUnionValue,
internal,
)
import GHC.Generics
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
getFieldName :: FieldName -> Int -> FieldName
getFieldName :: FieldName -> Int -> FieldName
getFieldName FieldName
"" Int
index = FieldName
"_" forall a. Semigroup a => a -> a -> a
<> forall b a. (Show a, IsString b) => a -> b
show Int
index
getFieldName FieldName
label Int
_ = FieldName
label
data VariantKind = InlineVariant | VariantRef deriving (VariantKind -> VariantKind -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VariantKind -> VariantKind -> Bool
$c/= :: VariantKind -> VariantKind -> Bool
== :: VariantKind -> VariantKind -> Bool
$c== :: VariantKind -> VariantKind -> Bool
Eq, Eq VariantKind
VariantKind -> VariantKind -> Bool
VariantKind -> VariantKind -> Ordering
VariantKind -> VariantKind -> VariantKind
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: VariantKind -> VariantKind -> VariantKind
$cmin :: VariantKind -> VariantKind -> VariantKind
max :: VariantKind -> VariantKind -> VariantKind
$cmax :: VariantKind -> VariantKind -> VariantKind
>= :: VariantKind -> VariantKind -> Bool
$c>= :: VariantKind -> VariantKind -> Bool
> :: VariantKind -> VariantKind -> Bool
$c> :: VariantKind -> VariantKind -> Bool
<= :: VariantKind -> VariantKind -> Bool
$c<= :: VariantKind -> VariantKind -> Bool
< :: VariantKind -> VariantKind -> Bool
$c< :: VariantKind -> VariantKind -> Bool
compare :: VariantKind -> VariantKind -> Ordering
$ccompare :: VariantKind -> VariantKind -> Ordering
Ord)
data Info = Info
{ Info -> VariantKind
kind :: VariantKind,
Info -> [TypeName]
tagName :: [TypeName]
}
instance Semigroup Info where
Info VariantKind
VariantRef [TypeName]
t1 <> :: Info -> Info -> Info
<> Info VariantKind
_ [TypeName]
t2 = VariantKind -> [TypeName] -> Info
Info VariantKind
VariantRef ([TypeName]
t1 forall a. Semigroup a => a -> a -> a
<> [TypeName]
t2)
Info VariantKind
_ [TypeName]
t1 <> Info VariantKind
VariantRef [TypeName]
t2 = VariantKind -> [TypeName] -> Info
Info VariantKind
VariantRef ([TypeName]
t1 forall a. Semigroup a => a -> a -> a
<> [TypeName]
t2)
Info VariantKind
InlineVariant [TypeName]
t1 <> Info VariantKind
InlineVariant [TypeName]
t2 = VariantKind -> [TypeName] -> Info
Info VariantKind
InlineVariant ([TypeName]
t1 forall a. Semigroup a => a -> a -> a
<> [TypeName]
t2)
data Context = Context
{ Context -> Bool
isVariantRef :: Bool,
Context -> TypeName
typeName :: TypeName,
Context -> TypeName -> TypeName
enumVisitor :: TypeName -> TypeName,
Context -> FieldName -> FieldName
fieldVisitor :: FieldName -> FieldName
}
type DecoderT = ReaderT Context ResolverState
setVariantRef :: Bool -> DecoderT a -> DecoderT a
setVariantRef :: forall a. Bool -> DecoderT a -> DecoderT a
setVariantRef Bool
isVariantRef = forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\Context
ctx -> Context
ctx {Bool
isVariantRef :: Bool
isVariantRef :: Bool
isVariantRef})
class DescribeCons (f :: Type -> Type) where
tags :: Proxy f -> Context -> Info
instance (Datatype d, DescribeCons f) => DescribeCons (M1 D d f) where
tags :: Proxy (M1 D d f) -> Context -> Info
tags Proxy (M1 D d f)
_ = forall (f :: * -> *). DescribeCons f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @f)
instance (DescribeCons a, DescribeCons b) => DescribeCons (a :+: b) where
tags :: Proxy (a :+: b) -> Context -> Info
tags Proxy (a :+: b)
_ = forall (f :: * -> *). DescribeCons f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @a) forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *). DescribeCons f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @b)
instance (Constructor c, DescribeFields a) => DescribeCons (M1 C c a) where
tags :: Proxy (M1 C c a) -> Context -> Info
tags Proxy (M1 C c a)
_ Context {TypeName
typeName :: TypeName
typeName :: Context -> TypeName
typeName} = Maybe TypeName -> Info
getTag (forall (f :: * -> *). DescribeFields f => Proxy f -> Maybe TypeName
refType (forall {k} (t :: k). Proxy t
Proxy @a))
where
getTag :: Maybe TypeName -> Info
getTag (Just TypeName
memberRef)
| TypeName -> Bool
isUnionRef TypeName
memberRef = Info {kind :: VariantKind
kind = VariantKind
VariantRef, tagName :: [TypeName]
tagName = [TypeName
memberRef]}
| Bool
otherwise = Info {kind :: VariantKind
kind = VariantKind
InlineVariant, tagName :: [TypeName]
tagName = [TypeName
consName]}
getTag Maybe TypeName
Nothing = Info {kind :: VariantKind
kind = VariantKind
InlineVariant, tagName :: [TypeName]
tagName = [TypeName
consName]}
consName :: TypeName
consName = forall (f :: Meta -> *) (c :: Meta).
Constructor c =>
f c -> TypeName
conNameProxy (forall {k} (t :: k). Proxy t
Proxy @c)
isUnionRef :: TypeName -> Bool
isUnionRef TypeName
x = TypeName
typeName forall a. Semigroup a => a -> a -> a
<> TypeName
x forall a. Eq a => a -> a -> Bool
== TypeName
consName
getUnionInfos ::
forall f a b.
(DescribeCons a, DescribeCons b) =>
f (a :+: b) ->
DecoderT (Bool, ([TypeName], [TypeName]))
getUnionInfos :: forall (f :: (* -> *) -> *) (a :: * -> *) (b :: * -> *).
(DescribeCons a, DescribeCons b) =>
f (a :+: b) -> DecoderT (Bool, ([TypeName], [TypeName]))
getUnionInfos f (a :+: b)
_ = do
Context
context <- forall r (m :: * -> *). MonadReader r m => m r
ask
let l :: Info
l = forall (f :: * -> *). DescribeCons f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @a) Context
context
let r :: Info
r = forall (f :: * -> *). DescribeCons f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @b) Context
context
let k :: VariantKind
k = Info -> VariantKind
kind (Info
l forall a. Semigroup a => a -> a -> a
<> Info
r)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VariantKind
k forall a. Eq a => a -> a -> Bool
== VariantKind
VariantRef, (Info -> [TypeName]
tagName Info
l, Info -> [TypeName]
tagName Info
r))
class DescribeFields (f :: Type -> Type) where
refType :: Proxy f -> Maybe TypeName
countFields :: Proxy f -> Int
instance (DescribeFields f, DescribeFields g) => DescribeFields (f :*: g) where
refType :: Proxy (f :*: g) -> Maybe TypeName
refType Proxy (f :*: g)
_ = forall a. Maybe a
Nothing
countFields :: Proxy (f :*: g) -> Int
countFields Proxy (f :*: g)
_ = forall (f :: * -> *). DescribeFields f => Proxy f -> Int
countFields (forall {k} (t :: k). Proxy t
Proxy @f) forall a. Num a => a -> a -> a
+ forall (f :: * -> *). DescribeFields f => Proxy f -> Int
countFields (forall {k} (t :: k). Proxy t
Proxy @g)
instance (Selector s, GQLType a) => DescribeFields (M1 S s (K1 i a)) where
refType :: Proxy (M1 S s (K1 i a)) -> Maybe TypeName
refType Proxy (M1 S s (K1 i a))
_ = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a (kind :: TypeCategory) (kinded :: TypeCategory -> * -> *).
(GQLType a, CategoryValue kind) =>
kinded kind a -> TypeName
deriveTypename (forall {k} {k} (k :: k) (a :: k). KindedProxy k a
KindedProxy :: KindedProxy IN a)
countFields :: Proxy (M1 S s (K1 i a)) -> Int
countFields Proxy (M1 S s (K1 i a))
_ = Int
1
instance DescribeFields U1 where
refType :: Proxy U1 -> Maybe TypeName
refType Proxy U1
_ = forall a. Maybe a
Nothing
countFields :: Proxy U1 -> Int
countFields Proxy U1
_ = Int
0