2015-10-17 17:49:56 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
module Test.StarWars where
|
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
import Control.Applicative ((<|>), liftA2)
|
|
|
|
import Data.Maybe (catMaybes)
|
|
|
|
-- import Data.Functor.Identity (Identity(..))
|
2015-10-17 17:49:56 +02:00
|
|
|
import Data.Text (Text)
|
2015-10-19 12:19:39 +02:00
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
-- import Data.Aeson (ToJSON(toJSON), genericToJSON, defaultOptions)
|
2015-10-19 12:19:39 +02:00
|
|
|
import qualified Data.Aeson as Aeson
|
2016-01-26 12:43:18 +01:00
|
|
|
import Data.Attoparsec.Text (parseOnly)
|
2015-10-19 12:19:39 +02:00
|
|
|
|
|
|
|
import Test.Tasty (TestTree)
|
|
|
|
import Test.Tasty.HUnit
|
|
|
|
|
|
|
|
import Data.GraphQL.AST
|
|
|
|
import Data.GraphQL.Execute
|
|
|
|
import qualified Data.GraphQL.Parser as Parser
|
2015-10-17 17:49:56 +02:00
|
|
|
import Data.GraphQL.Schema
|
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
-- * Test
|
2016-01-26 12:43:18 +01:00
|
|
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsQueryTests.js
|
2015-10-19 12:19:39 +02:00
|
|
|
|
|
|
|
test :: TestTree
|
2016-01-26 12:43:18 +01:00
|
|
|
test = testCase "R2-D2" $ execute schema heroQuery @?= expected
|
2015-10-19 12:19:39 +02:00
|
|
|
where
|
|
|
|
heroQuery :: Document
|
|
|
|
heroQuery = either (error "Parsing error") id $ parseOnly Parser.document
|
2016-01-26 12:43:18 +01:00
|
|
|
"query HeroNameQuery{hero{name}}"
|
2015-10-19 12:19:39 +02:00
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
expected :: Maybe Response
|
|
|
|
expected = Just $ Aeson.Object [("hero", Aeson.Object [("name", "R2-D2")])]
|
2015-10-19 12:19:39 +02:00
|
|
|
|
|
|
|
-- * Schema
|
2016-01-26 12:43:18 +01:00
|
|
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsSchema.js
|
2015-10-19 12:19:39 +02:00
|
|
|
|
2015-10-17 17:49:56 +02:00
|
|
|
type ID = Text
|
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
schema :: Schema
|
|
|
|
schema = Schema query
|
|
|
|
|
|
|
|
query :: QueryRoot
|
|
|
|
query (InputField "hero") = OutputResolver hero
|
|
|
|
query (InputField "human") = OutputResolver human
|
|
|
|
query (InputField "droid") = OutputResolver droid
|
|
|
|
query _ = OutputError
|
|
|
|
|
|
|
|
hero :: Resolver
|
|
|
|
hero (InputList (InputScalar (ScalarInt ep) : inputFields)) =
|
|
|
|
maybe OutputError (\char -> OutputList $ (`characterOutput` char) <$> fields inputFields) $ getHero ep
|
|
|
|
hero (InputField fld) = characterOutput fld artoo
|
|
|
|
hero _ = OutputError
|
|
|
|
|
|
|
|
human :: Resolver
|
|
|
|
human (InputList (InputScalar (ScalarID i) : inputFields)) =
|
|
|
|
maybe OutputError (\char -> OutputList $ (`characterOutput` char) <$> fields inputFields) $ getHuman i
|
|
|
|
human _ = OutputError
|
|
|
|
|
|
|
|
droid :: Resolver
|
|
|
|
droid (InputList (InputScalar (ScalarID i) : inputFields)) =
|
|
|
|
maybe OutputError (\char -> OutputList $ (`characterOutput` char) <$> fields inputFields) $ getDroid i
|
|
|
|
droid _ = OutputError
|
|
|
|
|
|
|
|
characterOutput :: Text -> Character -> Output
|
|
|
|
characterOutput "id" char = OutputScalar . ScalarString $ id_ char
|
|
|
|
characterOutput "name" char = OutputScalar . ScalarString $ name char
|
|
|
|
characterOutput "friends" char = OutputList $ OutputResolver . (\c (InputField f) -> characterOutput f c) <$> getFriends char
|
|
|
|
characterOutput _ _ = OutputError
|
2015-10-17 17:49:56 +02:00
|
|
|
|
|
|
|
-- * Data
|
2016-01-26 12:43:18 +01:00
|
|
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsData.js
|
2015-10-17 17:49:56 +02:00
|
|
|
|
|
|
|
-- ** Characters
|
|
|
|
|
|
|
|
data Character = Character
|
2016-01-26 12:43:18 +01:00
|
|
|
{ id_ :: ID
|
|
|
|
, name :: Text
|
|
|
|
, friends :: [ID]
|
|
|
|
, appearsIn :: [Int]
|
2015-10-17 17:49:56 +02:00
|
|
|
, homePlanet :: Text
|
2016-01-26 12:43:18 +01:00
|
|
|
} deriving (Show)
|
2015-10-17 17:49:56 +02:00
|
|
|
|
|
|
|
luke :: Character
|
|
|
|
luke = Character
|
2016-01-26 12:43:18 +01:00
|
|
|
{ id_ = "1000"
|
|
|
|
, name = "Luke Skywalker"
|
|
|
|
, friends = ["1002","1003","2000","2001"]
|
|
|
|
, appearsIn = [4,5,6]
|
2015-10-17 17:49:56 +02:00
|
|
|
, homePlanet = "Tatoonie"
|
|
|
|
}
|
|
|
|
|
|
|
|
artoo :: Character
|
|
|
|
artoo = Character
|
2016-01-26 12:43:18 +01:00
|
|
|
{ id_ = "2001"
|
|
|
|
, name = "R2-D2"
|
|
|
|
, friends = ["1000","1002","1003"]
|
|
|
|
, appearsIn = [4,5,6]
|
2015-10-17 17:49:56 +02:00
|
|
|
, homePlanet = "Astrometch"
|
|
|
|
}
|
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
-- ** Helper functions
|
2015-10-17 17:49:56 +02:00
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
getHero :: Int -> Maybe Character
|
|
|
|
getHero 5 = Just luke
|
|
|
|
getHero _ = Just artoo
|
2015-10-17 17:49:56 +02:00
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
getHuman :: ID -> Maybe Character
|
|
|
|
getHuman "1000" = Just luke
|
|
|
|
-- getHuman "1001" = "vader"
|
|
|
|
-- getHuman "1002" = "han"
|
|
|
|
-- getHuman "1003" = "leia"
|
|
|
|
-- getHuman "1004" = "tarkin"
|
|
|
|
getHuman _ = Nothing
|
2015-10-17 17:49:56 +02:00
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
getDroid :: ID -> Maybe Character
|
|
|
|
-- getDroid "2000" = "threepio"
|
|
|
|
getDroid "2001" = Just artoo
|
|
|
|
getDroid _ = Nothing
|
2015-10-17 17:49:56 +02:00
|
|
|
|
|
|
|
|
2016-01-26 12:43:18 +01:00
|
|
|
getFriends :: Character -> [Character]
|
|
|
|
getFriends char = catMaybes $ liftA2 (<|>) getDroid getHuman <$> friends char
|