2019-06-27 08:00:59 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-07-02 20:07:26 +02:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | This module provides a representation of a @GraphQL@ Schema in addition to
|
|
|
|
-- functions for defining and manipulating Schemas.
|
2019-07-07 06:31:53 +02:00
|
|
|
module Language.GraphQL.Schema
|
2019-07-05 20:05:04 +02:00
|
|
|
( Resolver
|
|
|
|
, Schema
|
2016-02-17 18:13:10 +01:00
|
|
|
, Subs
|
2016-02-19 19:21:32 +01:00
|
|
|
, object
|
|
|
|
, objectA
|
|
|
|
, scalar
|
|
|
|
, scalarA
|
|
|
|
, enum
|
|
|
|
, enumA
|
2017-01-29 16:53:15 +01:00
|
|
|
, resolve
|
2019-07-05 20:05:04 +02:00
|
|
|
, wrappedEnum
|
|
|
|
, wrappedEnumA
|
|
|
|
, wrappedObject
|
|
|
|
, wrappedObjectA
|
|
|
|
, wrappedScalar
|
|
|
|
, wrappedScalarA
|
2016-02-19 19:21:32 +01:00
|
|
|
-- * AST Reexports
|
2016-02-17 18:13:10 +01:00
|
|
|
, Field
|
2016-02-19 19:21:32 +01:00
|
|
|
, Argument(..)
|
|
|
|
, Value(..)
|
2016-02-17 18:13:10 +01:00
|
|
|
) where
|
2015-10-17 13:19:00 +02:00
|
|
|
|
2019-07-02 20:07:26 +02:00
|
|
|
import Control.Monad (MonadPlus(..))
|
2019-06-28 11:12:28 +02:00
|
|
|
import Control.Monad.Trans.Class (lift)
|
2019-07-02 20:07:26 +02:00
|
|
|
import Control.Monad.Trans.Except (runExceptT)
|
2019-07-01 06:39:13 +02:00
|
|
|
import Data.Foldable ( find
|
|
|
|
, fold
|
|
|
|
)
|
2017-01-28 18:15:14 +01:00
|
|
|
import Data.List.NonEmpty (NonEmpty)
|
|
|
|
import Data.Maybe (fromMaybe)
|
2016-02-19 19:21:32 +01:00
|
|
|
import qualified Data.Aeson as Aeson
|
2016-02-11 14:24:31 +01:00
|
|
|
import Data.HashMap.Strict (HashMap)
|
2016-02-17 18:13:10 +01:00
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
2019-07-01 06:39:13 +02:00
|
|
|
import Data.Text (Text)
|
2019-06-27 08:00:59 +02:00
|
|
|
import qualified Data.Text as T
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL.Error
|
2019-07-02 20:07:26 +02:00
|
|
|
import Language.GraphQL.Trans
|
2019-07-05 20:05:04 +02:00
|
|
|
import Language.GraphQL.Type
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL.AST.Core
|
2015-10-17 13:19:00 +02:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | A GraphQL schema.
|
|
|
|
-- @f@ is usually expected to be an instance of 'Alternative'.
|
2019-06-28 11:12:28 +02:00
|
|
|
type Schema m = NonEmpty (Resolver m)
|
2016-01-26 13:38:02 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Resolves a 'Field' into an @Aeson.@'Aeson.Object' with error information
|
2019-07-01 06:39:13 +02:00
|
|
|
-- (or 'empty'). @m@ is usually expected to be an instance of 'MonadPlus'.
|
|
|
|
data Resolver m = Resolver
|
|
|
|
Text -- ^ Name
|
|
|
|
(Field -> CollectErrsT m Aeson.Object) -- ^ Resolver
|
2017-01-29 16:53:15 +01:00
|
|
|
|
|
|
|
type Fields = [Field]
|
|
|
|
|
|
|
|
type Arguments = [Argument]
|
2015-10-17 13:19:00 +02:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Variable substitution function.
|
2017-02-12 19:19:13 +01:00
|
|
|
type Subs = Name -> Maybe Value
|
2016-01-26 12:43:18 +01:00
|
|
|
|
2017-01-29 16:53:15 +01:00
|
|
|
-- | Create a new 'Resolver' with the given 'Name' from the given 'Resolver's.
|
2019-07-02 20:07:26 +02:00
|
|
|
object :: MonadPlus m => Name -> ActionT m [Resolver m] -> Resolver m
|
|
|
|
object name = objectA name . const
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Like 'object' but also taking 'Argument's.
|
2019-07-02 20:07:26 +02:00
|
|
|
objectA :: MonadPlus m
|
|
|
|
=> Name -> (Arguments -> ActionT m [Resolver m]) -> Resolver m
|
|
|
|
objectA name f = Resolver name $ resolveFieldValue f resolveRight
|
2019-07-01 06:39:13 +02:00
|
|
|
where
|
2019-07-03 17:54:50 +02:00
|
|
|
resolveRight fld@(Field _ _ _ flds) resolver = withField (resolve resolver flds) fld
|
2017-03-04 00:39:03 +01:00
|
|
|
|
2019-07-05 20:05:04 +02:00
|
|
|
-- | Like 'object' but also taking 'Argument's and can be null or a list of objects.
|
|
|
|
wrappedObjectA :: MonadPlus m
|
|
|
|
=> Name -> (Arguments -> ActionT m (Wrapping [Resolver m])) -> Resolver m
|
|
|
|
wrappedObjectA name f = Resolver name $ resolveFieldValue f resolveRight
|
2019-07-01 06:39:13 +02:00
|
|
|
where
|
2019-07-05 20:05:04 +02:00
|
|
|
resolveRight fld@(Field _ _ _ sels) resolver
|
|
|
|
= withField (traverse (`resolve` sels) resolver) fld
|
|
|
|
|
|
|
|
-- | Like 'object' but can be null or a list of objects.
|
|
|
|
wrappedObject :: MonadPlus m
|
|
|
|
=> Name -> ActionT m (Wrapping [Resolver m]) -> Resolver m
|
|
|
|
wrappedObject name = wrappedObjectA name . const
|
2017-02-23 04:03:08 +01:00
|
|
|
|
2016-03-09 01:15:46 +01:00
|
|
|
-- | A scalar represents a primitive value, like a string or an integer.
|
2019-07-02 20:07:26 +02:00
|
|
|
scalar :: (MonadPlus m, Aeson.ToJSON a) => Name -> ActionT m a -> Resolver m
|
|
|
|
scalar name = scalarA name . const
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Like 'scalar' but also taking 'Argument's.
|
2019-07-02 20:07:26 +02:00
|
|
|
scalarA :: (MonadPlus m, Aeson.ToJSON a)
|
|
|
|
=> Name -> (Arguments -> ActionT m a) -> Resolver m
|
|
|
|
scalarA name f = Resolver name $ resolveFieldValue f resolveRight
|
2019-07-01 06:39:13 +02:00
|
|
|
where
|
2019-07-05 20:05:04 +02:00
|
|
|
resolveRight fld result = withField (return result) fld
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2019-07-05 20:05:04 +02:00
|
|
|
-- | Lika 'scalar' but also taking 'Argument's and can be null or a list of scalars.
|
|
|
|
wrappedScalarA :: (MonadPlus m, Aeson.ToJSON a)
|
|
|
|
=> Name -> (Arguments -> ActionT m (Wrapping a)) -> Resolver m
|
|
|
|
wrappedScalarA name f = Resolver name $ resolveFieldValue f resolveRight
|
2019-07-03 17:54:50 +02:00
|
|
|
where
|
2019-07-05 20:05:04 +02:00
|
|
|
resolveRight fld (Named result) = withField (return result) fld
|
|
|
|
resolveRight fld Null
|
2019-07-03 17:54:50 +02:00
|
|
|
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
2019-07-05 20:05:04 +02:00
|
|
|
resolveRight fld (List result) = withField (return result) fld
|
2019-07-03 17:54:50 +02:00
|
|
|
|
2019-07-05 20:05:04 +02:00
|
|
|
-- | Like 'scalar' but can be null or a list of scalars.
|
|
|
|
wrappedScalar :: (MonadPlus m, Aeson.ToJSON a)
|
|
|
|
=> Name -> ActionT m (Wrapping a) -> Resolver m
|
|
|
|
wrappedScalar name = wrappedScalarA name . const
|
2017-02-23 04:03:08 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Represents one of a finite set of possible values.
|
|
|
|
-- Used in place of a 'scalar' when the possible responses are easily enumerable.
|
2019-07-02 20:07:26 +02:00
|
|
|
enum :: MonadPlus m => Name -> ActionT m [Text] -> Resolver m
|
|
|
|
enum name = enumA name . const
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Like 'enum' but also taking 'Argument's.
|
2019-07-02 20:07:26 +02:00
|
|
|
enumA :: MonadPlus m => Name -> (Arguments -> ActionT m [Text]) -> Resolver m
|
|
|
|
enumA name f = Resolver name $ resolveFieldValue f resolveRight
|
2019-07-01 06:39:13 +02:00
|
|
|
where
|
2019-07-03 17:54:50 +02:00
|
|
|
resolveRight fld resolver = withField (return resolver) fld
|
|
|
|
|
2019-07-05 20:05:04 +02:00
|
|
|
-- | Like 'enum' but also taking 'Argument's and can be null or a list of enums.
|
|
|
|
wrappedEnumA :: MonadPlus m
|
|
|
|
=> Name -> (Arguments -> ActionT m (Wrapping [Text])) -> Resolver m
|
|
|
|
wrappedEnumA name f = Resolver name $ resolveFieldValue f resolveRight
|
2019-07-03 17:54:50 +02:00
|
|
|
where
|
2019-07-05 20:05:04 +02:00
|
|
|
resolveRight fld (Named resolver) = withField (return resolver) fld
|
|
|
|
resolveRight fld Null
|
2019-07-03 17:54:50 +02:00
|
|
|
= return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
2019-07-05 20:05:04 +02:00
|
|
|
resolveRight fld (List resolver) = withField (return resolver) fld
|
|
|
|
|
|
|
|
-- | Like 'enum' but can be null or a list of enums.
|
|
|
|
wrappedEnum :: MonadPlus m => Name -> ActionT m (Wrapping [Text]) -> Resolver m
|
|
|
|
wrappedEnum name = wrappedEnumA name . const
|
2019-07-02 20:07:26 +02:00
|
|
|
|
|
|
|
resolveFieldValue :: MonadPlus m
|
|
|
|
=> ([Argument] -> ActionT m a)
|
|
|
|
-> (Field -> a -> CollectErrsT m (HashMap Text Aeson.Value))
|
|
|
|
-> Field
|
|
|
|
-> CollectErrsT m (HashMap Text Aeson.Value)
|
2019-07-04 06:17:11 +02:00
|
|
|
resolveFieldValue f resolveRight fld@(Field _ _ args _) = do
|
2019-07-02 20:07:26 +02:00
|
|
|
result <- lift $ runExceptT . runActionT $ f args
|
|
|
|
either resolveLeft (resolveRight fld) result
|
|
|
|
where
|
|
|
|
resolveLeft err = do
|
|
|
|
_ <- addErrMsg err
|
2019-07-03 17:54:50 +02:00
|
|
|
return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Helper function to facilitate 'Argument' handling.
|
2019-06-28 11:12:28 +02:00
|
|
|
withField :: (MonadPlus m, Aeson.ToJSON a)
|
2019-07-03 17:54:50 +02:00
|
|
|
=> CollectErrsT m a -> Field -> CollectErrsT m (HashMap Text Aeson.Value)
|
|
|
|
withField v fld
|
|
|
|
= HashMap.singleton (aliasOrName fld) . Aeson.toJSON <$> runAppendErrs v
|
2017-01-29 16:53:15 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Takes a list of 'Resolver's and a list of 'Field's and applies each
|
|
|
|
-- 'Resolver' to each 'Field'. Resolves into a value containing the
|
|
|
|
-- resolved 'Field', or a null value and error information.
|
2019-07-01 06:39:13 +02:00
|
|
|
resolve :: MonadPlus m
|
2019-07-02 20:07:26 +02:00
|
|
|
=> [Resolver m] -> Fields -> CollectErrsT m Aeson.Value
|
2019-07-01 06:39:13 +02:00
|
|
|
resolve resolvers = fmap (Aeson.toJSON . fold) . traverse tryResolvers
|
2019-06-27 08:00:59 +02:00
|
|
|
where
|
2019-07-02 20:07:26 +02:00
|
|
|
tryResolvers fld = mplus (maybe mzero (tryResolver fld) $ find (compareResolvers fld) resolvers) $ errmsg fld
|
2019-07-01 06:39:13 +02:00
|
|
|
compareResolvers (Field _ name _ _) (Resolver name' _) = name == name'
|
|
|
|
tryResolver fld (Resolver _ resolver) = resolver fld
|
2019-07-04 06:17:11 +02:00
|
|
|
errmsg fld@(Field _ name _ _) = do
|
2019-06-28 11:12:28 +02:00
|
|
|
addErrMsg $ T.unwords ["field", name, "not resolved."]
|
2019-07-03 17:54:50 +02:00
|
|
|
return $ HashMap.singleton (aliasOrName fld) Aeson.Null
|
|
|
|
|
|
|
|
aliasOrName :: Field -> Text
|
|
|
|
aliasOrName (Field alias name _ _) = fromMaybe name alias
|