graphql/tests/Test/StarWars/QuerySpec.hs

365 lines
11 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
2019-07-10 05:57:35 +02:00
module Test.StarWars.QuerySpec
( spec
) where
import qualified Data.Aeson as Aeson
import Data.Aeson ((.=))
import Data.Functor.Identity (Identity(..))
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text)
import Language.GraphQL
import Text.RawString.QQ (r)
import Test.Hspec.Expectations (Expectation, shouldBe)
import Test.Hspec (Spec, describe, it)
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
}
|]
$ 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 {
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
}
|]
$ Aeson.object [ "data" .= Aeson.object [
"hero" .= Aeson.object
2019-07-10 05:57:35 +02:00
[ "id" .= ("2001" :: Text)
, r2d2Name
, "friends" .=
[ 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
]
]]
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
}
}
}
|]
$ Aeson.object [ "data" .= Aeson.object [
"hero" .= Aeson.object [
2016-02-15 11:19:05 +01:00
"name" .= ("R2-D2" :: Text)
, "friends" .= [
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" .= [
Aeson.object [hanName]
, Aeson.object [leiaName]
, Aeson.object [c3poName]
, Aeson.object [r2d2Name]
2016-02-15 11:19:05 +01:00
]
]
, Aeson.object [
hanName
2020-06-13 07:20:19 +02:00
, "appearsIn" .= ["NEW_HOPE", "EMPIRE", "JEDI" :: Text]
, "friends" .=
[ Aeson.object [lukeName]
, Aeson.object [leiaName]
, Aeson.object [r2d2Name]
2016-02-15 11:19:05 +01:00
]
]
, Aeson.object [
leiaName
2020-06-13 07:20:19 +02:00
, "appearsIn" .= ["NEW_HOPE", "EMPIRE", "JEDI" :: Text]
, "friends" .=
[ Aeson.object [lukeName]
, Aeson.object [hanName]
, Aeson.object [c3poName]
, Aeson.object [r2d2Name]
2016-02-15 11:19:05 +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
}
}
|]
$ Aeson.object [ "data" .= Aeson.object
[ "human" .= Aeson.object [lukeName]
]]
2019-07-10 05:57:35 +02:00
it "Luke ID with variable" $ testQueryParams
(HashMap.singleton "someId" "1000")
2019-07-10 05:57:35 +02:00
[r| query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
2019-07-10 05:57:35 +02:00
}
|]
$ Aeson.object [ "data" .= Aeson.object [
"human" .= Aeson.object [lukeName]
2019-07-10 05:57:35 +02:00
]]
it "Han ID with variable" $ testQueryParams
(HashMap.singleton "someId" "1002")
2019-07-10 05:57:35 +02:00
[r| query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
2019-07-10 05:57:35 +02:00
}
|]
$ Aeson.object [ "data" .= Aeson.object [
"human" .= Aeson.object [hanName]
2019-07-10 05:57:35 +02:00
]]
it "Invalid ID" $ testQueryParams
(HashMap.singleton "id" "Not a valid ID")
2019-07-10 05:57:35 +02:00
[r| query humanQuery($id: String!) {
human(id: $id) {
name
}
2019-07-10 05:57:35 +02: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
}
}
|]
$ 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
}
}
2019-07-10 05:57:35 +02:00
}
|]
$ Aeson.object [ "data" .= Aeson.object [
"hero" .= Aeson.object [
2019-07-10 05:57:35 +02:00
"id" .= ("2001" :: Text)
, r2d2Name
, "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
}
2019-07-10 05:57:35 +02:00
leia: human(id: "1003") {
name
}
}
|]
$ 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
[r| query DuplicateFields {
luke: human(id: "1000") {
name
homePlanet
}
leia: human(id: "1003") {
name
homePlanet
}
}
|]
$ Aeson.object [ "data" .= Aeson.object [
"luke" .= Aeson.object [lukeName, tatooine]
, "leia" .= Aeson.object [leiaName, alderaan]
]]
2019-07-10 05:57:35 +02:00
it "Fragment for duplicate content" $ testQuery
[r| query UseFragment {
luke: human(id: "1000") {
...HumanFragment
}
leia: human(id: "1003") {
...HumanFragment
}
}
fragment HumanFragment on Human {
name
homePlanet
}
|]
$ Aeson.object [ "data" .= Aeson.object [
"luke" .= Aeson.object [lukeName, tatooine]
, "leia" .= Aeson.object [leiaName, alderaan]
]]
2019-07-10 05:57:35 +02:00
describe "__typename" $ do
it "R2D2 is a Droid" $ testQuery
[r| query CheckTypeOfR2 {
hero {
__typename
name
}
}
|]
$ 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
[r| query CheckTypeOfLuke {
hero(episode: EMPIRE) {
__typename
name
}
}
|]
$ 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
[r|
query HeroNameQuery {
hero {
name
secretBackstory
}
}
|]
$ Aeson.object
[ "data" .= Aeson.object
[ "hero" .= Aeson.object
[ "name" .= ("R2-D2" :: Text)
, "secretBackstory" .= Aeson.Null
]
]
, "errors" .=
[ Aeson.object
["message" .= ("secretBackstory is secret." :: Text)]
]
]
2019-07-10 05:57:35 +02:00
it "Error in a list" $ testQuery
[r| query HeroNameQuery {
hero {
name
friends {
name
secretBackstory
}
}
}
|]
$ Aeson.object ["data" .= Aeson.object
[ "hero" .= Aeson.object
2019-07-10 05:57:35 +02:00
[ "name" .= ("R2-D2" :: Text)
, "friends" .=
[ Aeson.object
2019-07-10 05:57:35 +02:00
[ "name" .= ("Luke Skywalker" :: Text)
, "secretBackstory" .= Aeson.Null
]
, Aeson.object
2019-07-10 05:57:35 +02:00
[ "name" .= ("Han Solo" :: Text)
, "secretBackstory" .= Aeson.Null
]
, Aeson.object
2019-07-10 05:57:35 +02:00
[ "name" .= ("Leia Organa" :: Text)
, "secretBackstory" .= Aeson.Null
]
]
]
]
2019-07-10 05:57:35 +02:00
, "errors" .=
[ 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
[r| query HeroNameQuery {
mainHero: hero {
name
story: secretBackstory
}
}
|]
$ Aeson.object
[ "data" .= Aeson.object
[ "mainHero" .= Aeson.object
[ "name" .= ("R2-D2" :: Text)
, "story" .= Aeson.Null
]
]
, "errors" .=
[ Aeson.object
[ "message" .= ("secretBackstory is secret." :: Text)
]
]
]
2019-07-10 05:57:35 +02:00
where
lukeName = "name" .= ("Luke Skywalker" :: Text)
leiaName = "name" .= ("Leia Organa" :: Text)
hanName = "name" .= ("Han Solo" :: Text)
r2d2Name = "name" .= ("R2-D2" :: Text)
c3poName = "name" .= ("C-3PO" :: Text)
tatooine = "homePlanet" .= ("Tatooine" :: Text)
alderaan = "homePlanet" .= ("Alderaan" :: Text)
2019-07-10 05:57:35 +02:00
testQuery :: Text -> Aeson.Value -> Expectation
testQuery q expected = runIdentity (graphql schema q) `shouldBe` expected
2020-05-21 10:20:59 +02:00
testQueryParams :: Aeson.Object -> Text -> Aeson.Value -> Expectation
testQueryParams f q expected =
runIdentity (graphqlSubs schema Nothing f q) `shouldBe` expected