2016-02-11 14:24:31 +01:00
|
|
|
{-# LANGUAGE CPP #-}
|
2016-03-12 00:59:51 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2016-02-19 19:21:32 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
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.
|
2016-02-17 18:13:10 +01:00
|
|
|
module Data.GraphQL.Schema
|
|
|
|
( Schema(..)
|
2016-02-19 19:21:32 +01:00
|
|
|
, Resolver
|
2016-02-17 18:13:10 +01:00
|
|
|
, Subs
|
2016-02-19 19:21:32 +01:00
|
|
|
, object
|
|
|
|
, objectA
|
|
|
|
, scalar
|
|
|
|
, scalarA
|
|
|
|
, array
|
|
|
|
, arrayA
|
|
|
|
, enum
|
|
|
|
, enumA
|
|
|
|
, resolvers
|
2016-02-17 18:13:10 +01:00
|
|
|
, fields
|
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
|
|
|
|
2016-02-11 14:24:31 +01:00
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
2016-03-15 14:02:34 +01:00
|
|
|
import Control.Applicative (pure)
|
|
|
|
import Control.Arrow (first)
|
2016-02-19 19:21:32 +01:00
|
|
|
import Data.Foldable (foldMap)
|
2016-02-17 18:13:10 +01:00
|
|
|
import Data.Traversable (traverse)
|
2016-02-19 19:21:32 +01:00
|
|
|
import Data.Monoid (Monoid(mempty,mappend))
|
|
|
|
#else
|
2016-03-15 14:02:34 +01:00
|
|
|
import Data.Bifunctor (first)
|
2016-02-19 19:21:32 +01:00
|
|
|
import Data.Monoid (Alt(Alt,getAlt))
|
2016-02-11 14:24:31 +01:00
|
|
|
#endif
|
2016-03-15 14:02:34 +01:00
|
|
|
import Control.Applicative (Alternative((<|>), empty))
|
2016-02-17 18:13:10 +01:00
|
|
|
import Data.Maybe (catMaybes)
|
|
|
|
import Data.Foldable (fold)
|
2015-10-17 13:19:00 +02:00
|
|
|
|
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
|
2016-02-19 19:21:32 +01:00
|
|
|
import Data.Text (Text)
|
2016-03-12 00:59:51 +01:00
|
|
|
import qualified Data.Text as T (null, unwords)
|
2016-02-17 18:13:10 +01:00
|
|
|
|
|
|
|
import Data.GraphQL.AST
|
2016-03-12 00:59:51 +01:00
|
|
|
import Data.GraphQL.Error
|
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'.
|
2016-02-19 19:21:32 +01:00
|
|
|
data Schema f = Schema [Resolver f]
|
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
|
|
|
|
-- (or 'empty'). @f@ is usually expected to be an instance of 'Alternative'.
|
2016-03-12 00:59:51 +01:00
|
|
|
type Resolver f = Field -> CollectErrsT f Aeson.Object
|
2015-10-17 13:19:00 +02:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Variable substitution function.
|
2016-02-17 18:13:10 +01:00
|
|
|
type Subs = Text -> Maybe Text
|
2016-01-26 12:43:18 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Create a named 'Resolver' from a list of 'Resolver's.
|
2016-02-19 19:21:32 +01:00
|
|
|
object :: Alternative f => Text -> [Resolver f] -> Resolver f
|
|
|
|
object name resolvs = objectA name $ \case
|
|
|
|
[] -> resolvs
|
|
|
|
_ -> empty
|
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Like 'object' but also taking 'Argument's.
|
2016-02-19 19:21:32 +01:00
|
|
|
objectA
|
|
|
|
:: Alternative f
|
|
|
|
=> Text -> ([Argument] -> [Resolver f]) -> Resolver f
|
|
|
|
objectA name f fld@(Field _ _ args _ sels) =
|
|
|
|
withField name (resolvers (f args) $ fields sels) fld
|
|
|
|
|
2016-03-09 01:15:46 +01:00
|
|
|
-- | A scalar represents a primitive value, like a string or an integer.
|
2016-02-19 19:21:32 +01:00
|
|
|
scalar :: (Alternative f, Aeson.ToJSON a) => Text -> a -> Resolver f
|
|
|
|
scalar name s = scalarA name $ \case
|
|
|
|
[] -> pure s
|
|
|
|
_ -> empty
|
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Like 'scalar' but also taking 'Argument's.
|
2016-02-19 19:21:32 +01:00
|
|
|
scalarA
|
|
|
|
:: (Alternative f, Aeson.ToJSON a)
|
|
|
|
=> Text -> ([Argument] -> f a) -> Resolver f
|
2016-03-12 00:59:51 +01:00
|
|
|
scalarA name f fld@(Field _ _ args _ []) = withField name (errWrap $ f args) fld
|
2016-02-19 19:21:32 +01:00
|
|
|
scalarA _ _ _ = empty
|
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Like 'object' but taking lists of 'Resolver's instead of a single list.
|
2016-02-19 19:21:32 +01:00
|
|
|
array :: Alternative f => Text -> [[Resolver f]] -> Resolver f
|
|
|
|
array name resolvs = arrayA name $ \case
|
|
|
|
[] -> resolvs
|
|
|
|
_ -> empty
|
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Like 'array' but also taking 'Argument's.
|
2016-02-19 19:21:32 +01:00
|
|
|
arrayA
|
|
|
|
:: Alternative f
|
|
|
|
=> Text -> ([Argument] -> [[Resolver f]]) -> Resolver f
|
|
|
|
arrayA name f fld@(Field _ _ args _ sels) =
|
2016-03-12 00:59:51 +01:00
|
|
|
withField name (joinErrs $ traverse (flip resolvers $ fields sels) $ f args) fld
|
2016-02-19 19:21:32 +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.
|
2016-02-19 19:21:32 +01:00
|
|
|
enum :: Alternative f => Text -> f [Text] -> Resolver f
|
|
|
|
enum name enums = enumA name $ \case
|
|
|
|
[] -> enums
|
|
|
|
_ -> empty
|
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Like 'enum' but also taking 'Argument's.
|
2016-02-19 19:21:32 +01:00
|
|
|
enumA :: Alternative f => Text -> ([Argument] -> f [Text]) -> Resolver f
|
2016-03-12 00:59:51 +01:00
|
|
|
enumA name f fld@(Field _ _ args _ []) = withField name (errWrap $ f args) fld
|
2016-02-19 19:21:32 +01:00
|
|
|
enumA _ _ _ = empty
|
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Helper function to facilitate 'Argument' handling.
|
2016-02-19 19:21:32 +01:00
|
|
|
withField
|
|
|
|
:: (Alternative f, Aeson.ToJSON a)
|
2016-03-12 00:59:51 +01:00
|
|
|
=> Text -> CollectErrsT f a -> Field -> CollectErrsT f (HashMap Text Aeson.Value)
|
2016-02-19 19:21:32 +01:00
|
|
|
withField name f (Field alias name' _ _ _) =
|
|
|
|
if name == name'
|
2016-03-12 00:59:51 +01:00
|
|
|
then fmap (first $ HashMap.singleton aliasOrName . Aeson.toJSON) f
|
2016-02-19 19:21:32 +01:00
|
|
|
else empty
|
|
|
|
where
|
|
|
|
aliasOrName = if T.null alias then name' else alias
|
|
|
|
|
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.
|
2016-03-12 00:59:51 +01:00
|
|
|
resolvers :: Alternative f => [Resolver f] -> [Field] -> CollectErrsT f Aeson.Value
|
2016-02-19 19:21:32 +01:00
|
|
|
resolvers resolvs =
|
2016-03-12 00:59:51 +01:00
|
|
|
fmap (first Aeson.toJSON . fold)
|
2016-03-15 14:02:34 +01:00
|
|
|
. traverse (\fld -> getAlt (foldMap (Alt . ($ fld)) resolvs) <|> errmsg fld)
|
|
|
|
where
|
|
|
|
errmsg (Field alias name _ _ _) = addErrMsg msg $ (errWrap . pure) val
|
|
|
|
where
|
|
|
|
val = HashMap.singleton aliasOrName Aeson.Null
|
|
|
|
msg = T.unwords ["field", name, "not resolved."]
|
|
|
|
aliasOrName = if T.null alias then name else alias
|
|
|
|
|
|
|
|
-- | Checks whether the given 'Selection' contains a 'Field' and
|
|
|
|
-- returns the 'Field' if so, else returns 'Nothing'.
|
2016-02-17 18:13:10 +01:00
|
|
|
field :: Selection -> Maybe Field
|
|
|
|
field (SelectionField x) = Just x
|
|
|
|
field _ = Nothing
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2016-03-15 14:02:34 +01:00
|
|
|
-- | Returns a list of the 'Field's contained in the given 'SelectionSet'.
|
2016-02-19 19:21:32 +01:00
|
|
|
fields :: SelectionSet -> [Field]
|
|
|
|
fields = catMaybes . fmap field
|
|
|
|
|
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
|
|
|
newtype Alt f a = Alt {getAlt :: f a}
|
|
|
|
|
|
|
|
instance Alternative f => Monoid (Alt f a) where
|
|
|
|
mempty = Alt empty
|
|
|
|
Alt x `mappend` Alt y = Alt $ x <|> y
|
|
|
|
#endif
|