{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Server.Deriving.Decode
  ( decodeArguments,
    Decode (..),
    DecodeConstraint,
  )
where

import Control.Monad.Except (MonadError (throwError))
import qualified Data.Map as M
import Data.Morpheus.App.Internal.Resolving
  ( ResolverState,
  )
import Data.Morpheus.Kind
  ( CUSTOM,
    DerivingKind,
    SCALAR,
    TYPE,
    WRAPPER,
  )
import Data.Morpheus.Server.Deriving.Utils
  ( conNameProxy,
    selNameProxy,
    symbolName,
  )
import Data.Morpheus.Server.Deriving.Utils.Decode
  ( decodeFieldWith,
    handleEither,
    withInputObject,
    withInputUnion,
    withScalar,
  )
import Data.Morpheus.Server.Deriving.Utils.Kinded
  ( KindedProxy (..),
  )
import Data.Morpheus.Server.Types.GQLType
  ( GQLType
      ( KIND,
        typeOptions
      ),
    GQLTypeOptions (..),
    TypeData (..),
    __typeData,
    defaultTypeOptions,
  )
import Data.Morpheus.Server.Types.Types (Arg (Arg))
import Data.Morpheus.Types.GQLScalar
  ( DecodeScalar (..),
  )
import Data.Morpheus.Types.GQLWrapper
  ( DecodeWrapper (..),
    DecodeWrapperConstraint,
  )
import Data.Morpheus.Types.Internal.AST
  ( Argument (..),
    Arguments,
    FieldName,
    GQLError,
    IN,
    LEAF,
    Object,
    ObjectEntry (..),
    TypeName,
    VALID,
    ValidObject,
    ValidValue,
    Value (..),
    internal,
    msg,
  )
import GHC.Generics
import GHC.TypeLits (KnownSymbol)
import Relude

type DecodeConstraint a = (DecodeKind (KIND a) a)

-- GENERIC
decodeArguments :: forall a. DecodeConstraint a => Arguments VALID -> ResolverState a
decodeArguments :: forall a. DecodeConstraint a => Arguments VALID -> ResolverState a
decodeArguments = forall (kind :: DerivingKind) a.
DecodeKind kind a =>
Proxy kind -> ValidValue -> ResolverState a
decodeKind (forall {k} (t :: k). Proxy t
Proxy @(KIND a)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (stage :: Stage). Object stage -> Value stage
Object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {s :: Stage}. Argument s -> ObjectEntry s
toEntry
  where
    toEntry :: Argument s -> ObjectEntry s
toEntry Argument {Value s
FieldName
Position
argumentPosition :: forall (valid :: Stage). Argument valid -> Position
argumentName :: forall (valid :: Stage). Argument valid -> FieldName
argumentValue :: forall (valid :: Stage). Argument valid -> Value valid
argumentValue :: Value s
argumentName :: FieldName
argumentPosition :: Position
..} = forall (s :: Stage). FieldName -> Value s -> ObjectEntry s
ObjectEntry FieldName
argumentName Value s
argumentValue

-- | Decode GraphQL query arguments and input values
class Decode a where
  decode :: ValidValue -> ResolverState a

instance DecodeKind (KIND a) a => Decode a where
  decode :: ValidValue -> ResolverState a
decode = forall (kind :: DerivingKind) a.
DecodeKind kind a =>
Proxy kind -> ValidValue -> ResolverState a
decodeKind (forall {k} (t :: k). Proxy t
Proxy @(KIND a))

-- | Decode GraphQL type with Specific Kind
class DecodeKind (kind :: DerivingKind) a where
  decodeKind :: Proxy kind -> ValidValue -> ResolverState a

-- SCALAR
instance (DecodeScalar a, GQLType a) => DecodeKind SCALAR a where
  decodeKind :: Proxy SCALAR -> ValidValue -> ResolverState a
decodeKind Proxy SCALAR
_ = forall (m :: * -> *) a.
(Applicative m, MonadError GQLError m) =>
TypeName -> (ScalarValue -> Either Token a) -> ValidValue -> m a
withScalar (TypeData -> TypeName
gqlTypeName forall a b. (a -> b) -> a -> b
$ forall (kinded :: TypeCategory -> * -> *) (kind :: TypeCategory) a.
(GQLType a, CategoryValue kind) =>
kinded kind a -> TypeData
__typeData (forall {k} {k1} (k2 :: k) (a :: k1). KindedProxy k2 a
KindedProxy :: KindedProxy LEAF a)) forall a. DecodeScalar a => ScalarValue -> Either Token a
decodeScalar

-- INPUT_OBJECT and  INPUT_UNION
instance
  ( Generic a,
    GQLType a,
    DecodeRep (Rep a)
  ) =>
  DecodeKind TYPE a
  where
  decodeKind :: Proxy TYPE -> ValidValue -> ResolverState a
decodeKind Proxy TYPE
_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a x. Generic a => Rep a x -> a
to forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
`runReaderT` Context
context) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. DecodeRep f => ValidValue -> DecoderT (f a)
decodeRep
    where
      context :: Context
context =
        Context
          { options :: GQLTypeOptions
options = forall a (f :: * -> *).
GQLType a =>
f a -> GQLTypeOptions -> GQLTypeOptions
typeOptions (forall {k} (t :: k). Proxy t
Proxy @a) GQLTypeOptions
defaultTypeOptions,
            contKind :: Tag
contKind = Tag
D_CONS,
            typeName :: TypeName
typeName = TypeData -> TypeName
gqlTypeName forall a b. (a -> b) -> a -> b
$ forall (kinded :: TypeCategory -> * -> *) (kind :: TypeCategory) a.
(GQLType a, CategoryValue kind) =>
kinded kind a -> TypeData
__typeData (forall {k} {k1} (k2 :: k) (a :: k1). KindedProxy k2 a
KindedProxy :: KindedProxy IN a)
          }

instance (Decode a, DecodeWrapperConstraint f a, DecodeWrapper f) => DecodeKind WRAPPER (f a) where
  decodeKind :: Proxy WRAPPER -> ValidValue -> ResolverState (f a)
decodeKind Proxy WRAPPER
_ ValidValue
value =
    forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (forall (f :: * -> *) (m :: * -> *) a.
(DecodeWrapper f, Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) -> ValidValue -> ExceptT GQLError m (f a)
decodeWrapper forall a. Decode a => ValidValue -> ResolverState a
decode ValidValue
value)
      forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a.
MonadError GQLError m =>
Either GQLError a -> m a
handleEither

instance (Decode a, KnownSymbol name) => DecodeKind CUSTOM (Arg name a) where
  decodeKind :: Proxy CUSTOM -> ValidValue -> ResolverState (Arg name a)
decodeKind Proxy CUSTOM
_ ValidValue
value = forall (name :: Symbol) a. a -> Arg name a
Arg forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
MonadError GQLError m =>
(ValidObject -> m a) -> ValidValue -> m a
withInputObject ValidObject -> ResolverStateT () Identity a
fieldDecoder ValidValue
value
    where
      fieldDecoder :: ValidObject -> ResolverStateT () Identity a
fieldDecoder = forall (m :: * -> *) a.
(ValidValue -> m a) -> FieldName -> ValidObject -> m a
decodeFieldWith forall a. Decode a => ValidValue -> ResolverState a
decode FieldName
fieldName
      fieldName :: FieldName
fieldName = forall (a :: Symbol) (f :: Symbol -> *).
KnownSymbol a =>
f a -> FieldName
symbolName (forall {k} (t :: k). Proxy t
Proxy @name)

--  Map
instance (Ord k, Decode (k, v)) => DecodeKind CUSTOM (Map k v) where
  decodeKind :: Proxy CUSTOM -> ValidValue -> ResolverState (Map k v)
decodeKind Proxy CUSTOM
_ ValidValue
v = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. Decode a => ValidValue -> ResolverState a
decode ValidValue
v :: ResolverState [(k, v)])

