2016-02-12 13:27:46 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2019-07-10 05:57:35 +02:00
|
|
|
module Test.StarWars.QuerySpec
|
|
|
|
( spec
|
|
|
|
) where
|
2016-02-12 13:27:46 +01:00
|
|
|
|
2019-07-02 20:07:26 +02:00
|
|
|
import qualified Data.Aeson as Aeson
|
2019-12-30 18:26:24 +01:00
|
|
|
import Data.Aeson ((.=))
|
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
2016-02-12 13:27:46 +01:00
|
|
|
import Data.Text (Text)
|
2019-07-07 06:31:53 +02:00
|
|
|
import Language.GraphQL
|
2016-02-12 13:27:46 +01:00
|
|
|
import Text.RawString.QQ (r)
|
2019-12-30 18:26:24 +01:00
|
|
|
import Test.Hspec.Expectations (Expectation, shouldBe)
|
|
|
|
import Test.Hspec (Spec, describe, it)
|
2016-02-12 13:27:46 +01:00
|
|
|
import Test.StarWars.Schema
|
|
|
|
|
|
|
|
-- * Test
|
|
|
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsQueryTests.js
|
|
|
|
|
2019-07-10 05:57:35 +02:00
|
|
|
spec :: Spec
|
|
|
|
spec = describe "Star Wars Query Tests" $ do
|
|
|
|
describe "Basic Queries" $ do
|
|
|
|
it "R2-D2 hero" $ testQuery
|
2016-02-15 11:19:05 +01:00
|
|
|
[r| query HeroNameQuery {
|
2019-07-10 05:57:35 +02:00
|
|
|
hero {
|
2016-02-15 11:19:05 +01:00
|
|
|
id
|
2019-07-10 05:57:35 +02:00
|
|
|
}
|
2016-02-15 11:19:05 +01:00
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object
|
|
|
|
[ "data" .= Aeson.object
|
|
|
|
[ "hero" .= Aeson.object ["id" .= ("2001" :: Text)]
|
|
|
|
]
|
|
|
|
]
|
2019-07-10 05:57:35 +02:00
|
|
|
it "R2-D2 ID and friends" $ testQuery
|
2016-02-15 11:19:05 +01:00
|
|
|
[r| query HeroNameAndFriendsQuery {
|
2019-07-10 05:57:35 +02:00
|
|
|
hero {
|
2016-02-15 11:19:05 +01:00
|
|
|
id
|
|
|
|
name
|
|
|
|
friends {
|
2020-05-23 06:46:21 +02:00
|
|
|
name
|
2016-02-15 11:19:05 +01:00
|
|
|
}
|
2019-07-10 05:57:35 +02:00
|
|
|
}
|
2016-02-15 11:19:05 +01:00
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object [
|
|
|
|
"hero" .= Aeson.object
|
2019-07-10 05:57:35 +02:00
|
|
|
[ "id" .= ("2001" :: Text)
|
|
|
|
, r2d2Name
|
|
|
|
, "friends" .=
|
2019-12-30 18:26:24 +01:00
|
|
|
[ Aeson.object [lukeName]
|
|
|
|
, Aeson.object [hanName]
|
|
|
|
, Aeson.object [leiaName]
|
2019-07-10 05:57:35 +02:00
|
|
|
]
|
2016-02-15 11:19:05 +01:00
|
|
|
]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2019-07-10 05:57:35 +02:00
|
|
|
|
|
|
|
describe "Nested Queries" $ do
|
|
|
|
it "R2-D2 friends" $ testQuery
|
2016-02-15 11:19:05 +01:00
|
|
|
[r| query NestedQuery {
|
|
|
|
hero {
|
|
|
|
name
|
|
|
|
friends {
|
|
|
|
name
|
|
|
|
appearsIn
|
2020-06-13 07:20:19 +02:00
|
|
|
friends {
|
|
|
|
name
|
|
|
|
}
|
2016-02-15 11:19:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object [
|
|
|
|
"hero" .= Aeson.object [
|
2016-02-15 11:19:05 +01:00
|
|
|
"name" .= ("R2-D2" :: Text)
|
|
|
|
, "friends" .= [
|
2019-12-30 18:26:24 +01:00
|
|
|
Aeson.object [
|
2016-02-15 11:19:05 +01:00
|
|
|
"name" .= ("Luke Skywalker" :: Text)
|
2020-06-13 07:20:19 +02:00
|
|
|
, "appearsIn" .= ["NEW_HOPE", "EMPIRE", "JEDI" :: Text]
|
2016-02-15 11:19:05 +01:00
|
|
|
, "friends" .= [
|
2019-12-30 18:26:24 +01:00
|
|
|
Aeson.object [hanName]
|
|
|
|
, Aeson.object [leiaName]
|
|
|
|
, Aeson.object [c3poName]
|
|
|
|
, Aeson.object [r2d2Name]
|
2016-02-15 11:19:05 +01:00
|
|
|
]
|
|
|
|
]
|
2019-12-30 18:26:24 +01:00
|
|
|
, Aeson.object [
|
2016-07-08 22:10:58 +02:00
|
|
|
hanName
|
2020-06-13 07:20:19 +02:00
|
|
|
, "appearsIn" .= ["NEW_HOPE", "EMPIRE", "JEDI" :: Text]
|
2019-12-30 18:26:24 +01:00
|
|
|
, "friends" .=
|
|
|
|
[ Aeson.object [lukeName]
|
|
|
|
, Aeson.object [leiaName]
|
|
|
|
, Aeson.object [r2d2Name]
|
2016-02-15 11:19:05 +01:00
|
|
|
]
|
|
|
|
]
|
2019-12-30 18:26:24 +01:00
|
|
|
, Aeson.object [
|
2016-07-08 22:10:58 +02:00
|
|
|
leiaName
|
2020-06-13 07:20:19 +02:00
|
|
|
, "appearsIn" .= ["NEW_HOPE", "EMPIRE", "JEDI" :: Text]
|
2019-12-30 18:26:24 +01:00
|
|
|
, "friends" .=
|
|
|
|
[ Aeson.object [lukeName]
|
|
|
|
, Aeson.object [hanName]
|
|
|
|
, Aeson.object [c3poName]
|
|
|
|
, Aeson.object [r2d2Name]
|
2016-02-15 11:19:05 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2019-07-10 05:57:35 +02:00
|
|
|
it "Luke ID" $ testQuery
|
2016-02-15 11:19:05 +01:00
|
|
|
[r| query FetchLukeQuery {
|
|
|
|
human(id: "1000") {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object
|
|
|
|
[ "human" .= Aeson.object [lukeName]
|
|
|
|
]]
|
2019-07-10 05:57:35 +02:00
|
|
|
|
|
|
|
it "Luke ID with variable" $ testQueryParams
|
2019-12-30 18:26:24 +01:00
|
|
|
(HashMap.singleton "someId" "1000")
|
2019-07-10 05:57:35 +02:00
|
|
|
[r| query FetchSomeIDQuery($someId: String!) {
|
|
|
|
human(id: $someId) {
|
|
|
|
name
|
2016-02-17 12:35:54 +01:00
|
|
|
}
|
2019-07-10 05:57:35 +02:00
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object [
|
|
|
|
"human" .= Aeson.object [lukeName]
|
2019-07-10 05:57:35 +02:00
|
|
|
]]
|
|
|
|
it "Han ID with variable" $ testQueryParams
|
2019-12-30 18:26:24 +01:00
|
|
|
(HashMap.singleton "someId" "1002")
|
2019-07-10 05:57:35 +02:00
|
|
|
[r| query FetchSomeIDQuery($someId: String!) {
|
|
|
|
human(id: $someId) {
|
|
|
|
name
|
2017-03-04 00:39:03 +01:00
|
|
|
}
|
2019-07-10 05:57:35 +02:00
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object [
|
|
|
|
"human" .= Aeson.object [hanName]
|
2019-07-10 05:57:35 +02:00
|
|
|
]]
|
|
|
|
it "Invalid ID" $ testQueryParams
|
2019-12-30 18:26:24 +01:00
|
|
|
(HashMap.singleton "id" "Not a valid ID")
|
2019-07-10 05:57:35 +02:00
|
|
|
[r| query humanQuery($id: String!) {
|
|
|
|
human(id: $id) {
|
|
|
|
name
|
2016-02-17 12:59:35 +01:00
|
|
|
}
|
2019-07-10 05:57:35 +02:00
|
|
|
}
|
2019-12-30 18:26:24 +01:00
|
|
|
|] $ Aeson.object ["data" .= Aeson.object ["human" .= Aeson.Null]]
|
2019-07-10 05:57:35 +02:00
|
|
|
it "Luke aliased" $ testQuery
|
|
|
|
[r| query FetchLukeAliased {
|
|
|
|
luke: human(id: "1000") {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object [
|
|
|
|
"luke" .= Aeson.object [lukeName]
|
2019-07-10 05:57:35 +02:00
|
|
|
]]
|
|
|
|
it "R2-D2 ID and friends aliased" $ testQuery
|
|
|
|
[r| query HeroNameAndFriendsQuery {
|
|
|
|
hero {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
friends {
|
|
|
|
friendName: name
|
2016-02-17 18:13:10 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 05:57:35 +02:00
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object [
|
|
|
|
"hero" .= Aeson.object [
|
2019-07-10 05:57:35 +02:00
|
|
|
"id" .= ("2001" :: Text)
|
|
|
|
, r2d2Name
|
2019-12-30 18:26:24 +01:00
|
|
|
, "friends" .=
|
|
|
|
[ Aeson.object ["friendName" .= ("Luke Skywalker" :: Text)]
|
|
|
|
, Aeson.object ["friendName" .= ("Han Solo" :: Text)]
|
|
|
|
, Aeson.object ["friendName" .= ("Leia Organa" :: Text)]
|
2019-07-10 05:57:35 +02:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]]
|
|
|
|
it "Luke and Leia aliased" $ testQuery
|
|
|
|
[r| query FetchLukeAndLeiaAliased {
|
|
|
|
luke: human(id: "1000") {
|
|
|
|
name
|
2016-02-17 12:59:35 +01:00
|
|
|
}
|
2019-07-10 05:57:35 +02:00
|
|
|
leia: human(id: "1003") {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object
|
|
|
|
[ "luke" .= Aeson.object [lukeName]
|
|
|
|
, "leia" .= Aeson.object [leiaName]
|
2019-07-10 05:57:35 +02:00
|
|
|
]]
|
|
|
|
|
|
|
|
describe "Fragments for complex queries" $ do
|
|
|
|
it "Aliases to query for duplicate content" $ testQuery
|
2016-07-08 22:10:58 +02:00
|
|
|
[r| query DuplicateFields {
|
|
|
|
luke: human(id: "1000") {
|
|
|
|
name
|
|
|
|
homePlanet
|
|
|
|
}
|
|
|
|
leia: human(id: "1003") {
|
|
|
|
name
|
|
|
|
homePlanet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object [
|
|
|
|
"luke" .= Aeson.object [lukeName, tatooine]
|
|
|
|
, "leia" .= Aeson.object [leiaName, alderaan]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2019-07-10 05:57:35 +02:00
|
|
|
it "Fragment for duplicate content" $ testQuery
|
2016-07-08 22:10:58 +02:00
|
|
|
[r| query UseFragment {
|
|
|
|
luke: human(id: "1000") {
|
|
|
|
...HumanFragment
|
|
|
|
}
|
|
|
|
leia: human(id: "1003") {
|
|
|
|
...HumanFragment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment HumanFragment on Human {
|
|
|
|
name
|
|
|
|
homePlanet
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object [ "data" .= Aeson.object [
|
|
|
|
"luke" .= Aeson.object [lukeName, tatooine]
|
|
|
|
, "leia" .= Aeson.object [leiaName, alderaan]
|
2016-07-08 22:10:58 +02:00
|
|
|
]]
|
2019-07-10 05:57:35 +02:00
|
|
|
|
|
|
|
describe "__typename" $ do
|
|
|
|
it "R2D2 is a Droid" $ testQuery
|
2016-07-08 22:36:45 +02:00
|
|
|
[r| query CheckTypeOfR2 {
|
|
|
|
hero {
|
|
|
|
__typename
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object ["data" .= Aeson.object [
|
|
|
|
"hero" .= Aeson.object
|
|
|
|
[ "__typename" .= ("Droid" :: Text)
|
|
|
|
, r2d2Name
|
|
|
|
]
|
2019-07-10 05:57:35 +02:00
|
|
|
]]
|
|
|
|
it "Luke is a human" $ testQuery
|
2016-07-08 22:36:45 +02:00
|
|
|
[r| query CheckTypeOfLuke {
|
|
|
|
hero(episode: EMPIRE) {
|
|
|
|
__typename
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object ["data" .= Aeson.object [
|
|
|
|
"hero" .= Aeson.object
|
|
|
|
[ "__typename" .= ("Human" :: Text)
|
|
|
|
, lukeName
|
|
|
|
]
|
2019-07-10 05:57:35 +02:00
|
|
|
]]
|
|
|
|
|
|
|
|
describe "Errors in resolvers" $ do
|
|
|
|
it "error on secretBackstory" $ testQuery
|
2019-07-02 20:07:26 +02:00
|
|
|
[r|
|
|
|
|
query HeroNameQuery {
|
|
|
|
hero {
|
|
|
|
name
|
2016-07-08 23:50:31 +02:00
|
|
|
secretBackstory
|
|
|
|
}
|
2019-07-02 20:07:26 +02:00
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object
|
|
|
|
[ "data" .= Aeson.object
|
|
|
|
[ "hero" .= Aeson.object
|
2019-07-02 20:07:26 +02:00
|
|
|
[ "name" .= ("R2-D2" :: Text)
|
|
|
|
, "secretBackstory" .= Aeson.Null
|
|
|
|
]
|
|
|
|
]
|
|
|
|
, "errors" .=
|
2019-12-30 18:26:24 +01:00
|
|
|
[ Aeson.object
|
2019-07-02 20:07:26 +02:00
|
|
|
["message" .= ("secretBackstory is secret." :: Text)]
|
|
|
|
]
|
|
|
|
]
|
2019-07-10 05:57:35 +02:00
|
|
|
it "Error in a list" $ testQuery
|
2016-07-08 23:50:31 +02:00
|
|
|
[r| query HeroNameQuery {
|
|
|
|
hero {
|
|
|
|
name
|
|
|
|
friends {
|
|
|
|
name
|
|
|
|
secretBackstory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object ["data" .= Aeson.object
|
|
|
|
[ "hero" .= Aeson.object
|
2019-07-10 05:57:35 +02:00
|
|
|
[ "name" .= ("R2-D2" :: Text)
|
|
|
|
, "friends" .=
|
2019-12-30 18:26:24 +01:00
|
|
|
[ Aeson.object
|
2019-07-10 05:57:35 +02:00
|
|
|
[ "name" .= ("Luke Skywalker" :: Text)
|
|
|
|
, "secretBackstory" .= Aeson.Null
|
|
|
|
]
|
2019-12-30 18:26:24 +01:00
|
|
|
, Aeson.object
|
2019-07-10 05:57:35 +02:00
|
|
|
[ "name" .= ("Han Solo" :: Text)
|
|
|
|
, "secretBackstory" .= Aeson.Null
|
|
|
|
]
|
2019-12-30 18:26:24 +01:00
|
|
|
, Aeson.object
|
2019-07-10 05:57:35 +02:00
|
|
|
[ "name" .= ("Leia Organa" :: Text)
|
|
|
|
, "secretBackstory" .= Aeson.Null
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2019-07-02 20:07:26 +02:00
|
|
|
]
|
2019-07-10 05:57:35 +02:00
|
|
|
, "errors" .=
|
2019-12-30 18:26:24 +01:00
|
|
|
[ Aeson.object
|
|
|
|
[ "message" .= ("secretBackstory is secret." :: Text)
|
|
|
|
]
|
|
|
|
, Aeson.object
|
|
|
|
[ "message" .= ("secretBackstory is secret." :: Text)
|
|
|
|
]
|
|
|
|
, Aeson.object
|
|
|
|
[ "message" .= ("secretBackstory is secret." :: Text)
|
|
|
|
]
|
2019-07-10 05:57:35 +02:00
|
|
|
]
|
|
|
|
]
|
|
|
|
it "error on secretBackstory with alias" $ testQuery
|
2016-07-08 23:50:31 +02:00
|
|
|
[r| query HeroNameQuery {
|
|
|
|
mainHero: hero {
|
|
|
|
name
|
|
|
|
story: secretBackstory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2019-12-30 18:26:24 +01:00
|
|
|
$ Aeson.object
|
|
|
|
[ "data" .= Aeson.object
|
|
|
|
[ "mainHero" .= Aeson.object
|
2019-07-02 20:07:26 +02:00
|
|
|
[ "name" .= ("R2-D2" :: Text)
|
|
|
|
, "story" .= Aeson.Null
|
|
|
|
]
|
|
|
|
]
|
|
|
|
, "errors" .=
|
2019-12-30 18:26:24 +01:00
|
|
|
[ Aeson.object
|
|
|
|
[ "message" .= ("secretBackstory is secret." :: Text)
|
|
|
|
]
|
2019-07-02 20:07:26 +02:00
|
|
|
]
|
|
|
|
]
|
2019-07-10 05:57:35 +02:00
|
|
|
|
2016-07-08 22:10:58 +02:00
|
|
|
where
|
|
|
|
lukeName = "name" .= ("Luke Skywalker" :: Text)
|
|
|
|
leiaName = "name" .= ("Leia Organa" :: Text)
|
|
|
|
hanName = "name" .= ("Han Solo" :: Text)
|
2016-07-08 22:36:45 +02:00
|
|
|
r2d2Name = "name" .= ("R2-D2" :: Text)
|
|
|
|
c3poName = "name" .= ("C-3PO" :: Text)
|
2016-07-08 22:10:58 +02:00
|
|
|
tatooine = "homePlanet" .= ("Tatooine" :: Text)
|
|
|
|
alderaan = "homePlanet" .= ("Alderaan" :: Text)
|
2016-02-17 18:13:10 +01:00
|
|
|
|
2019-07-10 05:57:35 +02:00
|
|
|
testQuery :: Text -> Aeson.Value -> Expectation
|
2020-07-17 07:05:03 +02:00
|
|
|
testQuery q expected =
|
|
|
|
let Right actual = graphql schema q
|
|
|
|
in actual `shouldBe` expected
|
2016-02-17 18:13:10 +01:00
|
|
|
|
2020-05-21 10:20:59 +02:00
|
|
|
testQueryParams :: Aeson.Object -> Text -> Aeson.Value -> Expectation
|
2020-02-01 20:46:35 +01:00
|
|
|
testQueryParams f q expected =
|
2020-07-17 07:05:03 +02:00
|
|
|
let Right actual = graphqlSubs schema Nothing f q
|
|
|
|
in actual `shouldBe` expected
|