2015-10-17 13:19:00 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
module Data.GraphQL.Execute where
|
|
|
|
|
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
2016-02-11 14:24:31 +01:00
|
|
|
import Control.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)
|
2016-02-15 14:25:15 +01:00
|
|
|
import Data.Maybe (catMaybes)
|
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
|
2016-02-11 14:24:31 +01:00
|
|
|
import Data.GraphQL.Schema (Resolver, Schema(..))
|
|
|
|
import qualified Data.GraphQL.Schema as Schema
|
2015-10-17 13:19:00 +02:00
|
|
|
|
2016-02-15 14:25:15 +01:00
|
|
|
execute :: (Alternative m, Monad m) => Schema m -> Schema.Subs -> Document -> m Aeson.Value
|
|
|
|
execute (Schema resolv) f doc = selectionSet f resolv =<< query doc
|
2016-02-08 17:30:18 +01:00
|
|
|
|
2016-02-11 14:24:31 +01:00
|
|
|
query :: Alternative f => Document -> f SelectionSet
|
2016-02-15 14:25:15 +01:00
|
|
|
query (Document [DefinitionOperation (Query (Node _varDefs _ _ sels))]) =
|
|
|
|
pure sels
|
|
|
|
query _ = empty
|
2016-02-11 14:24:31 +01:00
|
|
|
|
2016-02-15 14:25:15 +01:00
|
|
|
selectionSet :: Alternative f => Schema.Subs -> Resolver f -> SelectionSet -> f Aeson.Value
|
|
|
|
selectionSet f resolv = fmap (Aeson.Object . fold) . traverse (selection f resolv)
|
2016-02-11 14:24:31 +01:00
|
|
|
|
2016-02-15 14:25:15 +01:00
|
|
|
selection :: Alternative f => Schema.Subs -> Resolver f -> Selection -> f Aeson.Object
|
|
|
|
selection f resolv (SelectionField field@(Field _ name _ _ _)) =
|
|
|
|
fmap (HashMap.singleton name) $ Aeson.toJSON <$> resolv (fieldToInput f field)
|
|
|
|
selection _ _ _ = empty
|
2016-02-11 14:24:31 +01:00
|
|
|
|
|
|
|
-- * AST/Schema conversions
|
|
|
|
|
2016-02-15 14:25:15 +01:00
|
|
|
argument :: Schema.Subs -> Argument -> Maybe Schema.Argument
|
|
|
|
argument f (Argument n (ValueVariable (Variable v))) =
|
|
|
|
maybe Nothing (\v' -> Just (n, v')) $ f v
|
|
|
|
argument _ (Argument n (ValueInt v)) =
|
|
|
|
Just (n, Schema.ScalarInt $ fromIntegral v)
|
|
|
|
argument _ (Argument n (ValueString (StringValue v))) =
|
|
|
|
Just (n, Schema.ScalarString v)
|
|
|
|
argument _ _ = error "argument: not implemented yet"
|
|
|
|
|
|
|
|
fieldToInput :: Schema.Subs -> Field -> Schema.Input
|
|
|
|
fieldToInput f (Field _ n as _ sels) =
|
|
|
|
Schema.InputField n (catMaybes $ argument f <$> as)
|
|
|
|
(fieldToInput f . selectionToField <$> sels)
|
2016-02-11 14:24:31 +01:00
|
|
|
|
|
|
|
selectionToField :: Selection -> Field
|
|
|
|
selectionToField (SelectionField x) = x
|
|
|
|
selectionToField _ = error "selectionField: not implemented yet"
|