graphql/src/Data/GraphQL/Schema.hs

169 lines
5.4 KiB
Haskell
Raw Normal View History

2019-06-27 08:00:59 +02:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
-- | This module provides a representation of a @GraphQL@ Schema in addition to
-- functions for defining and manipulating Schemas.
module Data.GraphQL.Schema
( Schema
, Resolver
, Subs
, object
, object'
, objectA
, objectA'
, scalar
, scalarA
, array
, array'
, arrayA
, arrayA'
, enum
, enumA
, resolve
-- * AST Reexports
, Field
, Argument(..)
, Value(..)
) where
2019-06-28 11:12:28 +02:00
import Control.Applicative (Alternative(..))
import Control.Monad (MonadPlus)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State ( get
, put
)
import Data.Foldable (fold)
2019-06-27 08:00:59 +02:00
import Data.GraphQL.Error
import Data.List.NonEmpty (NonEmpty)
import Data.Maybe (fromMaybe)
2019-06-28 11:12:28 +02:00
import Data.Monoid (Alt(..))
import qualified Data.Aeson as Aeson
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
2019-06-27 08:00:59 +02:00
import qualified Data.Text as T
import Data.GraphQL.AST.Core
-- | 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
-- | Resolves a 'Field' into an @Aeson.@'Aeson.Object' with error information
-- (or 'empty'). @f@ is usually expected to be an instance of 'Alternative'.
2019-06-28 11:12:28 +02:00
type Resolver m = Field -> CollectErrsT m Aeson.Object
2019-06-28 11:12:28 +02:00
type Resolvers m = [Resolver m]
type Fields = [Field]
type Arguments = [Argument]
-- | Variable substitution function.
type Subs = Name -> Maybe Value
-- | Create a new 'Resolver' with the given 'Name' from the given 'Resolver's.
2019-06-28 11:12:28 +02:00
object :: MonadPlus m => Name -> Resolvers m -> Resolver m
object name resolvers = objectA name $ \case
[] -> resolvers
_ -> empty
-- | Like 'object' but also taking 'Argument's.
objectA
2019-06-28 11:12:28 +02:00
:: MonadPlus m
=> Name -> (Arguments -> Resolvers m) -> Resolver m
objectA name f fld@(Field _ _ args flds) = withField name (resolve (f args) flds) fld
-- | Create a named 'Resolver' from a list of 'Resolver's.
2019-06-28 11:12:28 +02:00
object' :: MonadPlus m => Name -> m (Resolvers m) -> Resolver m
object' name resolvs = objectA' name $ \case
[] -> resolvs
_ -> empty
-- | Like 'object'' but also taking 'Argument's.
objectA'
2019-06-28 11:12:28 +02:00
:: MonadPlus m
=> Name -> (Arguments -> m (Resolvers m)) -> Resolver m
objectA' name f fld@(Field _ _ args flds) = do
2019-06-28 11:12:28 +02:00
resolvs <- lift $ f args
withField name (resolve resolvs flds) fld
-- | A scalar represents a primitive value, like a string or an integer.
2019-06-28 11:12:28 +02:00
scalar :: (MonadPlus m, Aeson.ToJSON a) => Name -> a -> Resolver m
scalar name s = scalarA name $ \case
[] -> pure s
_ -> empty
-- | Like 'scalar' but also taking 'Argument's.
scalarA
2019-06-28 11:12:28 +02:00
:: (MonadPlus m, Aeson.ToJSON a)
=> Name -> (Arguments -> m a) -> Resolver m
scalarA name f fld@(Field _ _ args []) = withField name (lift $ f args) fld
scalarA _ _ _ = empty
2019-06-28 11:12:28 +02:00
array :: MonadPlus m => Name -> [Resolvers m] -> Resolver m
array name resolvers = arrayA name $ \case
[] -> resolvers
_ -> empty
-- | Like 'array' but also taking 'Argument's.
arrayA
2019-06-28 11:12:28 +02:00
:: MonadPlus m
=> Name -> (Arguments -> [Resolvers m]) -> Resolver m
arrayA name f fld@(Field _ _ args sels) =
2019-06-28 11:12:28 +02:00
withField name (traverse (`resolve` sels) $ f args) fld
-- | Like 'object'' but taking lists of 'Resolver's instead of a single list.
2019-06-28 11:12:28 +02:00
array' :: MonadPlus m => Name -> m [Resolvers m] -> Resolver m
array' name resolvs = arrayA' name $ \case
[] -> resolvs
_ -> empty
-- | Like 'array'' but also taking 'Argument's.
arrayA'
2019-06-28 11:12:28 +02:00
:: MonadPlus m
=> Name -> (Arguments -> m [Resolvers m]) -> Resolver m
arrayA' name f fld@(Field _ _ args sels) = do
2019-06-28 11:12:28 +02:00
resolvs <- lift $ f args
withField name (traverse (`resolve` sels) resolvs) fld
-- | Represents one of a finite set of possible values.
-- Used in place of a 'scalar' when the possible responses are easily enumerable.
2019-06-28 11:12:28 +02:00
enum :: MonadPlus m => Name -> m [T.Text] -> Resolver m
enum name enums = enumA name $ \case
[] -> enums
_ -> empty
-- | Like 'enum' but also taking 'Argument's.
2019-06-28 11:12:28 +02:00
enumA :: MonadPlus m => Name -> (Arguments -> m [T.Text]) -> Resolver m
enumA name f fld@(Field _ _ args []) = withField name (lift $ f args) fld
enumA _ _ _ = empty
-- | Helper function to facilitate 'Argument' handling.
2019-06-28 11:12:28 +02:00
withField :: (MonadPlus m, Aeson.ToJSON a)
=> Name -> CollectErrsT m a -> Field -> CollectErrsT m (HashMap T.Text Aeson.Value)
2019-06-27 08:00:59 +02:00
withField name v (Field alias name' _ _)
2019-06-28 11:12:28 +02:00
| name == name' = do
collection <- HashMap.singleton aliasOrName . Aeson.toJSON <$> runAppendErrs v
errors <- get
if null errors
then return collection
-- TODO: Report error when Non-Nullable type for field argument.
else put [] >> return (HashMap.singleton aliasOrName Aeson.Null)
2019-06-27 08:00:59 +02:00
| otherwise = empty
where
aliasOrName = fromMaybe name alias
-- | 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-06-28 11:12:28 +02:00
resolve :: MonadPlus m => Resolvers m -> Fields -> CollectErrsT m Aeson.Value
resolve resolvers =
2019-06-28 11:12:28 +02:00
fmap (Aeson.toJSON . fold)
2019-06-27 08:00:59 +02:00
. traverse (\fld -> getAlt (foldMap (Alt . ($ fld)) resolvers) <|> errmsg fld)
where
2019-06-28 11:12:28 +02:00
errmsg (Field alias name _ _) = do
addErrMsg $ T.unwords ["field", name, "not resolved."]
return $ HashMap.singleton aliasOrName Aeson.Null
where
aliasOrName = fromMaybe name alias