2016-03-15 14:02:34 +01:00
|
|
|
-- | 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
|
2016-02-05 12:32:35 +01:00
|
|
|
|
|
|
|
import qualified Data.Aeson as Aeson
|
2019-08-30 07:26:04 +02:00
|
|
|
import Data.List.NonEmpty (NonEmpty)
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Language.GraphQL.Error
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL.Execute
|
2019-11-03 10:42:10 +01:00
|
|
|
import Language.GraphQL.AST.Parser
|
2019-08-30 07:26:04 +02:00
|
|
|
import qualified Language.GraphQL.Schema as Schema
|
|
|
|
import Text.Megaparsec (parse)
|
2016-03-12 00:59:51 +01:00
|
|
|
|
2019-08-30 07:26:04 +02:00
|
|
|
-- | If the text parses correctly as a @GraphQL@ query the query is
|
|
|
|
-- executed using the given 'Schema.Resolver's.
|
2020-02-01 20:46:35 +01:00
|
|
|
graphql :: Monad m
|
2019-08-30 07:26:04 +02:00
|
|
|
=> NonEmpty (Schema.Resolver m) -- ^ Resolvers.
|
|
|
|
-> T.Text -- ^ Text representing a @GraphQL@ request document.
|
|
|
|
-> m Aeson.Value -- ^ Response.
|
2019-12-30 18:26:24 +01:00
|
|
|
graphql = flip graphqlSubs mempty
|
2016-02-15 14:25:15 +01:00
|
|
|
|
2019-08-30 07:26:04 +02:00
|
|
|
-- | 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-02-01 20:46:35 +01:00
|
|
|
graphqlSubs :: Monad m
|
2019-08-30 07:26:04 +02:00
|
|
|
=> NonEmpty (Schema.Resolver m) -- ^ Resolvers.
|
|
|
|
-> Schema.Subs -- ^ Variable substitution function.
|
|
|
|
-> T.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 ""
|