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
|
2017-01-28 18:15:14 +01:00
|
|
|
( 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
|
2017-02-23 04:03:08 +01:00
|
|
|
, object'
|
2016-02-19 19:21:32 +01:00
|
|
|
, objectA
|
2017-02-23 04:03:08 +01:00
|
|
|
, objectA'
|
2016-02-19 19:21:32 +01:00
|
|
|
, scalar
|
|
|
|
, scalarA
|
|
|
|
, array
|
2017-02-23 04:03:08 +01:00
|
|
|
, array'
|
2016-02-19 19:21:32 +01:00
|
|
|
, arrayA
|
2017-02-23 04:03:08 +01:00
|
|
|
, arrayA'
|
2016-02-19 19:21:32 +01:00
|
|
|
, enum
|
|
|
|
, enumA
|
2017-01-29 16:53:15 +01:00
|
|
|
, resolve
|
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
|
|
|
|
2017-03-04 00:39:03 +01:00
|
|
|
import Control.Applicative (Alternative(empty), (<|>))
|
2016-02-17 18:13:10 +01:00
|
|
|
import Data.Foldable (fold)
|
2017-01-28 18:15:14 +01:00
|
|
|
import Data.List.NonEmpty (NonEmpty)
|
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import Data.Monoid (Alt(Alt,getAlt))
|
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-02-17 18:13:10 +01:00
|
|
|
|
2017-01-28 18:15:14 +01:00
|
|
|
import Data.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'.
|
2017-01-28 18:15:14 +01:00
|
|
|
type Schema f = NonEmpty (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'.
|
2017-01-29 16:53:15 +01:00
|
|
|
type Resolver f = Field -> f Aeson.Object
|
|
|
|
|
|
|
|
type Resolvers f = [Resolver f]
|
|
|
|
|
|
|
|
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.
|
|
|
|
object :: Alternative f => Name -> Resolvers f -> Resolver f
|
|
|
|
object name resolvers = objectA name $ \case
|
|
|
|
[] -> resolvers
|
|
|
|
_ -> empty
|
2016-02-19 19:21:32 +01:00
|
|
|
|
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
|
2017-01-29 16:53:15 +01:00
|
|
|
=> Name -> (Arguments -> Resolvers f) -> Resolver f
|
|
|
|
objectA name f fld@(Field _ _ args flds) = withField name (resolve (f args) flds) fld
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2017-03-04 00:39:03 +01:00
|
|
|
|
2017-02-23 04:03:08 +01:00
|
|
|
-- | Create a named 'Resolver' from a list of 'Resolver's.
|
2017-03-01 19:13:20 +01:00
|
|
|
object' :: (Alternative f, Monad f) => Text -> f [Resolver f] -> Resolver f
|
2017-02-23 04:03:08 +01:00
|
|
|
object' name resolvs = objectA' name $ \case
|
|
|
|
[] -> resolvs
|
|
|
|
_ -> empty
|
|
|
|
|
|
|
|
-- | Like 'object'' but also taking 'Argument's.
|
|
|
|
objectA'
|
|
|
|
:: (Alternative f, Monad f)
|
|
|
|
=> Text -> ([Argument] -> f [Resolver f]) -> Resolver f
|
2017-03-02 01:04:35 +01:00
|
|
|
objectA' name f fld@(Field _ _ args flds) = do
|
2017-02-23 04:03:08 +01:00
|
|
|
resolvs <- f args
|
2017-03-02 01:04:35 +01:00
|
|
|
withField name (resolve resolvs flds) fld
|
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.
|
2017-01-29 16:53:15 +01:00
|
|
|
scalar :: (Alternative f, Aeson.ToJSON a) => Name -> a -> Resolver f
|
2016-02-19 19:21:32 +01:00
|
|
|
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)
|
2017-01-29 16:53:15 +01:00
|
|
|
=> Name -> (Arguments -> f a) -> Resolver f
|
|
|
|
scalarA name f fld@(Field _ _ args []) = withField name (f args) fld
|
2016-02-19 19:21:32 +01:00
|
|
|
scalarA _ _ _ = empty
|
|
|
|
|
2017-01-29 16:53:15 +01:00
|
|
|
array :: Alternative f => Name -> [Resolvers f] -> Resolver f
|
|
|
|
array name resolvers = arrayA name $ \case
|
|
|
|
[] -> resolvers
|
2016-02-19 19:21:32 +01:00
|
|
|
_ -> 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
|
2017-01-29 16:53:15 +01:00
|
|
|
=> Text -> (Arguments -> [Resolvers f]) -> Resolver f
|
2017-01-28 18:15:14 +01:00
|
|
|
arrayA name f fld@(Field _ _ args sels) =
|
2017-01-29 16:53:15 +01:00
|
|
|
withField name (traverse (`resolve` sels) $ f args) fld
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2017-02-23 04:03:08 +01:00
|
|
|
-- | Like 'object'' but taking lists of 'Resolver's instead of a single list.
|
|
|
|
array' :: (Alternative f, Monad f) => Text -> f [[Resolver f]] -> Resolver f
|
|
|
|
array' name resolvs = arrayA' name $ \case
|
|
|
|
[] -> resolvs
|
|
|
|
_ -> empty
|
|
|
|
|
|
|
|
-- | Like 'array'' but also taking 'Argument's.
|
|
|
|
arrayA'
|
|
|
|
:: (Alternative f, Monad f)
|
|
|
|
=> Text -> ([Argument] -> f [[Resolver f]]) -> Resolver f
|
2017-03-02 01:04:35 +01:00
|
|
|
arrayA' name f fld@(Field _ _ args sels) = do
|
2017-02-23 04:03:08 +01:00
|
|
|
resolvs <- f args
|
2017-03-02 01:04:35 +01:00
|
|
|
withField name (traverse (`resolve` sels) resolvs) fld
|
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.
|
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
|
2017-01-29 16:53:15 +01:00
|
|
|
enumA name f fld@(Field _ _ args []) = withField name (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)
|
2017-01-29 16:53:15 +01:00
|
|
|
=> Name -> f a -> Field -> f (HashMap Text Aeson.Value)
|
2017-03-04 00:39:03 +01:00
|
|
|
withField name v (Field alias name' _ _) =
|
2017-01-29 16:53:15 +01:00
|
|
|
if name == name'
|
2017-03-04 00:39:03 +01:00
|
|
|
then fmap (HashMap.singleton aliasOrName . Aeson.toJSON) v
|
|
|
|
-- TODO: Report error when Non-Nullable type for field argument.
|
|
|
|
<|> pure (HashMap.singleton aliasOrName Aeson.Null)
|
2017-01-29 16:53:15 +01:00
|
|
|
else empty
|
|
|
|
where
|
|
|
|
aliasOrName = fromMaybe name 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.
|
2017-01-29 16:53:15 +01:00
|
|
|
resolve :: Alternative f => Resolvers f -> Fields -> f Aeson.Value
|
|
|
|
resolve resolvers =
|
|
|
|
fmap (Aeson.toJSON . fold)
|
|
|
|
. traverse (\fld -> getAlt (foldMap (Alt . ($ fld)) resolvers))
|