-- data Input  =
--    InputHuman Human  -- direct link: { __typename: Human, Human: {field: ""} }
--   | InputRecord { name :: Text, age :: Int } -- { __typename: InputRecord, InputRecord: {field: ""} }
--   | IndexedType Int Text  -- { __typename: InputRecord, _0:2 , _1:""  }
--   | Zeus                 -- { __typename: Zeus }
--     deriving (Generic, GQLType)

decideUnion ::
  ( Functor m,
    MonadError GQLError m
  ) =>
  ([TypeName], value -> m (f1 a)) ->
  ([TypeName], value -> m (f2 a)) ->
  TypeName ->
  value ->
  m ((:+:) f1 f2 a)
decideUnion :: forall (m :: * -> *) value (f1 :: * -> *) a (f2 :: * -> *).
(Functor m, MonadError GQLError m) =>
([TypeName], value -> m (f1 a))
-> ([TypeName], value -> m (f2 a))
-> TypeName
-> value
-> m ((:+:) f1 f2 a)
decideUnion ([TypeName]
left, value -> m (f1 a)
f1) ([TypeName]
right, value -> m (f2 a)
f2) TypeName
name value
value
  | TypeName
name forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [TypeName]
left =
    forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> value -> m (f1 a)
f1 value
value
  | TypeName
name forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [TypeName]
right =
    forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> value -> m (f2 a)
f2 value
value
  | Bool
otherwise =
    forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
      forall a b. (a -> b) -> a -> b
$ GQLError -> GQLError
internal
      forall a b. (a -> b) -> a -> b
$ GQLError
"Constructor \""
        forall a. Semigroup a => a -> a -> a
<> forall a. Msg a => a -> GQLError
msg TypeName
name
        forall a. Semigroup a => a -> a -> a
<> GQLError
"\" could not find in Union"

traverseUnion ::
  (DecodeRep f, DecodeRep g) =>
  ([TypeName], [TypeName]) ->
  TypeName ->
  Object VALID ->
  ValidObject ->
  DecoderT ((f :+: g) a)
traverseUnion :: forall (f :: * -> *) (g :: * -> *) a.
(DecodeRep f, DecodeRep g) =>
([TypeName], [TypeName])
-> TypeName -> ValidObject -> ValidObject -> DecoderT ((:+:) f g a)
traverseUnion ([TypeName]
l1, [TypeName]
r1) TypeName
name ValidObject
unions ValidObject
object
  | [TypeName
name] forall a. Eq a => a -> a -> Bool
== [TypeName]
l1 =
    forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. DecodeRep f => ValidValue -> DecoderT (f a)
decodeRep (forall (stage :: Stage). Object stage -> Value stage
Object ValidObject
object)
  | [TypeName
name] forall a. Eq a => a -> a -> Bool
== [TypeName]
r1 =
    forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. DecodeRep f => ValidValue -> DecoderT (f a)
decodeRep (forall (stage :: Stage). Object stage -> Value stage
Object ValidObject
object)
  | Bool
otherwise = forall (m :: * -> *) value (f1 :: * -> *) a (f2 :: * -> *).
(Functor m, MonadError GQLError m) =>
([TypeName], value -> m (f1 a))
-> ([TypeName], value -> m (f2 a))
-> TypeName
-> value
-> m ((:+:) f1 f2 a)
decideUnion ([TypeName]
l1, forall (f :: * -> *) a. DecodeRep f => ValidValue -> DecoderT (f a)
decodeRep) ([TypeName]
r1, forall (f :: * -> *) a. DecodeRep f => ValidValue -> DecoderT (f a)
decodeRep) TypeName
name (forall (stage :: Stage). Object stage -> Value stage
Object ValidObject
unions)

