2015-10-17 13:19:00 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2016-01-26 12:43:18 +01:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
2015-10-17 13:19:00 +02:00
|
|
|
module Data.GraphQL.Execute where
|
|
|
|
|
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
2016-01-26 13:53:15 +01:00
|
|
|
import Control.Applicative ((<$>), Applicative)
|
2015-10-17 13:19:00 +02:00
|
|
|
#endif
|
|
|
|
|
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-26 12:43:18 +01:00
|
|
|
execute :: Schema -> Document -> Maybe Response
|
|
|
|
execute (Schema resolv0) doc = go resolv0 =<< root doc
|
|
|
|
where
|
|
|
|
|
|
|
|
root :: Document -> Maybe Selection
|
|
|
|
root (Document [DefinitionOperation (Query (Node _ _ _ [sel]))]) = Just sel
|
|
|
|
root _ = error "root: Not implemented yet"
|
|
|
|
|
|
|
|
go :: Resolver -> Selection -> Maybe Response
|
|
|
|
go resolv (SelectionField (Field _ n _ _ sfs)) =
|
|
|
|
case resolv (InputField n) of
|
|
|
|
(OutputScalar s) -> if null sfs
|
|
|
|
then Just $ Aeson.Object [(n, Aeson.toJSON s)]
|
|
|
|
else Nothing
|
|
|
|
(OutputResolver resolv') -> (\r-> Aeson.Object [(n, r)]) <$> go resolv' (head sfs)
|
|
|
|
_ -> error "go case resolv: Not implemented yet"
|
|
|
|
go _ _ = error "go: Not implemented yet"
|