{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Morpheus.Client.Declare
( declareGlobalTypes,
declareGlobalTypesByName,
declareLocalTypes,
declareLocalTypesInline,
internalLegacyLocalDeclareTypes,
clientTypeDeclarations,
raw,
)
where
import Data.Morpheus.Client.Declare.Client
( declareTypes,
)
import Data.Morpheus.Client.Declare.Fetch
import Data.Morpheus.Client.Internal.Types
( ExecutableSource,
SchemaSource,
)
import Data.Morpheus.Client.Internal.Utils (getFile, getSource, handleResult)
import Data.Morpheus.Client.QuasiQuoter (raw)
import Data.Morpheus.Client.Schema.Parse (parseSchema)
import Data.Morpheus.Client.Transform
( toGlobalDefinitions,
toLocalDefinitions,
)
import Data.Morpheus.CodeGen.Internal.AST
import Data.Morpheus.Core (parseRequest)
import Data.Morpheus.Types.IO (GQLRequest (..))
import Data.Set
import qualified Data.Set as S
import Language.Haskell.TH (Dec, Q, runIO)
import Relude
internalLegacyLocalDeclareTypes :: IO SchemaSource -> ExecutableSource -> Q [Dec]
internalLegacyLocalDeclareTypes :: IO SchemaSource -> ExecutableSource -> Q [Dec]
internalLegacyLocalDeclareTypes IO SchemaSource
schemaSrc ExecutableSource
query = do
SchemaSource
schemaText <- IO SchemaSource -> Q SchemaSource
forall a. IO a -> Q a
runIO IO SchemaSource
schemaSrc
let request :: GQLRequest
request =
GQLRequest :: Maybe FieldName -> ExecutableSource -> Maybe Value -> GQLRequest
GQLRequest
{ ExecutableSource
query :: ExecutableSource
query :: ExecutableSource
query,
operationName :: Maybe FieldName
operationName = Maybe FieldName
forall a. Maybe a
Nothing,
variables :: Maybe Value
variables = Maybe Value
forall a. Maybe a
Nothing
}
GQLResult (FetchDefinition, [ClientTypeDefinition])
-> ((FetchDefinition, [ClientTypeDefinition]) -> Q [Dec])
-> Q [Dec]
forall t a. GQLResult t -> (t -> Q a) -> Q a
handleResult
( do
Schema VALID
schemaDoc <- SchemaSource -> GQLResult (Schema VALID)
parseSchema SchemaSource
schemaText
ExecutableDocument
executableDoc <- GQLRequest -> GQLResult ExecutableDocument
parseRequest GQLRequest
request
ExecutableDocument
-> Schema VALID
-> GQLResult (FetchDefinition, [ClientTypeDefinition])
toLocalDefinitions ExecutableDocument
executableDoc Schema VALID
schemaDoc
)
( \(FetchDefinition
fetch, [ClientTypeDefinition]
types) ->
[Dec] -> [Dec] -> [Dec]
forall a. Semigroup a => a -> a -> a
(<>)
([Dec] -> [Dec] -> [Dec]) -> Q [Dec] -> Q ([Dec] -> [Dec])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ExecutableSource -> FetchDefinition -> Q [Dec]
declareFetch ExecutableSource
query FetchDefinition
fetch
Q ([Dec] -> [Dec]) -> Q [Dec] -> Q [Dec]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [ClientTypeDefinition] -> Q [Dec]
declareTypes [ClientTypeDefinition]
types
)
globalTypeDeclarations :: SchemaSource -> (TypeName -> Bool) -> Q [Dec]
globalTypeDeclarations :: SchemaSource -> (TypeName -> Bool) -> Q [Dec]
globalTypeDeclarations SchemaSource
src TypeName -> Bool
f = GQLResult [ClientTypeDefinition]
-> ([ClientTypeDefinition] -> Q [Dec]) -> Q [Dec]
forall t a. GQLResult t -> (t -> Q a) -> Q a
handleResult ((TypeName -> Bool) -> Schema VALID -> [ClientTypeDefinition]
toGlobalDefinitions TypeName -> Bool
f (Schema VALID -> [ClientTypeDefinition])
-> GQLResult (Schema VALID) -> GQLResult [ClientTypeDefinition]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SchemaSource -> GQLResult (Schema VALID)
parseSchema SchemaSource
src) [ClientTypeDefinition] -> Q [Dec]
declareTypes
clientTypeDeclarations ::
SchemaSource ->
Maybe ExecutableSource ->
Q [Dec]
clientTypeDeclarations :: SchemaSource -> Maybe ExecutableSource -> Q [Dec]
clientTypeDeclarations SchemaSource
src (Just ExecutableSource
doc) = IO SchemaSource -> ExecutableSource -> Q [Dec]
internalLegacyLocalDeclareTypes (SchemaSource -> IO SchemaSource
forall (f :: * -> *) a. Applicative f => a -> f a
pure SchemaSource
src) ExecutableSource
doc
clientTypeDeclarations SchemaSource
src Maybe ExecutableSource
Nothing = SchemaSource -> (TypeName -> Bool) -> Q [Dec]
globalTypeDeclarations SchemaSource
src (Bool -> TypeName -> Bool
forall a b. a -> b -> a
const Bool
True)
declareGlobalTypes ::
FilePath
-> Q [Dec]
declareGlobalTypes :: FilePath -> Q [Dec]
declareGlobalTypes = (FilePath -> Maybe FilePath -> Q [Dec])
-> Maybe FilePath -> FilePath -> Q [Dec]
forall a b c. (a -> b -> c) -> b -> a -> c
flip FilePath -> Maybe FilePath -> Q [Dec]
declareClientTypes Maybe FilePath
forall a. Maybe a
Nothing
declareGlobalTypesByName :: FilePath -> [TypeName] -> Q [Dec]
declareGlobalTypesByName :: FilePath -> [TypeName] -> Q [Dec]
declareGlobalTypesByName FilePath
path [TypeName]
names = do
SchemaSource
schema <- FilePath -> Q SchemaSource
getSource FilePath
path
SchemaSource -> (TypeName -> Bool) -> Q [Dec]
globalTypeDeclarations SchemaSource
schema (TypeName -> Set TypeName -> Bool
forall a. Ord a => a -> Set a -> Bool
`member` [TypeName] -> Set TypeName
forall a. Ord a => [a] -> Set a
S.fromList [TypeName]
names)
declareLocalTypes ::
FilePath
-> FilePath
-> Q [Dec]
declareLocalTypes :: FilePath -> FilePath -> Q [Dec]
declareLocalTypes FilePath
schema FilePath
query = FilePath -> Maybe FilePath -> Q [Dec]
declareClientTypes FilePath
schema (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
query)
declareLocalTypesInline ::
FilePath
-> ExecutableSource
-> Q [Dec]
declareLocalTypesInline :: FilePath -> ExecutableSource -> Q [Dec]
declareLocalTypesInline FilePath
schemaPath ExecutableSource
query = do
SchemaSource
schema <- FilePath -> Q SchemaSource
getSource FilePath
schemaPath
SchemaSource -> Maybe ExecutableSource -> Q [Dec]
clientTypeDeclarations SchemaSource
schema (ExecutableSource -> Maybe ExecutableSource
forall a. a -> Maybe a
Just ExecutableSource
query)
declareClientTypes ::
FilePath ->
Maybe FilePath ->
Q [Dec]
declareClientTypes :: FilePath -> Maybe FilePath -> Q [Dec]
declareClientTypes FilePath
schemaPath Maybe FilePath
queryPath = do
SchemaSource
schema <- FilePath -> Q SchemaSource
getSource FilePath
schemaPath
Maybe ExecutableSource
query <- (FilePath -> Q ExecutableSource)
-> Maybe FilePath -> Q (Maybe ExecutableSource)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse FilePath -> Q ExecutableSource
getFile Maybe FilePath
queryPath
SchemaSource -> Maybe ExecutableSource -> Q [Dec]
clientTypeDeclarations SchemaSource
schema Maybe ExecutableSource
query