data Tag = D_CONS | D_UNION deriving (Tag -> Tag -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Tag -> Tag -> Bool
$c/= :: Tag -> Tag -> Bool
== :: Tag -> Tag -> Bool
$c== :: Tag -> Tag -> Bool
Eq, Eq Tag
Tag -> Tag -> Bool
Tag -> Tag -> Ordering
Tag -> Tag -> Tag
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 :: Tag -> Tag -> Tag
$cmin :: Tag -> Tag -> Tag
max :: Tag -> Tag -> Tag
$cmax :: Tag -> Tag -> Tag
>= :: Tag -> Tag -> Bool
$c>= :: Tag -> Tag -> Bool
> :: Tag -> Tag -> Bool
$c> :: Tag -> Tag -> Bool
<= :: Tag -> Tag -> Bool
$c<= :: Tag -> Tag -> Bool
< :: Tag -> Tag -> Bool
$c< :: Tag -> Tag -> Bool
compare :: Tag -> Tag -> Ordering
$ccompare :: Tag -> Tag -> Ordering
Ord)

data Context = Context
  { Context -> Tag
contKind :: Tag,
    Context -> TypeName
typeName :: TypeName,
    Context -> GQLTypeOptions
options :: GQLTypeOptions
  }

data Info = Info
  { Info -> Tag
kind :: Tag,
    Info -> [TypeName]
tagName :: [TypeName]
  }

instance Semigroup Info where
  Info Tag
D_UNION [TypeName]
t1 <> :: Info -> Info -> Info
<> Info Tag
_ [TypeName]
t2 = Tag -> [TypeName] -> Info
Info Tag
D_UNION ([TypeName]
t1 forall a. Semigroup a => a -> a -> a
<> [TypeName]
t2)
  Info Tag
_ [TypeName]
t1 <> Info Tag
D_UNION [TypeName]
t2 = Tag -> [TypeName] -> Info
Info Tag
D_UNION ([TypeName]
t1 forall a. Semigroup a => a -> a -> a
<> [TypeName]
t2)
  Info Tag
D_CONS [TypeName]
t1 <> Info Tag
D_CONS [TypeName]
t2 = Tag -> [TypeName] -> Info
Info Tag
D_CONS ([TypeName]
t1 forall a. Semigroup a => a -> a -> a
<> [TypeName]
t2)

type DecoderT = ReaderT Context ResolverState

withKind :: Tag -> DecoderT a -> DecoderT a
withKind :: forall a. Tag -> DecoderT a -> DecoderT a
withKind Tag
contKind = forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\Context
ctx -> Context
ctx {Tag
contKind :: Tag
contKind :: Tag
contKind})

getUnionInfos ::
  forall f a b.
  (DecodeRep a, DecodeRep b) =>
  f (a :+: b) ->
  DecoderT (Info, Info)
getUnionInfos :: forall (f :: (* -> *) -> *) (a :: * -> *) (b :: * -> *).
(DecodeRep a, DecodeRep b) =>
f (a :+: b) -> DecoderT (Info, Info)
getUnionInfos f (a :+: b)
_ =
  ( \Context
context ->
      ( forall (f :: * -> *). DecodeRep f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @a) Context
context,
        forall (f :: * -> *). DecodeRep f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @b) Context
context
      )
  )
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r (m :: * -> *). MonadReader r m => m r
ask

--
-- GENERICS
--
class DecodeRep (f :: Type -> Type) where
  tags :: Proxy f -> Context -> Info
  decodeRep :: ValidValue -> DecoderT (f a)

instance (Datatype d, DecodeRep f) => DecodeRep (M1 D d f) where
  tags :: Proxy (M1 D d f) -> Context -> Info
tags Proxy (M1 D d f)
_ = forall (f :: * -> *). DecodeRep f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @f)
  decodeRep :: forall a. ValidValue -> DecoderT (M1 D d f a)
decodeRep ValidValue
value = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. DecodeRep f => ValidValue -> DecoderT (f a)
decodeRep ValidValue
value

instance (DecodeRep a, DecodeRep b) => DecodeRep (a :+: b) where
  tags :: Proxy (a :+: b) -> Context -> Info
