Initial support for aliases in execute

This commit is contained in:
Danny Navarro 2016-02-17 12:59:35 +01:00
parent a0f12455c5
commit 7131d1c142
2 changed files with 35 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import Data.Maybe (catMaybes)
import qualified Data.Aeson as Aeson import qualified Data.Aeson as Aeson
import qualified Data.HashMap.Strict as HashMap import qualified Data.HashMap.Strict as HashMap
import qualified Data.Text as T
import Data.GraphQL.AST import Data.GraphQL.AST
import Data.GraphQL.Schema (Resolver, Schema(..)) 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) selectionSet f resolv = fmap (Aeson.Object . fold) . traverse (selection f resolv)
selection :: Alternative f => Schema.Subs -> Resolver f -> Selection -> f Aeson.Object selection :: Alternative f => Schema.Subs -> Resolver f -> Selection -> f Aeson.Object
selection f resolv (SelectionField field@(Field _ name _ _ _)) = selection f resolv (SelectionField field@(Field alias name _ _ _)) =
fmap (HashMap.singleton name) $ Aeson.toJSON <$> resolv (fieldToInput f field) fmap (HashMap.singleton aliasOrName) $ Aeson.toJSON <$> resolv (fieldToInput f field)
where
aliasOrName = if T.null alias then name else alias
selection _ _ _ = empty selection _ _ _ = empty
-- * AST/Schema conversions -- * AST/Schema conversions

View File

@ -165,4 +165,34 @@ test = testGroup "Star Wars Query Tests"
-- } -- }
-- |] -- |]
-- $ object ["human" .= Aeson.Null] -- $ 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)
]
]
] ]