graphql/src/Data/GraphQL.hs

37 lines
1.3 KiB
Haskell
Raw Normal View History

-- | This module provides the functions to parse and execute @GraphQL@ queries.
module Data.GraphQL where
2019-06-28 11:12:28 +02:00
import Control.Monad (MonadPlus)
2019-06-21 10:44:58 +02:00
import qualified Data.Text as T
import qualified Data.Aeson as Aeson
2019-06-21 10:44:58 +02:00
import Text.Megaparsec ( errorBundlePretty
, parse
)
import Data.GraphQL.Execute
import Data.GraphQL.Parser
import Data.GraphQL.Schema
import Data.GraphQL.Error
-- | 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-06-28 11:12:28 +02:00
graphql :: MonadPlus m => Schema m -> T.Text -> m Aeson.Value
graphql = flip graphqlSubs $ const Nothing
-- | 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-06-28 11:12:28 +02:00
graphqlSubs :: MonadPlus m => Schema m -> Subs -> T.Text -> m Aeson.Value
graphqlSubs schema f =
2019-06-21 10:44:58 +02:00
either (parseError . errorBundlePretty) (execute schema f)
. parse document ""