tags Proxy (a :+: b)
_ = forall (f :: * -> *). DecodeRep f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @a) forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *). DecodeRep f => Proxy f -> Context -> Info
tags (forall {k} (t :: k). Proxy t
Proxy @b)
  decodeRep :: forall a. ValidValue -> DecoderT ((:+:) a b a)
decodeRep (Object ValidObject
obj) =
    do
      (Info
left, Info
right) <- forall (f :: (* -> *) -> *) (a :: * -> *) (b :: * -> *).
(DecodeRep a, DecodeRep b) =>
f (a :+: b) -> DecoderT (Info, Info)
getUnionInfos (forall {k} (t :: k). Proxy t
Proxy @(a :+: b))
      forall a. Tag -> DecoderT a -> DecoderT a
withKind (Info -> Tag
kind (Info
left forall a. Semigroup a => a -> a -> a
<> Info
right)) forall a b. (a -> b) -> a -> b
$
        forall (m :: * -> *) a.
(MonadError GQLError m, Monad m) =>
(TypeName -> ValidObject -> ValidObject -> m a)
-> ValidObject -> m a
withInputUnion
          (forall (f :: * -> *) (g :: * -> *) a.
(DecodeRep f, DecodeRep g) =>
([TypeName], [TypeName])
-> TypeName -> ValidObject -> ValidObject -> DecoderT ((:+:) f g a)
traverseUnion (Info -> [TypeName]
tagName Info
left, Info -> [TypeName]
tagName Info
right))
          ValidObject
obj
  decodeRep (Enum TypeName
name) = do
    (Info
left, Info
right) <- forall (f :: (* -> *) -> *) (a :: * -> *) (b :: * -> *).
(DecodeRep a, DecodeRep b) =>
f (a :+: b) -> DecoderT (Info, Info)
getUnionInfos (forall {k} (t :: k). Proxy t
Proxy @(a :+: b))
    forall (m :: * -> *) value (f1 :: * -> *) a (f2 :: * -> *).
(Functor m, MonadError GQLError m) =>
([TypeName], value -> m (f1 a))
-> ([TypeName], value -> m (f2 a))
-> TypeName
-> value
-> m ((:+:) f1 f2 a)
decideUnion
      (Info -> [TypeName]
tagName Info
left, forall (f :: * -> *) a. DecodeRep f => ValidValue -> DecoderT (f a)
decodeRep)
      (Info -> [TypeName]
tagName Info
right, forall (f :: * -> *) a. DecodeRep f => ValidValue -> DecoderT (f a)
decodeRep)
      TypeName
name
      (forall (stage :: Stage). TypeName -> Value stage
Enum TypeName
name)
  decodeRep ValidValue
_ = forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (GQLError -> GQLError
internal GQLError
"lists and scalars are not allowed in Union")

instance (Constructor c, DecodeFields a) => DecodeRep (M1 C c a) where
  decodeRep :: forall a. ValidValue -> DecoderT (M1 C c a a)
decodeRep = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a.
DecodeFields f =>
Int -> ValidValue -> DecoderT (f a)
decodeFields Int
0
  tags :: Proxy (M1 C c a) -> Context -> Info
tags Proxy (M1 C c a)
_ Context {TypeName
typeName :: TypeName
typeName :: Context -> TypeName
typeName, GQLTypeOptions
options :: GQLTypeOptions
options :: Context -> GQLTypeOptions
options} = Maybe TypeName -> Info
getTag (forall (f :: * -> *). DecodeFields 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 :: Tag
kind = Tag
D_UNION, tagName :: [TypeName]
tagName = [TypeName
memberRef]}
        | Bool
otherwise = Info {kind :: Tag
kind = Tag
D_CONS, tagName :: [TypeName]
tagName = [TypeName
consName]}
      getTag Maybe TypeName
Nothing = Info {kind :: Tag
kind = Tag
D_CONS, tagName :: [TypeName]
tagName = [TypeName
consName]}
      --------
      consName :: TypeName
