{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Data.Morpheus.Server.TH.Compile
  ( compileDocument,
    gqlDocument,
  )
where

--
--  Morpheus

import qualified Data.ByteString.Lazy.Char8 as LB
import Data.ByteString.Lazy.Char8 (ByteString)
import Data.Morpheus.CodeGen
  ( parseServerTypeDefinitions,
  )
import Data.Morpheus.CodeGen.Internal.AST
  ( CodeGenConfig (..),
  )
import Data.Morpheus.Server.TH.Declare
  ( runDeclare,
  )
import Language.Haskell.TH
import Language.Haskell.TH.Quote (QuasiQuoter (..))
import Relude hiding (ByteString)

gqlDocument :: QuasiQuoter
gqlDocument :: QuasiQuoter
gqlDocument = CodeGenConfig -> QuasiQuoter
mkQuasiQuoter CodeGenConfig {namespace :: Bool
namespace = Bool
False}

mkQuasiQuoter :: CodeGenConfig -> QuasiQuoter
mkQuasiQuoter :: CodeGenConfig -> QuasiQuoter
mkQuasiQuoter CodeGenConfig
ctx =
  QuasiQuoter
    { quoteExp :: String -> Q Exp
quoteExp = forall {a}. Text -> a
notHandled Text
"Expressions",
      quotePat :: String -> Q Pat
quotePat = forall {a}. Text -> a
notHandled Text
"Patterns",
      quoteType :: String -> Q Type
quoteType = forall {a}. Text -> a
notHandled Text
"Types",
      quoteDec :: String -> Q [Dec]
quoteDec = CodeGenConfig -> ByteString -> Q [Dec]
compileDocument CodeGenConfig
ctx forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
LB.pack
    }
  where
    notHandled :: Text -> a
notHandled Text
things =
      forall a t. (HasCallStack, IsText t) => t -> a
error forall a b. (a -> b) -> a -> b
$ Text
things forall a. Semigroup a => a -> a -> a
<> Text
" are not supported by the GraphQL QuasiQuoter"

compileDocument :: CodeGenConfig -> ByteString -> Q [Dec]
compileDocument :: CodeGenConfig -> ByteString -> Q [Dec]
compileDocument CodeGenConfig
ctx = forall (m :: * -> *).
CodeGenMonad m =>
CodeGenConfig -> ByteString -> m [ServerTypeDefinition]
parseServerTypeDefinitions CodeGenConfig
ctx forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Declare a => CodeGenConfig -> a -> Q [Dec]
runDeclare CodeGenConfig
ctx