2016-02-12 13:27:46 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2016-02-15 11:19:05 +01:00
|
|
|
module Test.StarWars.QueryTests (test) where
|
2016-02-12 13:27:46 +01:00
|
|
|
|
2016-03-12 00:59:51 +01:00
|
|
|
import qualified Data.Aeson as Aeson (Value(Null), toJSON)
|
2016-02-17 12:35:54 +01:00
|
|
|
import Data.Aeson (object, (.=))
|
2016-02-12 13:27:46 +01:00
|
|
|
import Data.Text (Text)
|
|
|
|
import Text.RawString.QQ (r)
|
|
|
|
|
|
|
|
import Test.Tasty (TestTree, testGroup)
|
2016-02-15 11:19:05 +01:00
|
|
|
import Test.Tasty.HUnit (Assertion, testCase, (@?=))
|
2016-02-12 13:27:46 +01:00
|
|
|
|
|
|
|
import Data.GraphQL
|
2016-02-15 14:25:15 +01:00
|
|
|
import Data.GraphQL.Schema (Subs)
|
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
|
|
|
|
|
|
|
|
test :: TestTree
|
|
|
|
test = testGroup "Star Wars Query Tests"
|
|
|
|
[ testGroup "Basic Queries"
|
2016-02-15 11:19:05 +01:00
|
|
|
[ testCase "R2-D2 hero" . testQuery
|
|
|
|
[r| query HeroNameQuery {
|
|
|
|
hero {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object ["hero" .= object ["id" .= ("2001" :: Text)]]]
|
2016-02-15 11:19:05 +01:00
|
|
|
, testCase "R2-D2 ID and friends" . testQuery
|
|
|
|
[r| query HeroNameAndFriendsQuery {
|
|
|
|
hero {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
friends {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object [
|
2016-02-12 13:27:46 +01:00
|
|
|
"hero" .= object [
|
2016-02-15 11:19:05 +01:00
|
|
|
"id" .= ("2001" :: Text)
|
2016-07-08 22:36:45 +02:00
|
|
|
, r2d2Name
|
2016-02-15 11:19:05 +01:00
|
|
|
, "friends" .= [
|
2016-07-08 22:10:58 +02:00
|
|
|
object [lukeName]
|
|
|
|
, object [hanName]
|
|
|
|
, object [leiaName]
|
2016-02-15 11:19:05 +01:00
|
|
|
]
|
|
|
|
]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2016-02-12 13:27:46 +01:00
|
|
|
]
|
|
|
|
, testGroup "Nested Queries"
|
2016-02-15 11:19:05 +01:00
|
|
|
[ testCase "R2-D2 friends" . testQuery
|
|
|
|
[r| query NestedQuery {
|
|
|
|
hero {
|
|
|
|
name
|
|
|
|
friends {
|
|
|
|
name
|
|
|
|
appearsIn
|
|
|
|
friends {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object [
|
2016-02-15 11:19:05 +01:00
|
|
|
"hero" .= object [
|
|
|
|
"name" .= ("R2-D2" :: Text)
|
|
|
|
, "friends" .= [
|
|
|
|
object [
|
|
|
|
"name" .= ("Luke Skywalker" :: Text)
|
|
|
|
, "appearsIn" .= ["NEWHOPE","EMPIRE","JEDI" :: Text]
|
|
|
|
, "friends" .= [
|
2016-07-08 22:10:58 +02:00
|
|
|
object [hanName]
|
|
|
|
, object [leiaName]
|
2016-07-08 22:36:45 +02:00
|
|
|
, object [c3poName]
|
|
|
|
, object [r2d2Name]
|
2016-02-15 11:19:05 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
, object [
|
2016-07-08 22:10:58 +02:00
|
|
|
hanName
|
2016-02-15 11:19:05 +01:00
|
|
|
, "appearsIn" .= [ "NEWHOPE","EMPIRE","JEDI" :: Text]
|
|
|
|
, "friends" .= [
|
2016-07-08 22:10:58 +02:00
|
|
|
object [lukeName]
|
|
|
|
, object [leiaName]
|
2016-07-08 22:36:45 +02:00
|
|
|
, object [r2d2Name]
|
2016-02-15 11:19:05 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
, object [
|
2016-07-08 22:10:58 +02:00
|
|
|
leiaName
|
2016-02-15 11:19:05 +01:00
|
|
|
, "appearsIn" .= [ "NEWHOPE","EMPIRE","JEDI" :: Text]
|
|
|
|
, "friends" .= [
|
2016-07-08 22:10:58 +02:00
|
|
|
object [lukeName]
|
|
|
|
, object [hanName]
|
2016-07-08 22:36:45 +02:00
|
|
|
, object [c3poName]
|
|
|
|
, object [r2d2Name]
|
2016-02-15 11:19:05 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2016-02-15 11:19:05 +01:00
|
|
|
, testCase "Luke ID" . testQuery
|
|
|
|
[r| query FetchLukeQuery {
|
|
|
|
human(id: "1000") {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object [
|
2016-07-08 22:10:58 +02:00
|
|
|
"human" .= object [lukeName]
|
2016-02-12 13:27:46 +01:00
|
|
|
]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2016-02-15 14:25:15 +01:00
|
|
|
, testCase "Luke ID with variable" . testQueryParams
|
|
|
|
(\v -> if v == "someId"
|
|
|
|
then Just "1000"
|
|
|
|
else Nothing)
|
|
|
|
[r| query FetchSomeIDQuery($someId: String!) {
|
|
|
|
human(id: $someId) {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object [
|
2016-07-08 22:10:58 +02:00
|
|
|
"human" .= object [lukeName]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2016-02-17 12:35:54 +01:00
|
|
|
, testCase "Han ID with variable" . testQueryParams
|
|
|
|
(\v -> if v == "someId"
|
|
|
|
then Just "1002"
|
|
|
|
else Nothing)
|
|
|
|
[r| query FetchSomeIDQuery($someId: String!) {
|
|
|
|
human(id: $someId) {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object [
|
2016-07-08 22:10:58 +02:00
|
|
|
"human" .= object [hanName]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2017-02-19 19:29:58 +01:00
|
|
|
-- TODO: Enable after Error handling restoration
|
|
|
|
-- , testCase "Invalid ID" . testQueryParams
|
|
|
|
-- (\v -> if v == "id"
|
|
|
|
-- then Just "Not a valid ID"
|
|
|
|
-- else Nothing)
|
|
|
|
-- [r| query humanQuery($id: String!) {
|
|
|
|
-- human(id: $id) {
|
|
|
|
-- name
|
|
|
|
-- }
|
|
|
|
-- }
|
|
|
|
-- |] $ object ["data" .= object ["human" .= object ["name" .= Aeson.Null]],
|
|
|
|
-- "errors" .= Aeson.toJSON [object ["message" .= ("field name not resolved." :: Text)]]]
|
2016-02-17 18:13:10 +01:00
|
|
|
-- TODO: This test is directly ported from `graphql-js`, however do we want
|
|
|
|
-- to mimic the same behavior? Is this part of the spec? Once proper
|
|
|
|
-- exceptions are implemented this test might no longer be meaningful.
|
|
|
|
-- If the same behavior needs to be replicated, should it be implemented
|
|
|
|
-- when defining the `Schema` or when executing?
|
2016-03-12 00:59:51 +01:00
|
|
|
-- $ object [ "data" .= object ["human" .= Aeson.Null] ]
|
2016-02-17 18:13:10 +01:00
|
|
|
, testCase "Luke aliased" . testQuery
|
2016-02-17 12:59:35 +01:00
|
|
|
[r| query FetchLukeAliased {
|
|
|
|
luke: human(id: "1000") {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object [
|
2016-07-08 22:10:58 +02:00
|
|
|
"luke" .= object [lukeName]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2016-02-17 18:13:10 +01:00
|
|
|
, testCase "R2-D2 ID and friends aliased" . testQuery
|
|
|
|
[r| query HeroNameAndFriendsQuery {
|
|
|
|
hero {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
friends {
|
|
|
|
friendName: name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object [
|
2016-02-17 18:13:10 +01:00
|
|
|
"hero" .= object [
|
|
|
|
"id" .= ("2001" :: Text)
|
2016-07-08 22:36:45 +02:00
|
|
|
, r2d2Name
|
2016-02-17 18:13:10 +01:00
|
|
|
, "friends" .= [
|
|
|
|
object ["friendName" .= ("Luke Skywalker" :: Text)]
|
|
|
|
, object ["friendName" .= ("Han Solo" :: Text)]
|
|
|
|
, object ["friendName" .= ("Leia Organa" :: Text)]
|
|
|
|
]
|
|
|
|
]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2016-02-17 12:59:35 +01:00
|
|
|
, testCase "Luke and Leia aliased" . testQuery
|
|
|
|
[r| query FetchLukeAndLeiaAliased {
|
|
|
|
luke: human(id: "1000") {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
leia: human(id: "1003") {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2016-03-12 00:59:51 +01:00
|
|
|
$ object [ "data" .= object [
|
2016-07-08 22:10:58 +02:00
|
|
|
"luke" .= object [lukeName]
|
|
|
|
, "leia" .= object [leiaName]
|
|
|
|
]]
|
|
|
|
, testGroup "Fragments for complex queries"
|
2016-12-18 15:43:45 +01:00
|
|
|
[ testCase "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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
$ object [ "data" .= object [
|
|
|
|
"luke" .= object [lukeName, tatooine]
|
|
|
|
, "leia" .= object [leiaName, alderaan]
|
2016-03-12 00:59:51 +01:00
|
|
|
]]
|
2016-07-08 22:10:58 +02:00
|
|
|
, testCase "Fragment for duplicate content" . testQuery
|
|
|
|
[r| query UseFragment {
|
|
|
|
luke: human(id: "1000") {
|
|
|
|
...HumanFragment
|
|
|
|
}
|
|
|
|
leia: human(id: "1003") {
|
|
|
|
...HumanFragment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment HumanFragment on Human {
|
|
|
|
name
|
|
|
|
homePlanet
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
$ object [ "data" .= object [
|
|
|
|
"luke" .= object [lukeName, tatooine]
|
|
|
|
, "leia" .= object [leiaName, alderaan]
|
|
|
|
]]
|
|
|
|
]
|
2016-07-08 22:36:45 +02:00
|
|
|
, testGroup "__typename"
|
|
|
|
[ testCase "R2D2 is a Droid" . testQuery
|
|
|
|
[r| query CheckTypeOfR2 {
|
|
|
|
hero {
|
|
|
|
__typename
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
$ object ["data" .= object [
|
|
|
|
"hero" .= ["__typename" .= ("Droid" :: Text), r2d2Name]
|
|
|
|
]]
|
|
|
|
, testCase "Luke is a human" . testQuery
|
|
|
|
[r| query CheckTypeOfLuke {
|
|
|
|
hero(episode: EMPIRE) {
|
|
|
|
__typename
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
$ object ["data" .= object [
|
|
|
|
"hero" .= ["__typename" .= ("Human" :: Text), lukeName]
|
|
|
|
]]
|
|
|
|
]
|
2016-07-08 23:50:31 +02:00
|
|
|
, testGroup "Errors in resolvers"
|
|
|
|
[ testCase "error on secretBackstory" . testQuery
|
|
|
|
[r| query HeroNameQuery {
|
|
|
|
hero {
|
|
|
|
name
|
|
|
|
secretBackstory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
$ object ["data" .= object [
|
|
|
|
"hero" .= [r2d2Name, secretBackstory]
|
|
|
|
]
|
|
|
|
, "errors" .= object [
|
|
|
|
"message" .= Aeson.toJSON [secretText]
|
|
|
|
, "path" .= Aeson.toJSON [[ "hero" :: Text, "secretBackstory" :: Text ]]
|
|
|
|
]]
|
|
|
|
, testCase "Error in a list" . testQuery
|
|
|
|
[r| query HeroNameQuery {
|
|
|
|
hero {
|
|
|
|
name
|
|
|
|
friends {
|
|
|
|
name
|
|
|
|
secretBackstory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
$ object ["data" .= object [
|
|
|
|
"hero" .= [r2d2Name, "friends" .= [
|
|
|
|
object [lukeName, secretBackstory]
|
|
|
|
, object [hanName, secretBackstory]
|
|
|
|
, object [leiaName, secretBackstory]
|
|
|
|
]]
|
|
|
|
]
|
|
|
|
, "errors" .= object [
|
|
|
|
"message" .= Aeson.toJSON [secretText, secretText, secretText]
|
|
|
|
, "path" .= Aeson.toJSON [secretPath 0, secretPath 1, secretPath 2]
|
|
|
|
]]
|
|
|
|
, testCase "error on secretBackstory with alias" . testQuery
|
|
|
|
[r| query HeroNameQuery {
|
|
|
|
mainHero: hero {
|
|
|
|
name
|
|
|
|
story: secretBackstory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
$ object ["data" .= object [
|
|
|
|
"mainHero" .= [r2d2Name, "story" .= ()]
|
|
|
|
]
|
|
|
|
, "errors" .= object [
|
|
|
|
"message" .= Aeson.toJSON [secretText]
|
|
|
|
, "path" .= Aeson.toJSON [[ "mainHero" :: Text, "story" :: Text ]]
|
|
|
|
]]
|
|
|
|
]
|
2016-02-12 13:27:46 +01: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-07-08 23:50:31 +02:00
|
|
|
secretBackstory = "secretBackstory" .= ()
|
|
|
|
secretText = "secretBackstory is secret" :: Text
|
|
|
|
secretPath n = ("hero", "friends", n, "secretBackstory") :: (Text, Text, Int, Text)
|
2016-02-17 18:13:10 +01:00
|
|
|
|
|
|
|
testQuery :: Text -> Aeson.Value -> Assertion
|
|
|
|
testQuery q expected = graphql schema q @?= Just expected
|
|
|
|
|
|
|
|
-- testFail :: Text -> Assertion
|
|
|
|
-- testFail q = graphql schema q @?= Nothing
|
|
|
|
|
|
|
|
testQueryParams :: Subs -> Text -> Aeson.Value -> Assertion
|
|
|
|
testQueryParams f q expected = graphqlSubs schema f q @?= Just expected
|
|
|
|
|
2016-03-12 00:59:51 +01:00
|
|
|
-- testFailParams :: Subs -> Text -> Assertion
|
|
|
|
-- testFailParams f q = graphqlSubs schema f q @?= Nothing
|