{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Server.Deriving.Schema
  ( compileTimeSchemaValidation,
    DeriveType,
    deriveImplementsInterface,
    deriveSchema,
    SchemaConstraints,
    SchemaT,
  )
where

-- MORPHEUS

import Control.Applicative (Applicative (..))
import Control.Monad ((>=>), (>>=))
import Data.Functor (($>), (<$>), Functor (..))
import Data.Map (Map)
import Data.Maybe (Maybe (..))
import Data.Morpheus.App.Internal.Resolving
  ( Resolver,
    resultOr,
  )
import Data.Morpheus.Core (defaultConfig, validateSchema)
import Data.Morpheus.Internal.Utils
  ( Failure (..),
  )
import Data.Morpheus.Kind
  ( CUSTOM,
    DerivingKind,
    INTERFACE,
    SCALAR,
    TYPE,
    WRAPPER,
  )
import Data.Morpheus.Server.Deriving.Schema.Internal
  ( KindedType (..),
    TyContentM,
    UpdateDef (..),
    asObjectType,
    builder,
    fromSchema,
    unpackMs,
    updateByContent,
    withObject,
  )
import Data.Morpheus.Server.Deriving.Utils
  ( TypeConstraint (..),
    TypeRep (..),
    toRep,
  )
import Data.Morpheus.Server.Types.GQLType
  ( GQLType (..),
    TypeData (..),
    __typeData,
  )
import Data.Morpheus.Server.Types.SchemaT
  ( SchemaT,
    toSchema,
    withInput,
  )
import Data.Morpheus.Server.Types.Types
  ( Pair,
  )
import Data.Morpheus.Types.GQLScalar
  ( DecodeScalar (..),
    scalarValidator,
  )
import Data.Morpheus.Types.Internal.AST
  ( ArgumentsDefinition,
    CONST,
    CONST,
    FieldContent (..),
    FieldsDefinition,
    GQLErrors,
    IN,
    LEAF,
    MUTATION,
    OBJECT,
    OUT,
    QUERY,
    SUBSCRIPTION,
    Schema (..),
    TRUE,
    TypeCategory,
    TypeContent (..),
    TypeDefinition (..),
    TypeName,
    fieldsToArguments,
  )
import Data.Morpheus.Utils.Kinded
  ( CategoryValue (..),
    KindedProxy (..),
    inputType,
    kinded,
    outputType,
    setKind,
  )
import Data.Proxy (Proxy (..))
import GHC.Generics (Generic, Rep)
import Language.Haskell.TH (Exp, Q)
import Prelude
  ( ($),
    (.),
    Bool (..),
  )

type SchemaConstraints event (m :: * -> *) query mutation subscription =
  ( DeriveTypeConstraintOpt OUT (query (Resolver QUERY event m)),
    DeriveTypeConstraintOpt OUT (mutation (Resolver MUTATION event m)),
    DeriveTypeConstraintOpt OUT (subscription (Resolver SUBSCRIPTION event m))
  )

type DeriveTypeConstraintOpt kind a =
  ( Generic a,
    GQLType a,
    TypeRep (DeriveType kind) (TyContentM kind) (Rep a),
    TypeRep (DeriveType kind) (SchemaT kind ()) (Rep a)
  )

-- | normal morpheus server validates schema at runtime (after the schema derivation).
--   this method allows you to validate it at compile time.
compileTimeSchemaValidation ::
  (SchemaConstraints event m qu mu su) =>
  proxy (root m event qu mu su) ->
  Q Exp
compileTimeSchemaValidation :: proxy (root m event qu mu su) -> Q Exp
compileTimeSchemaValidation =
  Eventless (Schema VALID) -> Q Exp
fromSchema
    (Eventless (Schema VALID) -> Q Exp)
-> (proxy (root m event qu mu su) -> Eventless (Schema VALID))
-> proxy (root m event qu mu su)
-> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (proxy (root m event qu mu su) -> Result () (Schema CONST)
forall k
       (root :: (* -> *)
                -> * -> ((* -> *) -> *) -> ((* -> *) -> *) -> ((* -> *) -> *) -> k)
       (proxy :: k -> *) (m :: * -> *) e (query :: (* -> *) -> *)
       (mut :: (* -> *) -> *) (subs :: (* -> *) -> *) (f :: * -> *).
(SchemaConstraints e m query mut subs, Failure GQLErrors f) =>
proxy (root m e query mut subs) -> f (Schema CONST)
deriveSchema (proxy (root m event qu mu su) -> Result () (Schema CONST))
-> (Schema CONST -> Eventless (Schema VALID))
-> proxy (root m event qu mu su)
-> Eventless (Schema VALID)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Bool -> Config -> Schema CONST -> Eventless (Schema VALID)
forall (s :: Stage).
ValidateSchema s =>
Bool -> Config -> Schema s -> Eventless (Schema VALID)
validateSchema Bool
True Config
defaultConfig)

deriveSchema ::
  forall
    root
    proxy
    m
    e
    query
    mut
    subs
    f.
  ( SchemaConstraints e m query mut subs,
    Failure GQLErrors f
  ) =>
  proxy (root m e query mut subs) ->
  f (Schema CONST)
deriveSchema :: proxy (root m e query mut subs) -> f (Schema CONST)
deriveSchema proxy (root m e query mut subs)
_ = (GQLErrors -> f (Schema CONST))
-> (Schema CONST -> f (Schema CONST))
-> Result () (Schema CONST)
-> f (Schema CONST)
forall a' a e. (GQLErrors -> a') -> (a -> a') -> Result e a -> a'
resultOr GQLErrors -> f (Schema CONST)
forall error (f :: * -> *) v. Failure error f => error -> f v
failure Schema CONST -> f (Schema CONST)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Result () (Schema CONST)
schema
  where
    schema :: Result () (Schema CONST)
schema = SchemaT
  OUT
  (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
   TypeDefinition OBJECT CONST)
-> Result () (Schema CONST)
forall (c :: TypeCategory).
SchemaT
  c
  (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
   TypeDefinition OBJECT CONST)
-> Result () (Schema CONST)
toSchema SchemaT
  OUT
  (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
   TypeDefinition OBJECT CONST)
schemaT
    schemaT ::
      SchemaT
        OUT
        ( TypeDefinition OBJECT CONST,
          TypeDefinition OBJECT CONST,
          TypeDefinition OBJECT CONST
        )
    schemaT :: SchemaT
  OUT
  (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
   TypeDefinition OBJECT CONST)
schemaT =
      (,,)
        (TypeDefinition OBJECT CONST
 -> TypeDefinition OBJECT CONST
 -> TypeDefinition OBJECT CONST
 -> (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
     TypeDefinition OBJECT CONST))
-> SchemaT OUT (TypeDefinition OBJECT CONST)
-> SchemaT
     OUT
     (TypeDefinition OBJECT CONST
      -> TypeDefinition OBJECT CONST
      -> (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
          TypeDefinition OBJECT CONST))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (query (Resolver QUERY e m))
-> SchemaT OUT (TypeDefinition OBJECT CONST)
forall a (f :: * -> *).
DeriveTypeConstraint OUT a =>
f a -> SchemaT OUT (TypeDefinition OBJECT CONST)
deriveObjectType (Proxy (query (Resolver QUERY e m))
forall k (t :: k). Proxy t
Proxy @(query (Resolver QUERY e m)))
        SchemaT
  OUT
  (TypeDefinition OBJECT CONST
   -> TypeDefinition OBJECT CONST
   -> (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
       TypeDefinition OBJECT CONST))
-> SchemaT OUT (TypeDefinition OBJECT CONST)
-> SchemaT
     OUT
     (TypeDefinition OBJECT CONST
      -> (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
          TypeDefinition OBJECT CONST))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Proxy (mut (Resolver MUTATION e m))
-> SchemaT OUT (TypeDefinition OBJECT CONST)
forall a (f :: * -> *).
DeriveTypeConstraint OUT a =>
f a -> SchemaT OUT (TypeDefinition OBJECT CONST)
deriveObjectType (Proxy (mut (Resolver MUTATION e m))
forall k (t :: k). Proxy t
Proxy @(mut (Resolver MUTATION e m)))
        SchemaT
  OUT
  (TypeDefinition OBJECT CONST
   -> (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
       TypeDefinition OBJECT CONST))
-> SchemaT OUT (TypeDefinition OBJECT CONST)
-> SchemaT
     OUT
     (TypeDefinition OBJECT CONST, TypeDefinition OBJECT CONST,
      TypeDefinition OBJECT CONST)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Proxy (subs (Resolver SUBSCRIPTION e m))
-> SchemaT OUT (TypeDefinition OBJECT CONST)
forall a (f :: * -> *).
DeriveTypeConstraint OUT a =>
f a -> SchemaT OUT (TypeDefinition OBJECT CONST)
deriveObjectType (Proxy (subs (Resolver SUBSCRIPTION e m))
forall k (t :: k). Proxy t
Proxy @(subs (Resolver SUBSCRIPTION e m)))

-- |  Generates internal GraphQL Schema for query validation and introspection rendering
class DeriveType (kind :: TypeCategory) (a :: *) where
  deriveType :: f a -> SchemaT kind ()
  deriveContent :: f a -> SchemaT kind (Maybe (FieldContent TRUE kind CONST))

instance (GQLType a, DeriveKindedType cat (KIND a) a) => DeriveType cat a where
  deriveType :: f a -> SchemaT cat ()
deriveType f a
_ = KindedProxy (KIND a) a -> SchemaT cat ()
forall k (cat :: TypeCategory) (kind :: DerivingKind) (a :: k)
       (kinded :: DerivingKind -> k -> *).
DeriveKindedType cat kind a =>
kinded kind a -> SchemaT cat ()
deriveKindedType (KindedProxy (KIND a) a
forall k k (k :: k) (a :: k). KindedProxy k a
KindedProxy :: KindedProxy (KIND a) a)
  deriveContent :: f a -> SchemaT cat (Maybe (FieldContent TRUE cat CONST))
deriveContent f a
_ = KindedProxy (KIND a) a
-> SchemaT cat (Maybe (FieldContent TRUE cat CONST))
forall k (cat :: TypeCategory) (kind :: DerivingKind) (a :: k)
       (kinded :: DerivingKind -> k -> *).
DeriveKindedType cat kind a =>
kinded kind a -> SchemaT cat (Maybe (FieldContent TRUE cat CONST))
deriveKindedContent (KindedProxy (KIND a) a
forall k k (k :: k) (a :: k). KindedProxy k a
KindedProxy :: KindedProxy (KIND a) a)

-- | DeriveType With specific Kind: 'kind': object, scalar, enum ...
class DeriveKindedType (cat :: TypeCategory) (kind :: DerivingKind) a where
  deriveKindedType :: kinded kind a -> SchemaT cat ()
  deriveKindedContent :: kinded kind a -> SchemaT cat (Maybe (FieldContent TRUE cat CONST))
  deriveKindedContent kinded kind a
_ = Maybe (FieldContent TRUE cat CONST)
-> SchemaT cat (Maybe (FieldContent TRUE cat CONST))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (FieldContent TRUE cat CONST)
forall a. Maybe a
Nothing

type DeriveTypeConstraint kind a =
  ( DeriveTypeConstraintOpt kind a,
    CategoryValue kind
  )

-- SCALAR
instance (GQLType a, DeriveType cat a) => DeriveKindedType cat WRAPPER (f a) where
  deriveKindedType :: kinded WRAPPER (f a) -> SchemaT cat ()
deriveKindedType kinded WRAPPER (f a)
_ = KindedProxy cat a -> SchemaT cat ()
forall (kind :: TypeCategory) a (f :: * -> *).
DeriveType kind a =>
f a -> SchemaT kind ()
deriveType (KindedProxy cat a
forall k k (k :: k) (a :: k). KindedProxy k a
KindedProxy :: KindedProxy cat a)

instance (GQLType a, DecodeScalar a) => DeriveKindedType cat SCALAR a where
  deriveKindedType :: kinded SCALAR a -> SchemaT cat ()
deriveKindedType = (KindedProxy LEAF a -> SchemaT cat (TypeContent TRUE LEAF CONST))
-> KindedProxy LEAF a -> SchemaT cat ()
forall a (kind :: TypeCategory) (f :: TypeCategory -> * -> *)
       (c :: TypeCategory).
(GQLType a, CategoryValue kind) =>
(f kind a -> SchemaT c (TypeContent TRUE kind CONST))
-> f kind a -> SchemaT c ()
updateByContent KindedProxy LEAF a -> SchemaT cat (TypeContent TRUE LEAF CONST)
forall k a (f :: k -> * -> *) (k :: k) (cat :: TypeCategory).
DecodeScalar a =>
f k a -> SchemaT cat (TypeContent TRUE LEAF CONST)
deriveScalarContent (KindedProxy LEAF a -> SchemaT cat ())
-> (kinded SCALAR a -> KindedProxy LEAF a)
-> kinded SCALAR a
-> SchemaT cat ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy LEAF -> kinded SCALAR a -> KindedProxy LEAF a
forall k1 k2 (f :: k1 -> *) (k3 :: k1) t (kinded :: t -> k2 -> *)
       (k' :: t) (a :: k2).
f k3 -> kinded k' a -> KindedProxy k3 a
setKind (Proxy LEAF
forall k (t :: k). Proxy t
Proxy @LEAF)

instance DeriveTypeConstraint OUT a => DeriveKindedType OUT INTERFACE a where
  deriveKindedType :: kinded INTERFACE a -> SchemaT OUT ()
deriveKindedType = (KindedProxy OUT a -> SchemaT OUT (TypeContent TRUE OUT CONST))
-> KindedProxy OUT a -> SchemaT OUT ()
forall a (kind :: TypeCategory) (f :: TypeCategory -> * -> *)
       (c :: TypeCategory).
(GQLType a, CategoryValue kind) =>
(f kind a -> SchemaT c (TypeContent TRUE kind CONST))
-> f kind a -> SchemaT c ()
updateByContent KindedProxy OUT a -> SchemaT OUT (TypeContent TRUE OUT CONST)
forall a (f :: * -> *).
DeriveTypeConstraint OUT a =>
f a -> SchemaT OUT (TypeContent TRUE OUT CONST)
deriveInterfaceContent (KindedProxy OUT a -> SchemaT OUT ())
-> (kinded INTERFACE a -> KindedProxy OUT a)
-> kinded INTERFACE a
-> SchemaT OUT ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy OUT -> kinded INTERFACE a -> KindedProxy OUT a
forall k1 k2 (f :: k1 -> *) (k3 :: k1) t (kinded :: t -> k2 -> *)
       (k' :: t) (a :: k2).
f k3 -> kinded k' a -> KindedProxy k3 a
setKind (Proxy OUT
forall k (t :: k). Proxy t
Proxy @OUT)

instance DeriveTypeConstraint OUT a => DeriveKindedType OUT TYPE a where
  deriveKindedType :: kinded TYPE a -> SchemaT OUT ()
deriveKindedType = kinded TYPE a -> SchemaT OUT ()
forall a (f :: * -> *).
DeriveTypeConstraint OUT a =>
f a -> SchemaT OUT ()
deriveOutputType

instance DeriveTypeConstraint IN a => DeriveKindedType IN TYPE a where
  deriveKindedType :: kinded TYPE a -> SchemaT IN ()
deriveKindedType = kinded TYPE a -> SchemaT IN ()
forall a (f :: * -> *).
DeriveTypeConstraint IN a =>
f a -> SchemaT IN ()
deriveInputType

instance DeriveType cat a => DeriveKindedType cat CUSTOM (Resolver o e m a) where
  deriveKindedType :: kinded CUSTOM (Resolver o e m a) -> SchemaT cat ()
deriveKindedType kinded CUSTOM (Resolver o e m a)
_ = Proxy a -> SchemaT cat ()
forall (kind :: TypeCategory) a (f :: * -> *).
DeriveType kind a =>
f a -> SchemaT kind ()
deriveType (Proxy a
forall k (t :: k). Proxy t
Proxy @a)

-- Tuple
instance DeriveType cat (Pair k v) => DeriveKindedType cat CUSTOM (k, v) where
  deriveKindedType :: kinded CUSTOM (k, v) -> SchemaT cat ()
deriveKindedType kinded CUSTOM (k, v)
_ = Proxy (Pair k v) -> SchemaT cat ()
forall (kind :: TypeCategory) a (f :: * -> *).
DeriveType kind a =>
f a -> SchemaT kind ()
deriveType (Proxy (Pair k v)
forall k (t :: k). Proxy t
Proxy @(Pair k v))

-- Map
instance DeriveType cat [Pair k v] => DeriveKindedType cat CUSTOM (Map k v) where
  deriveKindedType :: kinded CUSTOM (Map k v) -> SchemaT cat ()
deriveKindedType kinded CUSTOM (Map k v)
_ = Proxy [Pair k v] -> SchemaT cat ()
forall (kind :: TypeCategory) a (f :: * -> *).
DeriveType kind a =>
f a -> SchemaT kind ()
deriveType (Proxy [Pair k v]
forall k (t :: k). Proxy t
Proxy @[Pair k v])

instance
  ( GQLType b,
    DeriveType OUT b,
    DeriveTypeConstraint IN a
  ) =>
  DeriveKindedType OUT CUSTOM (a -> m b)
  where
  deriveKindedContent :: kinded CUSTOM (a -> m b)
-> SchemaT OUT (Maybe (FieldContent TRUE OUT CONST))
deriveKindedContent kinded CUSTOM (a -> m b)
_ = FieldContent TRUE OUT CONST -> Maybe (FieldContent TRUE OUT CONST)
forall a. a -> Maybe a
Just (FieldContent TRUE OUT CONST
 -> Maybe (FieldContent TRUE OUT CONST))
-> (ArgumentsDefinition CONST -> FieldContent TRUE OUT CONST)
-> ArgumentsDefinition CONST
-> Maybe (FieldContent TRUE OUT CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ArgumentsDefinition CONST -> FieldContent TRUE OUT CONST
forall (s :: Stage) (cat :: TypeCategory).
ArgumentsDefinition s -> FieldContent (OUT <=? cat) cat s
FieldArgs (ArgumentsDefinition CONST -> Maybe (FieldContent TRUE OUT CONST))
-> SchemaT OUT (ArgumentsDefinition CONST)
-> SchemaT OUT (Maybe (FieldContent TRUE OUT CONST))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy a -> SchemaT OUT (ArgumentsDefinition CONST)
forall a (f :: * -> *).
DeriveTypeConstraint IN a =>
f a -> SchemaT OUT (ArgumentsDefinition CONST)
deriveArgumentDefinition (Proxy a
forall k (t :: k). Proxy t
Proxy @a)
  deriveKindedType :: kinded CUSTOM (a -> m b) -> SchemaT OUT ()
deriveKindedType kinded CUSTOM (a -> m b)
_ = KindedType OUT b -> SchemaT OUT ()
forall (kind :: TypeCategory) a (f :: * -> *).
DeriveType kind a =>
f a -> SchemaT kind ()
deriveType (Proxy b -> KindedType OUT b
forall k (f :: k -> *) (a :: k). f a -> KindedType OUT a
outputType (Proxy b -> KindedType OUT b) -> Proxy b -> KindedType OUT b
forall a b. (a -> b) -> a -> b
$ Proxy b
forall k (t :: k). Proxy t
Proxy @b)

deriveScalarContent :: (DecodeScalar a) => f k a -> SchemaT cat (TypeContent TRUE LEAF CONST)
deriveScalarContent :: f k a -> SchemaT cat (TypeContent TRUE LEAF CONST)
deriveScalarContent = TypeContent TRUE LEAF CONST
-> SchemaT cat (TypeContent TRUE LEAF CONST)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TypeContent TRUE LEAF CONST
 -> SchemaT cat (TypeContent TRUE LEAF CONST))
-> (f k a -> TypeContent TRUE LEAF CONST)
-> f k a
-> SchemaT cat (TypeContent TRUE LEAF CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScalarDefinition -> TypeContent TRUE LEAF CONST
forall (a :: TypeCategory) (s :: Stage).
ScalarDefinition -> TypeContent (LEAF <=? a) a s
DataScalar (ScalarDefinition -> TypeContent TRUE LEAF CONST)
-> (f k a -> ScalarDefinition)
-> f k a
-> TypeContent TRUE LEAF CONST
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f k a -> ScalarDefinition
forall (f :: * -> *) a. DecodeScalar a => f a -> ScalarDefinition
scalarValidator

deriveInterfaceContent :: DeriveTypeConstraint OUT a => f a -> SchemaT OUT (TypeContent TRUE OUT CONST)
deriveInterfaceContent :: f a -> SchemaT OUT (TypeContent TRUE OUT CONST)
deriveInterfaceContent = (FieldsDefinition OUT CONST -> TypeContent TRUE OUT CONST)
-> SchemaT OUT (FieldsDefinition OUT CONST)
-> SchemaT OUT (TypeContent TRUE OUT CONST)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FieldsDefinition OUT CONST -> TypeContent TRUE OUT CONST
forall (s :: Stage) (a :: TypeCategory).
FieldsDefinition OUT s -> TypeContent (IMPLEMENTABLE <=? a) a s
DataInterface (SchemaT OUT (FieldsDefinition OUT CONST)
 -> SchemaT OUT (TypeContent TRUE OUT CONST))
-> (f a -> SchemaT OUT (FieldsDefinition OUT CONST))
-> f a
-> SchemaT OUT (TypeContent TRUE OUT CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindedType OUT a -> SchemaT OUT (FieldsDefinition OUT CONST)
forall (kind :: TypeCategory) a.
DeriveTypeConstraint kind a =>
KindedType kind a -> SchemaT kind (FieldsDefinition kind CONST)
deriveFields (KindedType OUT a -> SchemaT OUT (FieldsDefinition OUT CONST))
-> (f a -> KindedType OUT a)
-> f a
-> SchemaT OUT (FieldsDefinition OUT CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> KindedType OUT a
forall k (f :: k -> *) (a :: k). f a -> KindedType OUT a
outputType

deriveArgumentDefinition :: DeriveTypeConstraint IN a => f a -> SchemaT OUT (ArgumentsDefinition CONST)
deriveArgumentDefinition :: f a -> SchemaT OUT (ArgumentsDefinition CONST)
deriveArgumentDefinition = SchemaT IN (ArgumentsDefinition CONST)
-> SchemaT OUT (ArgumentsDefinition CONST)
forall a. SchemaT IN a -> SchemaT OUT a
withInput (SchemaT IN (ArgumentsDefinition CONST)
 -> SchemaT OUT (ArgumentsDefinition CONST))
-> (f a -> SchemaT IN (ArgumentsDefinition CONST))
-> f a
-> SchemaT OUT (ArgumentsDefinition CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FieldsDefinition IN CONST -> ArgumentsDefinition CONST)
-> SchemaT IN (FieldsDefinition IN CONST)
-> SchemaT IN (ArgumentsDefinition CONST)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FieldsDefinition IN CONST -> ArgumentsDefinition CONST
forall (s :: Stage). FieldsDefinition IN s -> ArgumentsDefinition s
fieldsToArguments (SchemaT IN (FieldsDefinition IN CONST)
 -> SchemaT IN (ArgumentsDefinition CONST))
-> (f a -> SchemaT IN (FieldsDefinition IN CONST))
-> f a
-> SchemaT IN (ArgumentsDefinition CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindedType IN a -> SchemaT IN (FieldsDefinition IN CONST)
forall (kind :: TypeCategory) a.
DeriveTypeConstraint kind a =>
KindedType kind a -> SchemaT kind (FieldsDefinition kind CONST)
deriveFields (KindedType IN a -> SchemaT IN (FieldsDefinition IN CONST))
-> (f a -> KindedType IN a)
-> f a
-> SchemaT IN (FieldsDefinition IN CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> KindedType IN a
forall k (f :: k -> *) (a :: k). f a -> KindedType IN a
inputType

deriveFields :: DeriveTypeConstraint kind a => KindedType kind a -> SchemaT kind (FieldsDefinition kind CONST)
deriveFields :: KindedType kind a -> SchemaT kind (FieldsDefinition kind CONST)
deriveFields KindedType kind a
kindedType = KindedType kind a -> SchemaT kind (TypeContent TRUE kind CONST)
forall (kind :: TypeCategory) a.
DeriveTypeConstraint kind a =>
KindedType kind a -> SchemaT kind (TypeContent TRUE kind CONST)
deriveTypeContent KindedType kind a
kindedType SchemaT kind (TypeContent TRUE kind CONST)
-> (TypeContent TRUE kind CONST
    -> SchemaT kind (FieldsDefinition kind CONST))
-> SchemaT kind (FieldsDefinition kind CONST)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= KindedType kind a
-> TypeContent TRUE kind CONST
-> SchemaT kind (FieldsDefinition kind CONST)
forall a (c :: TypeCategory) (any :: TypeCategory) (s :: Stage).
(GQLType a, CategoryValue c) =>
KindedType c a
-> TypeContent TRUE any s -> SchemaT c (FieldsDefinition c s)
withObject KindedType kind a
kindedType

deriveInputType :: DeriveTypeConstraint IN a => f a -> SchemaT IN ()
deriveInputType :: f a -> SchemaT IN ()
deriveInputType = (KindedType IN a -> SchemaT IN (TypeContent TRUE IN CONST))
-> KindedType IN a -> SchemaT IN ()
forall a (kind :: TypeCategory) (f :: TypeCategory -> * -> *)
       (c :: TypeCategory).
(GQLType a, CategoryValue kind) =>
(f kind a -> SchemaT c (TypeContent TRUE kind CONST))
-> f kind a -> SchemaT c ()
updateByContent KindedType IN a -> SchemaT IN (TypeContent TRUE IN CONST)
forall (kind :: TypeCategory) a.
DeriveTypeConstraint kind a =>
KindedType kind a -> SchemaT kind (TypeContent TRUE kind CONST)
deriveTypeContent (KindedType IN a -> SchemaT IN ())
-> (f a -> KindedType IN a) -> f a -> SchemaT IN ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> KindedType IN a
forall k (f :: k -> *) (a :: k). f a -> KindedType IN a
inputType

deriveOutputType :: DeriveTypeConstraint OUT a => f a -> SchemaT OUT ()
deriveOutputType :: f a -> SchemaT OUT ()
deriveOutputType = (KindedType OUT a -> SchemaT OUT (TypeContent TRUE OUT CONST))
-> KindedType OUT a -> SchemaT OUT ()
forall a (kind :: TypeCategory) (f :: TypeCategory -> * -> *)
       (c :: TypeCategory).
(GQLType a, CategoryValue kind) =>
(f kind a -> SchemaT c (TypeContent TRUE kind CONST))
-> f kind a -> SchemaT c ()
updateByContent KindedType OUT a -> SchemaT OUT (TypeContent TRUE OUT CONST)
forall (kind :: TypeCategory) a.
DeriveTypeConstraint kind a =>
KindedType kind a -> SchemaT kind (TypeContent TRUE kind CONST)
deriveTypeContent (KindedType OUT a -> SchemaT OUT ())
-> (f a -> KindedType OUT a) -> f a -> SchemaT OUT ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> KindedType OUT a
forall k (f :: k -> *) (a :: k). f a -> KindedType OUT a
outputType

deriveObjectType :: DeriveTypeConstraint OUT a => f a -> SchemaT OUT (TypeDefinition OBJECT CONST)
deriveObjectType :: f a -> SchemaT OUT (TypeDefinition OBJECT CONST)
deriveObjectType = (f a -> SchemaT OUT (FieldsDefinition OUT CONST))
-> f a -> SchemaT OUT (TypeDefinition OBJECT CONST)
forall a (f2 :: * -> *) (c :: TypeCategory).
GQLType a =>
(f2 a -> SchemaT c (FieldsDefinition OUT CONST))
-> f2 a -> SchemaT c (TypeDefinition OBJECT CONST)
asObjectType (KindedType OUT a -> SchemaT OUT (FieldsDefinition OUT CONST)
forall (kind :: TypeCategory) a.
DeriveTypeConstraint kind a =>
KindedType kind a -> SchemaT kind (FieldsDefinition kind CONST)
deriveFields (KindedType OUT a -> SchemaT OUT (FieldsDefinition OUT CONST))
-> (f a -> KindedType OUT a)
-> f a
-> SchemaT OUT (FieldsDefinition OUT CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> KindedType OUT a
forall k (f :: k -> *) (a :: k). f a -> KindedType OUT a
outputType)

deriveImplementsInterface :: (GQLType a, DeriveType OUT a) => f a -> SchemaT OUT TypeName
deriveImplementsInterface :: f a -> SchemaT OUT TypeName
deriveImplementsInterface f a
x = KindedType OUT a -> SchemaT OUT ()
forall (kind :: TypeCategory) a (f :: * -> *).
DeriveType kind a =>
f a -> SchemaT kind ()
deriveType (f a -> KindedType OUT a
forall k (f :: k -> *) (a :: k). f a -> KindedType OUT a
outputType f a
x) SchemaT OUT () -> TypeName -> SchemaT OUT TypeName
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> TypeData -> TypeName
gqlTypeName (KindedProxy OUT a -> TypeData
forall (kinded :: TypeCategory -> * -> *) (kind :: TypeCategory) a.
(GQLType a, CategoryValue kind) =>
kinded kind a -> TypeData
__typeData (Proxy OUT -> f a -> KindedProxy OUT a
forall k1 k2 (f :: k1 -> *) (k3 :: k1) (f' :: k2 -> *) (a :: k2).
f k3 -> f' a -> KindedProxy k3 a
kinded (Proxy OUT
forall k (t :: k). Proxy t
Proxy @OUT) f a
x))

fieldContentConstraint :: f kind a -> TypeConstraint (DeriveType kind) (TyContentM kind) Proxy
fieldContentConstraint :: f kind a
-> TypeConstraint (DeriveType kind) (TyContentM kind) Proxy
fieldContentConstraint f kind a
_ = (forall a. DeriveType kind a => Proxy a -> TyContentM kind)
-> TypeConstraint (DeriveType kind) (TyContentM kind) Proxy
forall (c :: * -> Constraint) v (f :: * -> *).
(forall a. c a => f a -> v) -> TypeConstraint c v f
TypeConstraint forall a. DeriveType kind a => Proxy a -> TyContentM kind
forall (f :: * -> *) (kind :: TypeCategory) a.
DeriveType kind a =>
f a -> TyContentM kind
deriveFieldContent

deriveFieldContent :: forall f kind a. (DeriveType kind a) => f a -> TyContentM kind
deriveFieldContent :: f a -> TyContentM kind
deriveFieldContent f a
_ = KindedProxy kind a -> SchemaT kind ()
forall (kind :: TypeCategory) a (f :: * -> *).
DeriveType kind a =>
f a -> SchemaT kind ()
deriveType KindedProxy kind a
kindedProxy SchemaT kind () -> TyContentM kind -> TyContentM kind
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> KindedProxy kind a -> TyContentM kind
forall (kind :: TypeCategory) a (f :: * -> *).
DeriveType kind a =>
f a -> SchemaT kind (Maybe (FieldContent TRUE kind CONST))
deriveContent KindedProxy kind a
kindedProxy
  where
    kindedProxy :: KindedProxy kind a
    kindedProxy :: KindedProxy kind a
kindedProxy = KindedProxy kind a
forall k k (k :: k) (a :: k). KindedProxy k a
KindedProxy

deriveTypeContent ::
  forall kind a.
  DeriveTypeConstraint kind a =>
  KindedType kind a ->
  SchemaT kind (TypeContent TRUE kind CONST)
deriveTypeContent :: KindedType kind a -> SchemaT kind (TypeContent TRUE kind CONST)
deriveTypeContent KindedType kind a
kindedProxy =
  [ConsRep (TyContentM kind)]
-> SchemaT kind [ConsRep (TyContent kind)]
forall (k :: TypeCategory).
[ConsRep (TyContentM k)] -> SchemaT k [ConsRep (TyContent k)]
unpackMs (TypeConstraint (DeriveType kind) (TyContentM kind) Proxy
-> KindedType kind a -> [ConsRep (TyContentM kind)]
forall (kinded :: TypeCategory -> * -> *)
       (constraint :: * -> Constraint) value a (kind :: TypeCategory).
(GQLType a, CategoryValue kind,
 TypeRep constraint value (Rep a)) =>
TypeConstraint constraint value Proxy
-> kinded kind a -> [ConsRep value]
toRep (KindedType kind a
-> TypeConstraint (DeriveType kind) (TyContentM kind) Proxy
forall k (f :: TypeCategory -> k -> *) (kind :: TypeCategory)
       (a :: k).
f kind a
-> TypeConstraint (DeriveType kind) (TyContentM kind) Proxy
fieldContentConstraint KindedType kind a
kindedProxy) KindedType kind a
kindedProxy)
    SchemaT kind [ConsRep (TyContent kind)]
-> ([ConsRep (TyContent kind)]
    -> SchemaT kind (TypeContent TRUE kind CONST))
-> SchemaT kind (TypeContent TRUE kind CONST)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TypeContent TRUE kind CONST -> TypeContent TRUE kind CONST)
-> SchemaT kind (TypeContent TRUE kind CONST)
-> SchemaT kind (TypeContent TRUE kind CONST)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (KindedType kind a
-> TypeContent TRUE kind CONST -> TypeContent TRUE kind CONST
forall value a (f :: * -> *).
(UpdateDef value, GQLType a) =>
f a -> value -> value
updateDef KindedType kind a
kindedProxy) (SchemaT kind (TypeContent TRUE kind CONST)
 -> SchemaT kind (TypeContent TRUE kind CONST))
-> ([ConsRep (TyContent kind)]
    -> SchemaT kind (TypeContent TRUE kind CONST))
-> [ConsRep (TyContent kind)]
-> SchemaT kind (TypeContent TRUE kind CONST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindedType kind a
-> [ConsRep (TyContent kind)]
-> SchemaT kind (TypeContent TRUE kind CONST)
forall a (kind :: TypeCategory) (cat :: TypeCategory).
(GQLType a, CategoryValue kind) =>
KindedType kind a
-> [ConsRep (TyContent kind)]
-> SchemaT cat (TypeContent TRUE kind CONST)
builder KindedType kind a
kindedProxy