2015-10-17 13:19:00 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2016-01-30 12:29:49 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2015-10-17 13:19:00 +02:00
|
|
|
module Data.GraphQL.Execute where
|
|
|
|
|
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
2016-01-30 12:29:49 +01:00
|
|
|
import Control.Applicative (Applicative, (<$>), pure)
|
2016-02-08 17:30:18 +01:00
|
|
|
import Data.Traversable (traverse)
|
2015-10-17 13:19:00 +02:00
|
|
|
#endif
|
2016-01-30 12:29:49 +01:00
|
|
|
import Control.Applicative (Alternative, empty)
|
2016-02-08 17:30:18 +01:00
|
|
|
import Data.Foldable (fold)
|
2015-10-17 13:19:00 +02:00
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
import qualified Data.Aeson as Aeson
|
2016-02-08 17:30:18 +01:00
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
2015-10-17 13:19:00 +02:00
|
|
|
|
|
|
|
import Data.GraphQL.AST
|
|
|
|
import Data.GraphQL.Schema
|
|
|
|
|
2016-02-08 17:30:18 +01:00
|
|
|
execute :: (Alternative f, Monad f) => Schema f -> Document -> f Aeson.Value
|
|
|
|
execute (Schema resolv) doc = selectionSet resolv =<< query doc
|
|
|
|
|
|
|
|
query :: Applicative f => Document -> f SelectionSet
|
|
|
|
query (Document [DefinitionOperation (Query (Node _ _ _ sels))]) = pure sels
|
|
|
|
query _ = error "query: Not implemented yet"
|
|
|
|
|
|
|
|
selectionSet :: (Alternative f, Monad f) => Resolver f -> SelectionSet -> f Aeson.Value
|
|
|
|
selectionSet resolv sels = Aeson.Object . fold <$> traverse (selection resolv) sels
|
|
|
|
|
|
|
|
selection :: (Alternative f, Monad f) => Resolver f -> Selection -> f Aeson.Object
|
|
|
|
selection resolv (SelectionField (Field _ n _ _ sfs)) =
|
|
|
|
fmap (HashMap.singleton n) $ output sfs =<< resolv (InputField n)
|
|
|
|
selection _ _ = error "selection: Not implemented yet"
|
|
|
|
|
|
|
|
output :: (Alternative f, Monad f) => SelectionSet -> Output f -> f Aeson.Value
|
|
|
|
output sels (OutputResolver resolv) = selectionSet resolv sels
|
|
|
|
output sels (OutputList os) = fmap array . traverse (output sels) =<< os
|
2016-02-09 13:31:28 +01:00
|
|
|
output sels (OutputEnum e)
|
|
|
|
| null sels = Aeson.toJSON <$> e
|
|
|
|
| otherwise = empty
|
2016-02-08 17:30:18 +01:00
|
|
|
output sels (OutputScalar s)
|
|
|
|
| null sels = Aeson.toJSON <$> s
|
|
|
|
| otherwise = empty
|
|
|
|
|
|
|
|
array :: [Aeson.Value] -> Aeson.Value
|
|
|
|
array = Aeson.toJSON
|