2020-07-05 14:36:00 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
|
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
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
import Control.Monad.Catch (MonadCatch)
|
2016-02-05 12:32:35 +01:00
|
|
|
import qualified Data.Aeson as Aeson
|
2020-07-08 08:16:14 +02:00
|
|
|
import qualified Data.Aeson.Types as Aeson
|
2020-07-14 19:37:56 +02:00
|
|
|
import Data.Either (fromRight)
|
2020-07-05 14:36:00 +02:00
|
|
|
import qualified Data.Sequence as Seq
|
2020-05-10 18:32:58 +02:00
|
|
|
import Data.Text (Text)
|
2020-07-05 14:36:00 +02:00
|
|
|
import Language.GraphQL.AST
|
2019-08-30 07:26:04 +02:00
|
|
|
import Language.GraphQL.Error
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL.Execute
|
2020-05-14 09:17:14 +02:00
|
|
|
import Language.GraphQL.Type.Schema
|
2019-08-30 07:26:04 +02:00
|
|
|
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
|
2020-06-03 07:20:38 +02:00
|
|
|
-- executed using the given 'Schema'.
|
2020-07-17 07:05:03 +02:00
|
|
|
graphql :: MonadCatch m
|
2020-05-14 09:17:14 +02:00
|
|
|
=> Schema m -- ^ Resolvers.
|
2020-05-10 18:32:58 +02:00
|
|
|
-> Text -- ^ Text representing a @GraphQL@ request document.
|
2019-08-30 07:26:04 +02:00
|
|
|
-> m Aeson.Value -- ^ Response.
|
2020-07-05 14:36:00 +02:00
|
|
|
graphql schema = graphqlSubs schema mempty 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
|
2020-06-03 07:20:38 +02:00
|
|
|
-- 'Schema'.
|
2020-07-17 07:05:03 +02:00
|
|
|
graphqlSubs :: MonadCatch m
|
2020-05-14 09:17:14 +02:00
|
|
|
=> Schema m -- ^ Resolvers.
|
2020-07-05 14:36:00 +02:00
|
|
|
-> Maybe Text -- ^ Operation name.
|
|
|
|
-> Aeson.Object -- ^ Variable substitution function.
|
2020-05-10 18:32:58 +02:00
|
|
|
-> Text -- ^ Text representing a @GraphQL@ request document.
|
2019-08-30 07:26:04 +02:00
|
|
|
-> m Aeson.Value -- ^ Response.
|
2020-07-14 19:37:56 +02:00
|
|
|
graphqlSubs schema operationName variableValues document'
|
|
|
|
= either parseError executeRequest (parse document "" document')
|
|
|
|
>>= formatResponse
|
2020-07-05 14:36:00 +02:00
|
|
|
where
|
2020-07-14 19:37:56 +02:00
|
|
|
executeRequest parsed
|
|
|
|
= fromRight streamReturned
|
|
|
|
<$> execute schema operationName variableValues parsed
|
|
|
|
streamReturned = singleError "This service does not support subscriptions."
|
2020-07-05 14:36:00 +02:00
|
|
|
formatResponse (Response data'' Seq.Empty) =
|
|
|
|
pure $ Aeson.object [("data", data'')]
|
|
|
|
formatResponse (Response data'' errors') = pure $ Aeson.object
|
|
|
|
[ ("data", data'')
|
2020-07-08 08:16:14 +02:00
|
|
|
, ("errors", Aeson.toJSON $ fromError <$> errors')
|
2020-07-05 14:36:00 +02:00
|
|
|
]
|
2020-07-08 08:16:14 +02:00
|
|
|
fromError Error{ locations = [], ..} =
|
2020-07-05 14:36:00 +02:00
|
|
|
Aeson.object [("message", Aeson.toJSON message)]
|
2020-07-08 08:16:14 +02:00
|
|
|
fromError Error{..} = Aeson.object
|
2020-07-05 14:36:00 +02:00
|
|
|
[ ("message", Aeson.toJSON message)
|
2020-07-08 08:16:14 +02:00
|
|
|
, ("locations", Aeson.listValue fromLocation locations)
|
|
|
|
]
|
|
|
|
fromLocation Location{..} = Aeson.object
|
|
|
|
[ ("line", Aeson.toJSON line)
|
2020-07-05 14:36:00 +02:00
|
|
|
, ("column", Aeson.toJSON column)
|
|
|
|
]
|