{-# 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.RequestType (declareRequestType)
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 <- forall a. IO a -> Q a
runIO IO SchemaSource
schemaSrc
let request :: GQLRequest
request =
GQLRequest
{ ExecutableSource
query :: ExecutableSource
query :: ExecutableSource
query,
operationName :: Maybe FieldName
operationName = forall a. Maybe a
Nothing,
variables :: Maybe Value
variables = forall a. Maybe a
Nothing
}
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) ->
forall a. Semigroup a => a -> a -> a
(<>)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ExecutableSource -> FetchDefinition -> Q [Dec]
declareRequestType ExecutableSource
query FetchDefinition
fetch
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 = forall t a. GQLResult t -> (t -> Q a) -> Q a
handleResult ((TypeName -> Bool) -> Schema VALID -> [ClientTypeDefinition]
toGlobalDefinitions TypeName -> Bool
f 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 (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 (forall a b. a -> b -> a
const Bool
True)
declareGlobalTypes ::
FilePath
-> Q [Dec]
declareGlobalTypes :: FilePath -> Q [Dec]
declareGlobalTypes = forall a b c. (a -> b -> c) -> b -> a -> c
flip FilePath -> Maybe FilePath -> Q [Dec]
declareClientTypes 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 (forall a. Ord a => a -> Set a -> Bool
`member` 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 (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 (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 <- 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