From 7131d1c142dc9c9fc5331ee3ebf90cf9154f85d9 Mon Sep 17 00:00:00 2001 From: Danny Navarro Date: Wed, 17 Feb 2016 12:59:35 +0100 Subject: [PATCH] Initial support for aliases in `execute` --- Data/GraphQL/Execute.hs | 7 +++++-- tests/Test/StarWars/QueryTests.hs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Data/GraphQL/Execute.hs b/Data/GraphQL/Execute.hs index 9d20e5c..1f921aa 100644 --- a/Data/GraphQL/Execute.hs +++ b/Data/GraphQL/Execute.hs @@ -11,6 +11,7 @@ import Data.Maybe (catMaybes) import qualified Data.Aeson as Aeson import qualified Data.HashMap.Strict as HashMap +import qualified Data.Text as T import Data.GraphQL.AST import Data.GraphQL.Schema (Resolver, Schema(..)) @@ -28,8 +29,10 @@ selectionSet :: Alternative f => Schema.Subs -> Resolver f -> SelectionSet -> f selectionSet f resolv = fmap (Aeson.Object . fold) . traverse (selection f resolv) 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 f resolv (SelectionField field@(Field alias name _ _ _)) = + fmap (HashMap.singleton aliasOrName) $ Aeson.toJSON <$> resolv (fieldToInput f field) + where + aliasOrName = if T.null alias then name else alias selection _ _ _ = empty -- * AST/Schema conversions diff --git a/tests/Test/StarWars/QueryTests.hs b/tests/Test/StarWars/QueryTests.hs index 1f7dc0d..5ffb4b0 100644 --- a/tests/Test/StarWars/QueryTests.hs +++ b/tests/Test/StarWars/QueryTests.hs @@ -165,4 +165,34 @@ test = testGroup "Star Wars Query Tests" -- } -- |] -- $ object ["human" .= Aeson.Null] + , testCase "Luke with alias" . testQuery + [r| query FetchLukeAliased { + luke: human(id: "1000") { + name + } + } + |] + $ object [ + "luke" .= object [ + "name" .= ("Luke Skywalker" :: Text) + ] + ] + , testCase "Luke and Leia aliased" . testQuery + [r| query FetchLukeAndLeiaAliased { + luke: human(id: "1000") { + name + } + leia: human(id: "1003") { + name + } + } + |] + $ object [ + "luke" .= object [ + "name" .= ("Luke Skywalker" :: Text) + ] + , "leia" .= object [ + "name" .= ("Leia Organa" :: Text) + ] + ] ]