{-# LANGUAGE OverloadedStrings #-}
module Language.GraphQL.Execute
( execute
, module Language.GraphQL.Execute.Coerce
) where
import Control.Monad.Catch (MonadCatch)
import Data.HashMap.Strict (HashMap)
import Data.Sequence (Seq(..))
import Data.Text (Text)
import Language.GraphQL.AST.Document (Document, Name)
import Language.GraphQL.Execute.Coerce
import Language.GraphQL.Execute.Execution
import qualified Language.GraphQL.Execute.Transform as Transform
import qualified Language.GraphQL.Execute.Subscribe as Subscribe
import Language.GraphQL.Error
import qualified Language.GraphQL.Type.Definition as Definition
import qualified Language.GraphQL.Type.Out as Out
import Language.GraphQL.Type.Schema
execute :: (MonadCatch m, VariableValue a, Serialize b)
=> Schema m
-> Maybe Text
-> HashMap Name a
-> Document
-> m (Either (ResponseEventStream m b) (Response b))
execute :: Schema m
-> Maybe Text
-> HashMap Text a
-> Document
-> m (Either (ResponseEventStream m b) (Response b))
execute Schema m
schema' Maybe Text
operationName HashMap Text a
subs Document
document =
case Schema m
-> Maybe Text
-> HashMap Text a
-> Document
-> Either QueryError (Document m)
forall a (m :: * -> *).
VariableValue a =>
Schema m
-> Maybe Text
-> HashMap Text a
-> Document
-> Either QueryError (Document m)
Transform.document Schema m
schema' Maybe Text
operationName HashMap Text a
subs Document
document of
Left QueryError
queryError -> Either (ResponseEventStream m b) (Response b)
-> m (Either (ResponseEventStream m b) (Response b))
forall (f :: * -> *) a. Applicative f => a -> f a
pure
(Either (ResponseEventStream m b) (Response b)
-> m (Either (ResponseEventStream m b) (Response b)))
-> Either (ResponseEventStream m b) (Response b)
-> m (Either (ResponseEventStream m b) (Response b))
forall a b. (a -> b) -> a -> b
$ Response b -> Either (ResponseEventStream m b) (Response b)
forall a b. b -> Either a b
Right
(Response b -> Either (ResponseEventStream m b) (Response b))
-> Response b -> Either (ResponseEventStream m b) (Response b)
forall a b. (a -> b) -> a -> b
$ Text -> Response b
forall a. Serialize a => Text -> Response a
singleError
(Text -> Response b) -> Text -> Response b
forall a b. (a -> b) -> a -> b
$ QueryError -> Text
Transform.queryError QueryError
queryError
Right Document m
transformed -> Document m -> m (Either (ResponseEventStream m b) (Response b))
forall (m :: * -> *) a.
(MonadCatch m, Serialize a) =>
Document m -> m (Either (ResponseEventStream m a) (Response a))
executeRequest Document m
transformed
executeRequest :: (MonadCatch m, Serialize a)
=> Transform.Document m
-> m (Either (ResponseEventStream m a) (Response a))
executeRequest :: Document m -> m (Either (ResponseEventStream m a) (Response a))
executeRequest (Transform.Document HashMap Text (Type m)
types' ObjectType m
rootObjectType Operation m
operation)
| (Transform.Query Maybe Text
_ Seq (Selection m)
fields) <- Operation m
operation =
Response a -> Either (ResponseEventStream m a) (Response a)
forall a b. b -> Either a b
Right (Response a -> Either (ResponseEventStream m a) (Response a))
-> m (Response a)
-> m (Either (ResponseEventStream m a) (Response a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Text (Type m)
-> ObjectType m -> Seq (Selection m) -> m (Response a)
forall (m :: * -> *) a.
(MonadCatch m, Serialize a) =>
HashMap Text (Type m)
-> ObjectType m -> Seq (Selection m) -> m (Response a)
executeOperation HashMap Text (Type m)
types' ObjectType m
rootObjectType Seq (Selection m)
fields
| (Transform.Mutation Maybe Text
_ Seq (Selection m)
fields) <- Operation m
operation =
Response a -> Either (ResponseEventStream m a) (Response a)
forall a b. b -> Either a b
Right (Response a -> Either (ResponseEventStream m a) (Response a))
-> m (Response a)
-> m (Either (ResponseEventStream m a) (Response a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Text (Type m)
-> ObjectType m -> Seq (Selection m) -> m (Response a)
forall (m :: * -> *) a.
(MonadCatch m, Serialize a) =>
HashMap Text (Type m)
-> ObjectType m -> Seq (Selection m) -> m (Response a)
executeOperation HashMap Text (Type m)
types' ObjectType m
rootObjectType Seq (Selection m)
fields
| (Transform.Subscription Maybe Text
_ Seq (Selection m)
fields) <- Operation m
operation
= (Text -> Either (ResponseEventStream m a) (Response a))
-> (ResponseEventStream m a
-> Either (ResponseEventStream m a) (Response a))
-> Either Text (ResponseEventStream m a)
-> Either (ResponseEventStream m a) (Response a)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Response a -> Either (ResponseEventStream m a) (Response a)
forall a b. b -> Either a b
Right (Response a -> Either (ResponseEventStream m a) (Response a))
-> (Text -> Response a)
-> Text
-> Either (ResponseEventStream m a) (Response a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Response a
forall a. Serialize a => Text -> Response a
singleError) ResponseEventStream m a
-> Either (ResponseEventStream m a) (Response a)
forall a b. a -> Either a b
Left
(Either Text (ResponseEventStream m a)
-> Either (ResponseEventStream m a) (Response a))
-> m (Either Text (ResponseEventStream m a))
-> m (Either (ResponseEventStream m a) (Response a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Text (Type m)
-> ObjectType m
-> Seq (Selection m)
-> m (Either Text (ResponseEventStream m a))
forall (m :: * -> *) a.
(MonadCatch m, Serialize a) =>
HashMap Text (Type m)
-> ObjectType m
-> Seq (Selection m)
-> m (Either Text (ResponseEventStream m a))
Subscribe.subscribe HashMap Text (Type m)
types' ObjectType m
rootObjectType Seq (Selection m)
fields
executeOperation :: (MonadCatch m, Serialize a)
=> HashMap Name (Type m)
-> Out.ObjectType m
-> Seq (Transform.Selection m)
-> m (Response a)
executeOperation :: HashMap Text (Type m)
-> ObjectType m -> Seq (Selection m) -> m (Response a)
executeOperation HashMap Text (Type m)
types' ObjectType m
objectType Seq (Selection m)
fields =
HashMap Text (Type m) -> CollectErrsT m a -> m (Response a)
forall (m :: * -> *) a.
(Monad m, Serialize a) =>
HashMap Text (Type m) -> CollectErrsT m a -> m (Response a)
runCollectErrs HashMap Text (Type m)
types' (CollectErrsT m a -> m (Response a))
-> CollectErrsT m a -> m (Response a)
forall a b. (a -> b) -> a -> b
$ Value -> ObjectType m -> Seq (Selection m) -> CollectErrsT m a
forall (m :: * -> *) a.
(MonadCatch m, Serialize a) =>
Value -> ObjectType m -> Seq (Selection m) -> CollectErrsT m a
executeSelectionSet Value
Definition.Null ObjectType m
objectType Seq (Selection m)
fields