From 5390c4ca1e7e6bcf36dbe5e773c1355dd4b65939 Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Sat, 28 Jan 2017 14:15:14 -0300 Subject: Split AST in 2 One AST is meant to be a target parser and tries to adhere as much as possible to the spec. The other is a simplified version of that AST meant for execution. Also newtypes have been replaced by type synonyms and NonEmpty lists are being used where it makes sense. --- Data/GraphQL/Execute.hs | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'Data/GraphQL/Execute.hs') diff --git a/Data/GraphQL/Execute.hs b/Data/GraphQL/Execute.hs index a7e3c91..4dab56c 100644 --- a/Data/GraphQL/Execute.hs +++ b/Data/GraphQL/Execute.hs @@ -21,38 +21,37 @@ import Data.GraphQL.Error -- errors wrapped in an /errors/ field. execute :: Alternative f => Schema.Schema f -> Schema.Subs -> Document -> f Aeson.Value -execute (Schema resolvs) subs doc = runCollectErrs res - where res = Schema.resolvers resolvs $ rootFields subs doc +execute resolvers subs doc = undefined -- resolver resolvs $ rootFields subs doc -- | Takes a variable substitution function and a @GraphQL@ document. -- If the document contains one query (and no other definitions) -- it applies the substitution to the query's set of selections -- and then returns their fields. -rootFields :: Schema.Subs -> Document -> [Field] -rootFields subs (Document [DefinitionOperation (Query (Node _varDefs _ _ sels))]) = - Schema.fields $ substitute subs <$> sels -rootFields _ _ = [] +-- rootFields :: Schema.Subs -> Document -> [Field] +-- rootFields subs (Document [DefinitionOperation (Query (Node _varDefs _ _ sels))]) = +-- Schema.fields $ substitute subs <$> sels +-- rootFields _ _ = [] -- | Takes a variable substitution function and a selection. If the -- selection is a field it applies the substitution to the field's -- arguments using 'subsArg', and recursively applies the substitution to -- the arguments of fields nested in the primary field. -substitute :: Schema.Subs -> Selection -> Selection -substitute subs (SelectionField (Field alias name args directives sels)) = - SelectionField $ Field - alias - name - -- TODO: Get rid of `catMaybes`, invalid arguments should raise an error - (catMaybes $ subsArg subs <$> args) - directives - (substitute subs <$> sels) -substitute _ sel = sel +-- substitute :: Schema.Subs -> Selection -> Selection +-- substitute subs (SelectionField (Field alias name args directives sels)) = +-- SelectionField $ Field +-- alias +-- name +-- -- TODO: Get rid of `catMaybes`, invalid arguments should raise an error +-- (catMaybes $ subsArg subs <$> args) +-- directives +-- (substitute subs <$> sels) +-- substitute _ sel = sel -- TODO: Support different value types -- | Takes a variable substitution function and an argument. If the -- argument's value is a variable the substitution is applied to the -- variable's name. -subsArg :: Schema.Subs -> Argument -> Maybe Argument -subsArg subs (Argument n (ValueVariable (Variable v))) = - Argument n . ValueString <$> subs v -subsArg _ arg = Just arg +-- subsArg :: Schema.Subs -> Argument -> Maybe Argument +-- subsArg subs (Argument n (ValueVariable (Variable v))) = +-- Argument n . ValueString <$> subs v +-- subsArg _ arg = Just arg -- cgit v1.2.3 From f35e1f949ab3ee718ab773baf9f38ac411d49a28 Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Sun, 29 Jan 2017 12:53:15 -0300 Subject: Define Schema using Core AST Also, temporarily remove error reporting to simplify execution. This should be restored once the new execution model is nailed. --- Data/GraphQL/Execute.hs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'Data/GraphQL/Execute.hs') diff --git a/Data/GraphQL/Execute.hs b/Data/GraphQL/Execute.hs index 4dab56c..561bf20 100644 --- a/Data/GraphQL/Execute.hs +++ b/Data/GraphQL/Execute.hs @@ -3,15 +3,23 @@ module Data.GraphQL.Execute (execute) where import Control.Applicative (Alternative) -import Data.Maybe (catMaybes) +import qualified Data.List.NonEmpty as NE +import Data.List.NonEmpty (NonEmpty((:|))) import qualified Data.Aeson as Aeson -import Data.GraphQL.AST -import Data.GraphQL.Schema (Schema(..)) +import qualified Data.GraphQL.AST as AST +import qualified Data.GraphQL.AST.Core as AST.Core +import Data.GraphQL.Schema (Schema) import qualified Data.GraphQL.Schema as Schema -import Data.GraphQL.Error + + +core :: Schema.Subs -> AST.Document -> AST.Core.Document +core subs ((AST.DefinitionOperation opDef) :| []) = error "Not implemented yet" +core _ ((AST.DefinitionFragment fragDef) :| []) = + error "Fragment definitions not supported yet" +core _ _ = error "Multiple definitions not supported yet" -- | Takes a 'Schema', a variable substitution function ('Schema.Subs'), and a -- @GraphQL@ 'document'. The substitution is applied to the document using @@ -19,9 +27,19 @@ import Data.GraphQL.Error -- -- Returns the result of the query against the 'Schema' wrapped in a /data/ field, or -- errors wrapped in an /errors/ field. -execute :: Alternative f - => Schema.Schema f -> Schema.Subs -> Document -> f Aeson.Value -execute resolvers subs doc = undefined -- resolver resolvs $ rootFields subs doc +execute + :: Alternative f + => Schema f -> Schema.Subs -> AST.Document -> f Aeson.Value +execute schema subs doc = document schema $ core subs doc + +document :: Alternative f => Schema f -> AST.Core.Document -> f Aeson.Value +document schema (op :| [])= operation schema op +document _ _ = error "Multiple operations not supported yet" + +operation :: Alternative f => Schema f -> AST.Core.Operation -> f Aeson.Value +operation schema (AST.Core.Query flds) = + Schema.resolve (NE.toList schema) (NE.toList flds) +operation _ _ = error "Mutations not supported yet" -- | Takes a variable substitution function and a @GraphQL@ document. -- If the document contains one query (and no other definitions) -- cgit v1.2.3 From 693b7d18dcd48525b10ce297f89b3b33fd020784 Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Sun, 29 Jan 2017 18:44:03 -0300 Subject: Introduce Tranform module In the Transform module the Full AST will converted to Core AST. This commit also includes a partial implementation of Fragment replacement. --- Data/GraphQL/Execute.hs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'Data/GraphQL/Execute.hs') diff --git a/Data/GraphQL/Execute.hs b/Data/GraphQL/Execute.hs index 561bf20..869753a 100644 --- a/Data/GraphQL/Execute.hs +++ b/Data/GraphQL/Execute.hs @@ -10,17 +10,10 @@ import qualified Data.Aeson as Aeson import qualified Data.GraphQL.AST as AST import qualified Data.GraphQL.AST.Core as AST.Core +import qualified Data.GraphQL.AST.Transform as Transform import Data.GraphQL.Schema (Schema) import qualified Data.GraphQL.Schema as Schema - - -core :: Schema.Subs -> AST.Document -> AST.Core.Document -core subs ((AST.DefinitionOperation opDef) :| []) = error "Not implemented yet" -core _ ((AST.DefinitionFragment fragDef) :| []) = - error "Fragment definitions not supported yet" -core _ _ = error "Multiple definitions not supported yet" - -- | Takes a 'Schema', a variable substitution function ('Schema.Subs'), and a -- @GraphQL@ 'document'. The substitution is applied to the document using -- 'rootFields', and the 'Schema''s resolvers are applied to the resulting fields. @@ -30,7 +23,7 @@ core _ _ = error "Multiple definitions not supported yet" execute :: Alternative f => Schema f -> Schema.Subs -> AST.Document -> f Aeson.Value -execute schema subs doc = document schema $ core subs doc +execute schema subs doc = document schema $ Transform.document subs doc document :: Alternative f => Schema f -> AST.Core.Document -> f Aeson.Value document schema (op :| [])= operation schema op -- cgit v1.2.3 From 4ab4660d364cc62c9e23d2cdc85abc3f7dc6dc8d Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Fri, 3 Feb 2017 21:48:26 -0300 Subject: Initial implementation of AST.Full -> AST.Core This focused mainly on fragments. --- Data/GraphQL/Execute.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Data/GraphQL/Execute.hs') diff --git a/Data/GraphQL/Execute.hs b/Data/GraphQL/Execute.hs index 869753a..52537a4 100644 --- a/Data/GraphQL/Execute.hs +++ b/Data/GraphQL/Execute.hs @@ -2,7 +2,7 @@ -- according to a 'Schema'. module Data.GraphQL.Execute (execute) where -import Control.Applicative (Alternative) +import Control.Applicative (Alternative, empty) import qualified Data.List.NonEmpty as NE import Data.List.NonEmpty (NonEmpty((:|))) @@ -21,9 +21,9 @@ import qualified Data.GraphQL.Schema as Schema -- Returns the result of the query against the 'Schema' wrapped in a /data/ field, or -- errors wrapped in an /errors/ field. execute - :: Alternative f + :: (Alternative f, Monad f) => Schema f -> Schema.Subs -> AST.Document -> f Aeson.Value -execute schema subs doc = document schema $ Transform.document subs doc +execute schema subs doc = document schema =<< maybe empty pure (Transform.document subs doc) document :: Alternative f => Schema f -> AST.Core.Document -> f Aeson.Value document schema (op :| [])= operation schema op -- cgit v1.2.3 From e716bc57e786e1d9b733c3a2782fdf27007b3e23 Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Fri, 10 Feb 2017 18:40:08 -0300 Subject: Wrap executed result in "data" object --- Data/GraphQL/Execute.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'Data/GraphQL/Execute.hs') diff --git a/Data/GraphQL/Execute.hs b/Data/GraphQL/Execute.hs index 52537a4..fe78323 100644 --- a/Data/GraphQL/Execute.hs +++ b/Data/GraphQL/Execute.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE OverloadedStrings #-} -- | This module provides the function to execute a @GraphQL@ request -- -- according to a 'Schema'. module Data.GraphQL.Execute (execute) where @@ -7,6 +8,7 @@ import qualified Data.List.NonEmpty as NE import Data.List.NonEmpty (NonEmpty((:|))) import qualified Data.Aeson as Aeson +import qualified Data.HashMap.Strict as HashMap import qualified Data.GraphQL.AST as AST import qualified Data.GraphQL.AST.Core as AST.Core @@ -31,7 +33,8 @@ document _ _ = error "Multiple operations not supported yet" operation :: Alternative f => Schema f -> AST.Core.Operation -> f Aeson.Value operation schema (AST.Core.Query flds) = - Schema.resolve (NE.toList schema) (NE.toList flds) + Aeson.Object . HashMap.singleton "data" + <$> Schema.resolve (NE.toList schema) (NE.toList flds) operation _ _ = error "Mutations not supported yet" -- | Takes a variable substitution function and a @GraphQL@ document. -- cgit v1.2.3 From d2c138f8d16acadb8ae2ba410484d985dde1e37c Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Sun, 19 Feb 2017 15:29:58 -0300 Subject: Add basic Fragment Support Only field names are supported for now. --- Data/GraphQL/Execute.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Data/GraphQL/Execute.hs') diff --git a/Data/GraphQL/Execute.hs b/Data/GraphQL/Execute.hs index fe78323..7609561 100644 --- a/Data/GraphQL/Execute.hs +++ b/Data/GraphQL/Execute.hs @@ -28,7 +28,7 @@ execute execute schema subs doc = document schema =<< maybe empty pure (Transform.document subs doc) document :: Alternative f => Schema f -> AST.Core.Document -> f Aeson.Value -document schema (op :| [])= operation schema op +document schema (op :| []) = operation schema op document _ _ = error "Multiple operations not supported yet" operation :: Alternative f => Schema f -> AST.Core.Operation -> f Aeson.Value -- cgit v1.2.3