consName = forall (f :: Meta -> *) (c :: Meta).
Constructor c =>
GQLTypeOptions -> f c -> TypeName
conNameProxy GQLTypeOptions
options (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

class DecodeFields (f :: Type -> Type) where
  refType :: Proxy f -> Maybe TypeName
  countFields :: Proxy f -> Int
  decodeFields :: Int -> ValidValue -> DecoderT (f a)

instance (DecodeFields f, DecodeFields g) => DecodeFields (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 :: * -> *). DecodeFields f => Proxy f -> Int
countFields (forall {k} (t :: k). Proxy t
Proxy @f) forall a. Num a => a -> a -> a
+ forall (f :: * -> *). DecodeFields f => Proxy f -> Int
countFields (forall {k} (t :: k). Proxy t
Proxy @g)
  decodeFields :: forall a. Int -> ValidValue -> DecoderT ((:*:) f g a)
decodeFields Int
index ValidValue
gql =
    forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a.
DecodeFields f =>
Int -> ValidValue -> DecoderT (f a)
decodeFields Int
index ValidValue
gql
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a.
DecodeFields f =>
Int -> ValidValue -> DecoderT (f a)
decodeFields (Int
index forall a. Num a => a -> a -> a
+ forall (f :: * -> *). DecodeFields f => Proxy f -> Int
countFields (forall {k} (t :: k). Proxy t
Proxy @g)) ValidValue
gql

instance (Selector s, GQLType a, Decode a) => DecodeFields (M1 S s (K1 i a)) where
  countFields :: Proxy (M1 S s (K1 i a)) -> Int
countFields Proxy (M1 S s (K1 i a))
_ = Int
1
  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
$ TypeData -> TypeName
gqlTypeName forall a b. (a -> b) -> a -> b
$ forall (kinded :: TypeCategory -> * -> *) (kind :: TypeCategory) a.
(GQLType a, CategoryValue kind) =>
kinded kind a -> TypeData
__typeData (forall {k} {k1} (k2 :: k) (a :: k1). KindedProxy k2 a
KindedProxy :: KindedProxy IN a)
  decodeFields :: forall a. Int -> ValidValue -> DecoderT (M1 S s (K1 i a) a)
decodeFields Int
index ValidValue
value = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k i c (p :: k). c -> K1 i c p
K1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do
    Context {GQLTypeOptions
options :: GQLTypeOptions
options :: Context -> GQLTypeOptions
options, Tag
contKind :: Tag
contKind :: Context -> Tag
contKind} <- forall r (m :: * -> *). MonadReader r m => m r
ask
    case Tag
contKind of
      Tag
D_UNION -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall a. Decode a => ValidValue -> ResolverState a
decode ValidValue
value)
      Tag
D_CONS ->
        let fieldName :: FieldName
fieldName = FieldName -> Int -> FieldName
getFieldName (forall (f :: Meta -> *) (s :: Meta).
Selector s =>
GQLTypeOptions -> f s -> FieldName
selNameProxy GQLTypeOptions
options (forall {k} (t :: k). Proxy t
Proxy @s)) Int
index
            fieldDecoder :: ValidObject -> ReaderT Context (ResolverStateT () Identity) a
fieldDecoder = forall (m :: * -> *) a.
(ValidValue -> m a) -> FieldName -> ValidObject -> m a
decodeFieldWith (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Decode a => ValidValue -> ResolverState a
decode) FieldName
fieldName
         in forall (m :: * -> *) a.
MonadError GQLError m =>
(ValidObject -> m a) -> ValidValue -> m a
withInputObject ValidObject -> ReaderT Context (ResolverStateT () Identity) a
fieldDecoder ValidValue
value

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

instance DecodeFields U1 where
  countFields :: Proxy U1 -> Int
countFields Proxy U1
_ = Int
0
  refType :: Proxy U1 -> Maybe TypeName
refType Proxy U1
_ = forall a. Maybe a
Nothing
  decodeFields :: forall a. Int -> ValidValue -> DecoderT (U1 a)
decodeFields Int
_ ValidValue
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall k (p :: k). U1 p
U1