graphql/src/Language/GraphQL.hs

37 lines
1.3 KiB
Haskell
Raw Normal View History

-- | This module provides the functions to parse and execute @GraphQL@ queries.
2019-07-14 05:58:05 +02:00
module Language.GraphQL
( graphql
, graphqlSubs
) where
import qualified Data.Aeson as Aeson
2020-05-21 10:20:59 +02:00
import Data.HashMap.Strict (HashMap)
import Data.Text (Text)
2020-05-21 10:20:59 +02:00
import Language.GraphQL.AST.Document
import Language.GraphQL.AST.Parser
import Language.GraphQL.Error
import Language.GraphQL.Execute
2020-05-21 10:20:59 +02:00
import Language.GraphQL.Execute.Coerce
2020-05-14 09:17:14 +02:00
import Language.GraphQL.Type.Schema
import Text.Megaparsec (parse)
-- | If the text parses correctly as a @GraphQL@ query the query is
-- executed using the given 'Schema.Resolver's.
graphql :: Monad m
2020-05-14 09:17:14 +02:00
=> Schema m -- ^ Resolvers.
-> Text -- ^ Text representing a @GraphQL@ request document.
-> m Aeson.Value -- ^ Response.
2020-05-21 10:20:59 +02:00
graphql = flip graphqlSubs (mempty :: Aeson.Object)
-- | If the text parses correctly as a @GraphQL@ query the substitution is
-- applied to the query and the query is then executed using to the given
-- 'Schema.Resolver's.
2020-05-21 10:20:59 +02:00
graphqlSubs :: (Monad m, VariableValue a)
2020-05-14 09:17:14 +02:00
=> Schema m -- ^ Resolvers.
2020-05-21 10:20:59 +02:00
-> HashMap Name a -- ^ Variable substitution function.
-> Text -- ^ Text representing a @GraphQL@ request document.
-> m Aeson.Value -- ^ Response.
graphqlSubs schema f
= either parseError (execute schema f)
2019-06-21 10:44:58 +02:00
. parse document ""