2015-10-17 13:19:00 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2016-01-26 12:43:18 +01:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
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)
|
2015-10-17 13:19:00 +02:00
|
|
|
#endif
|
2016-01-30 12:29:49 +01:00
|
|
|
import Control.Applicative (Alternative, empty)
|
2015-10-17 13:19:00 +02:00
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
import qualified Data.Aeson as Aeson
|
2015-10-17 13:19:00 +02:00
|
|
|
|
|
|
|
import Data.GraphQL.AST
|
|
|
|
import Data.GraphQL.Schema
|
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
type Response = Aeson.Value
|
|
|
|
|
2016-01-30 12:29:49 +01:00
|
|
|
execute :: (Alternative f, Monad f) => Schema f -> Document -> f Response
|
2016-01-26 12:43:18 +01:00
|
|
|
execute (Schema resolv0) doc = go resolv0 =<< root doc
|
|
|
|
where
|
2016-01-30 12:29:49 +01:00
|
|
|
root :: Applicative f => Document -> f Selection
|
|
|
|
root (Document [DefinitionOperation (Query (Node _ _ _ [sel]))]) = pure sel
|
2016-01-26 12:43:18 +01:00
|
|
|
root _ = error "root: Not implemented yet"
|
|
|
|
|
2016-01-30 12:29:49 +01:00
|
|
|
go :: (Alternative f, Monad f) => Resolver f -> Selection -> f Response
|
2016-01-26 12:43:18 +01:00
|
|
|
go resolv (SelectionField (Field _ n _ _ sfs)) =
|
2016-01-30 12:29:49 +01:00
|
|
|
resolv (InputField n) >>= \case
|
|
|
|
(OutputScalar s) ->
|
|
|
|
if null sfs
|
|
|
|
then (\s' -> Aeson.Object [(n, Aeson.toJSON s')]) <$> s
|
|
|
|
else empty
|
2016-01-26 12:43:18 +01:00
|
|
|
(OutputResolver resolv') -> (\r-> Aeson.Object [(n, r)]) <$> go resolv' (head sfs)
|
|
|
|
_ -> error "go case resolv: Not implemented yet"
|
|
|
|
go _ _ = error "go: Not implemented yet"
|