2016-03-15 14:02:34 +01:00
|
|
|
-- | This module provides the functions to parse and execute @GraphQL@ queries.
|
2019-07-07 06:31:53 +02:00
|
|
|
module Language.GraphQL where
|
2016-02-05 12:32:35 +01:00
|
|
|
|
2019-07-08 10:15:47 +02:00
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
2019-06-21 10:44:58 +02:00
|
|
|
import qualified Data.Text as T
|
2016-02-05 12:32:35 +01:00
|
|
|
|
|
|
|
import qualified Data.Aeson as Aeson
|
2019-06-21 10:44:58 +02:00
|
|
|
import Text.Megaparsec ( errorBundlePretty
|
|
|
|
, parse
|
|
|
|
)
|
2016-02-05 12:32:35 +01:00
|
|
|
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL.Execute
|
|
|
|
import Language.GraphQL.Parser
|
|
|
|
import Language.GraphQL.Schema
|
2016-02-05 12:32:35 +01:00
|
|
|
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL.Error
|
2016-03-12 00:59:51 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Takes a 'Schema' and text representing a @GraphQL@ request document.
|
|
|
|
-- If the text parses correctly as a @GraphQL@ query the query is
|
|
|
|
-- executed according to the given 'Schema'.
|
|
|
|
--
|
|
|
|
-- Returns the response as an @Aeson.@'Aeson.Value'.
|
2019-07-08 10:15:47 +02:00
|
|
|
graphql :: MonadIO m => Schema m -> T.Text -> m Aeson.Value
|
2016-02-15 14:25:15 +01:00
|
|
|
graphql = flip graphqlSubs $ const Nothing
|
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Takes a 'Schema', a variable substitution function and text
|
|
|
|
-- representing a @GraphQL@ request document. If the text parses
|
|
|
|
-- correctly as a @GraphQL@ query the substitution is applied to the
|
|
|
|
-- query and the query is then executed according to the given 'Schema'.
|
|
|
|
--
|
|
|
|
-- Returns the response as an @Aeson.@'Aeson.Value'.
|
2019-07-08 10:15:47 +02:00
|
|
|
graphqlSubs :: MonadIO m => Schema m -> Subs -> T.Text -> m Aeson.Value
|
2016-02-15 14:25:15 +01:00
|
|
|
graphqlSubs schema f =
|
2019-06-21 10:44:58 +02:00
|
|
|
either (parseError . errorBundlePretty) (execute schema f)
|
|
|
|
. parse document ""
|