summaryrefslogtreecommitdiff
path: root/Data/GraphQL/Schema.hs
diff options
context:
space:
mode:
authorDanny Navarro <j@dannynavarro.net>2017-02-26 16:07:00 -0300
committerDanny Navarro <j@dannynavarro.net>2017-02-28 16:07:00 -0300
commit1b8fca3658215c69402e2bc0f0c46d28e46d70e2 (patch)
treee3d2c3760c6ec720c3a1a60b3ca7cacc74f2e331 /Data/GraphQL/Schema.hs
parent642eab312f7b18619ff24e07a8863591f13ba07f (diff)
parentbada28ce24dcd0fcae95ebd7dd9a9ebb106e3842 (diff)
downloadgraphql-1b8fca3658215c69402e2bc0f0c46d28e46d70e2.tar.gz
Merge branch 'core'
This introduces a distinction between a Full and a Core AST. Fragments and variables are replaced when transforming from Full to Core.
Diffstat (limited to 'Data/GraphQL/Schema.hs')
-rw-r--r--Data/GraphQL/Schema.hs104
1 files changed, 44 insertions, 60 deletions
diff --git a/Data/GraphQL/Schema.hs b/Data/GraphQL/Schema.hs
index b8668d9..4acc4ac 100644
--- a/Data/GraphQL/Schema.hs
+++ b/Data/GraphQL/Schema.hs
@@ -1,9 +1,8 @@
-{-# 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(..)
+ ( Schema
, Resolver
, Subs
, object
@@ -14,55 +13,57 @@ module Data.GraphQL.Schema
, arrayA
, enum
, enumA
- , resolvers
- , fields
+ , resolve
-- * AST Reexports
, Field
, Argument(..)
, Value(..)
) where
-import Data.Bifunctor (first)
-import Data.Monoid (Alt(Alt,getAlt))
-import Control.Applicative (Alternative((<|>), empty))
-import Data.Maybe (catMaybes)
+import Control.Applicative (Alternative(empty))
import Data.Foldable (fold)
+import Data.List.NonEmpty (NonEmpty)
+import Data.Maybe (fromMaybe)
+import Data.Monoid (Alt(Alt,getAlt))
import qualified Data.Aeson as Aeson
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text)
-import qualified Data.Text as T (null, unwords)
-import Data.GraphQL.AST
-import Data.GraphQL.Error
+import Data.GraphQL.AST.Core
-- | A GraphQL schema.
-- @f@ is usually expected to be an instance of 'Alternative'.
-data Schema f = Schema [Resolver f]
+type Schema f = NonEmpty (Resolver f)
-- | Resolves a 'Field' into an @Aeson.@'Aeson.Object' with error information
-- (or 'empty'). @f@ is usually expected to be an instance of 'Alternative'.
-type Resolver f = Field -> CollectErrsT f Aeson.Object
+type Resolver f = Field -> f Aeson.Object
+
+type Resolvers f = [Resolver f]
+
+type Fields = [Field]
+
+type Arguments = [Argument]
-- | Variable substitution function.
-type Subs = Text -> Maybe Text
+type Subs = Name -> Maybe Value
--- | Create a named 'Resolver' from a list of 'Resolver's.
-object :: Alternative f => Text -> [Resolver f] -> Resolver f
-object name resolvs = objectA name $ \case
- [] -> resolvs
- _ -> empty
+-- | 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
-- | Like 'object' but also taking 'Argument's.
objectA
:: Alternative f
- => Text -> ([Argument] -> [Resolver f]) -> Resolver f
-objectA name f fld@(Field _ _ args _ sels) =
- withField name (resolvers (f args) $ fields sels) fld
+ => Name -> (Arguments -> Resolvers f) -> Resolver f
+objectA name f fld@(Field _ _ args flds) = withField name (resolve (f args) flds) fld
-- | A scalar represents a primitive value, like a string or an integer.
-scalar :: (Alternative f, Aeson.ToJSON a) => Text -> a -> Resolver f
+scalar :: (Alternative f, Aeson.ToJSON a) => Name -> a -> Resolver f
scalar name s = scalarA name $ \case
[] -> pure s
_ -> empty
@@ -70,22 +71,21 @@ scalar name s = scalarA name $ \case
-- | Like 'scalar' but also taking 'Argument's.
scalarA
:: (Alternative f, Aeson.ToJSON a)
- => Text -> ([Argument] -> f a) -> Resolver f
-scalarA name f fld@(Field _ _ args _ []) = withField name (errWrap $ f args) fld
+ => Name -> (Arguments -> f a) -> Resolver f
+scalarA name f fld@(Field _ _ args []) = withField name (f args) fld
scalarA _ _ _ = empty
--- | Like 'object' but taking lists of 'Resolver's instead of a single list.
-array :: Alternative f => Text -> [[Resolver f]] -> Resolver f
-array name resolvs = arrayA name $ \case
- [] -> resolvs
+array :: Alternative f => Name -> [Resolvers f] -> Resolver f
+array name resolvers = arrayA name $ \case
+ [] -> resolvers
_ -> empty
-- | Like 'array' but also taking 'Argument's.
arrayA
:: Alternative f
- => Text -> ([Argument] -> [[Resolver f]]) -> Resolver f
-arrayA name f fld@(Field _ _ args _ sels) =
- withField name (joinErrs $ traverse (flip resolvers $ fields sels) $ f args) fld
+ => Text -> (Arguments -> [Resolvers f]) -> Resolver f
+arrayA name f fld@(Field _ _ args sels) =
+ withField name (traverse (`resolve` sels) $ f args) fld
-- | Represents one of a finite set of possible values.
-- Used in place of a 'scalar' when the possible responses are easily enumerable.
@@ -96,40 +96,24 @@ enum name enums = enumA name $ \case
-- | Like 'enum' but also taking 'Argument's.
enumA :: Alternative f => Text -> ([Argument] -> f [Text]) -> Resolver f
-enumA name f fld@(Field _ _ args _ []) = withField name (errWrap $ f args) fld
+enumA name f fld@(Field _ _ args []) = withField name (f args) fld
enumA _ _ _ = empty
-- | Helper function to facilitate 'Argument' handling.
withField
:: (Alternative f, Aeson.ToJSON a)
- => Text -> CollectErrsT f a -> Field -> CollectErrsT f (HashMap Text Aeson.Value)
-withField name f (Field alias name' _ _ _) =
- if name == name'
- then fmap (first $ HashMap.singleton aliasOrName . Aeson.toJSON) f
- else empty
- where
- aliasOrName = if T.null alias then name' else alias
+ => Name -> f a -> Field -> f (HashMap Text Aeson.Value)
+withField name f (Field alias name' _ _) =
+ if name == name'
+ then fmap (HashMap.singleton aliasOrName . Aeson.toJSON) f
+ else 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.
-resolvers :: Alternative f => [Resolver f] -> [Field] -> CollectErrsT f Aeson.Value
-resolvers resolvs =
- fmap (first Aeson.toJSON . fold)
- . 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'.
-field :: Selection -> Maybe Field
-field (SelectionField x) = Just x
-field _ = Nothing
-
--- | Returns a list of the 'Field's contained in the given 'SelectionSet'.
-fields :: SelectionSet -> [Field]
-fields = catMaybes . fmap field
+resolve :: Alternative f => Resolvers f -> Fields -> f Aeson.Value
+resolve resolvers =
+ fmap (Aeson.toJSON . fold)
+ . traverse (\fld -> getAlt (foldMap (Alt . ($ fld)) resolvers))