diff options
| author | Eugen Wissner <belka@caraus.de> | 2020-05-26 11:13:55 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2020-05-26 11:13:55 +0200 |
| commit | c06d0b8e95ea4b87eab69da085cb32dbd052c1f0 (patch) | |
| tree | 12bcabe076d873f2676b33c6f510dba566352756 /src/Language/GraphQL/Execute.hs | |
| parent | 61dbe6c7280a899b485146aa8557948417e78360 (diff) | |
| download | graphql-c06d0b8e95ea4b87eab69da085cb32dbd052c1f0.tar.gz | |
Add Union and Interface type definitions
Diffstat (limited to 'src/Language/GraphQL/Execute.hs')
| -rw-r--r-- | src/Language/GraphQL/Execute.hs | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/src/Language/GraphQL/Execute.hs b/src/Language/GraphQL/Execute.hs index 65ab6f7..862e360 100644 --- a/src/Language/GraphQL/Execute.hs +++ b/src/Language/GraphQL/Execute.hs @@ -9,6 +9,7 @@ module Language.GraphQL.Execute import qualified Data.Aeson as Aeson import qualified Data.HashMap.Strict as HashMap +import Data.Sequence (Seq(..)) import Data.Text (Text) import Language.GraphQL.AST.Document import qualified Language.GraphQL.AST.Core as AST.Core @@ -29,7 +30,7 @@ execute :: (Monad m, VariableValue a) -> HashMap.HashMap Name a -- ^ Variable substitution function. -> Document -- @GraphQL@ document. -> m Aeson.Value -execute schema = document schema Nothing +execute schema = executeRequest schema Nothing -- | The substitution is applied to the document, and the resolvers are applied -- to the resulting fields. The operation name can be used if the document @@ -43,36 +44,34 @@ executeWithName :: (Monad m, VariableValue a) -> HashMap.HashMap Name a -- ^ Variable substitution function. -> Document -- ^ @GraphQL@ Document. -> m Aeson.Value -executeWithName schema operationName = document schema (Just operationName) +executeWithName schema operationName = + executeRequest schema (Just operationName) -document :: (Monad m, VariableValue a) +executeRequest :: (Monad m, VariableValue a) => Schema m -> Maybe Text -> HashMap.HashMap Name a -> Document -> m Aeson.Value -document schema operationName subs document' = - case Transform.document schema operationName subs document' of +executeRequest schema operationName subs document = + case Transform.document schema operationName subs document of Left queryError -> pure $ singleError $ Transform.queryError queryError - Right (Transform.Document operation') -> operation schema operation' + Right (Transform.Document rootObjectType operation) + | (AST.Core.Query _ fields) <- operation -> + executeOperation rootObjectType fields + | (AST.Core.Mutation _ fields) <- operation -> + executeOperation rootObjectType fields -operation :: Monad m - => Schema m - -> AST.Core.Operation +-- This is actually executeMutation, but we don't distinguish between queries +-- and mutations yet. +executeOperation :: Monad m + => Out.ObjectType m + -> Seq AST.Core.Selection -> m Aeson.Value -operation = schemaOperation +executeOperation (Out.ObjectType _ _ _ objectFields) fields + = runCollectErrs + $ flip Schema.resolve fields + $ fmap getResolver + $ objectFields where - resolve queryFields = runCollectErrs - . flip Schema.resolve queryFields - . fmap getResolver - . fields - fields (Out.ObjectType _ _ objectFields) = objectFields - lookupError = pure - $ singleError "Root operation type couldn't be found in the schema." - schemaOperation Schema {query} (AST.Core.Query _ fields') = - resolve fields' query - schemaOperation Schema {mutation = Just mutation} (AST.Core.Mutation _ fields') = - resolve fields' mutation - schemaOperation Schema {mutation = Nothing} (AST.Core.Mutation _ _) = - lookupError getResolver (Out.Field _ _ _ resolver) = resolver |
