2015-10-17 17:49:56 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
module Test.StarWars where
|
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
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
|
|
|
|
|
|
|
import Data.Attoparsec.Text (parseOnly)
|
|
|
|
import qualified Data.Aeson as Aeson
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
-- * Test
|
|
|
|
|
|
|
|
test :: TestTree
|
|
|
|
test = testCase "R2-D2" $ execute schema heroQuery @=? Identity expected
|
|
|
|
where
|
|
|
|
heroQuery :: Document
|
|
|
|
heroQuery = either (error "Parsing error") id $ parseOnly Parser.document
|
|
|
|
"{ query HeroNameQuery { hero { name } } }"
|
|
|
|
|
|
|
|
expected :: Response
|
|
|
|
expected = Aeson.Object
|
|
|
|
[ ( "hero" , Aeson.Object [ ("name", "R2-D2") ] ) ]
|
|
|
|
|
|
|
|
-- * Schema
|
|
|
|
|
2015-10-17 17:49:56 +02:00
|
|
|
type ID = Text
|
|
|
|
|
|
|
|
schema :: Applicative f => Schema f
|
|
|
|
schema = Schema query Nothing
|
|
|
|
|
|
|
|
query :: Applicative f => QueryRoot f
|
|
|
|
query = [ ("hero", hero)
|
|
|
|
, ("human", human)
|
|
|
|
, ("droid", droid)
|
|
|
|
]
|
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
hero :: Applicative f => Resolver f
|
|
|
|
hero (InputScalar (ScalarInt ep)) = OutputMap $ getHeroF ep
|
|
|
|
hero _ = InputError
|
2015-10-17 17:49:56 +02:00
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
human :: Applicative f => Resolver f
|
|
|
|
human (InputScalar (ScalarString id_)) = OutputScalar $ ScalarString <$> getHumanF id_
|
|
|
|
human _ = InputError
|
2015-10-17 17:49:56 +02:00
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
droid :: Applicative f => Resolver f
|
|
|
|
droid (InputScalar (ScalarString id_)) = OutputScalar $ ScalarString <$> getDroidF id_
|
|
|
|
droid _ = InputError
|
2015-10-17 17:49:56 +02:00
|
|
|
|
|
|
|
-- * Data
|
|
|
|
|
|
|
|
-- ** Characters
|
|
|
|
|
|
|
|
data Character = Character
|
|
|
|
{ id_ :: ID
|
|
|
|
, name :: Text
|
|
|
|
, friends :: [ID]
|
|
|
|
, appearsIn :: [Int]
|
|
|
|
, homePlanet :: Text
|
|
|
|
}
|
|
|
|
|
|
|
|
luke :: Character
|
|
|
|
luke = Character
|
|
|
|
{ id_ = "1000"
|
|
|
|
, name = "Luke Skywalker"
|
|
|
|
, friends = ["1002","1003","2000","2001"]
|
|
|
|
, appearsIn = [4,5,6]
|
|
|
|
, homePlanet = "Tatoonie"
|
|
|
|
}
|
|
|
|
|
|
|
|
artoo :: Character
|
|
|
|
artoo = Character
|
|
|
|
{ id_ = "2001"
|
|
|
|
, name = "R2-D2"
|
|
|
|
, friends = ["1000","1002","1003"]
|
|
|
|
, appearsIn = [4,5,6]
|
|
|
|
, homePlanet = "Astrometch"
|
|
|
|
}
|
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
type CharacterMap f = Map f
|
2015-10-17 17:49:56 +02:00
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
character :: Applicative f => Character -> CharacterMap f
|
2015-10-17 17:49:56 +02:00
|
|
|
character (Character{..}) =
|
2015-10-19 12:19:39 +02:00
|
|
|
[ ("id_", const . OutputScalar . pure $ ScalarID id_)
|
|
|
|
, ("name", const . OutputScalar . pure $ ScalarString name)
|
|
|
|
, ("friends", const . OutputList $ OutputScalar . pure . ScalarID <$> friends)
|
|
|
|
, ("appearsIn", const . OutputList $ OutputScalar . pure . ScalarInt <$> appearsIn)
|
|
|
|
, ("homePlanet", const . OutputScalar . pure $ ScalarString homePlanet)
|
2015-10-17 17:49:56 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
-- ** Hero
|
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
getHero :: Int -> Character
|
|
|
|
getHero 5 = luke
|
|
|
|
getHero _ = artoo
|
2015-10-17 17:49:56 +02:00
|
|
|
|
2015-10-19 12:19:39 +02:00
|
|
|
getHeroF :: Applicative f => Int -> CharacterMap f
|
|
|
|
getHeroF = character . getHero
|
2015-10-17 17:49:56 +02:00
|
|
|
|
|
|
|
-- ** Human
|
|
|
|
|
|
|
|
getHuman :: ID -> Text
|
|
|
|
getHuman "1000" = "luke"
|
|
|
|
getHuman "1001" = "vader"
|
|
|
|
getHuman "1002" = "han"
|
|
|
|
getHuman "1003" = "leia"
|
|
|
|
getHuman "1004" = "tarkin"
|
|
|
|
getHuman _ = ""
|
|
|
|
|
|
|
|
getHumanF :: Applicative f => ID -> f Text
|
|
|
|
getHumanF = pure . getHuman
|
|
|
|
|
|
|
|
getHumanIO :: ID -> IO Text
|
|
|
|
getHumanIO = getHumanF
|
|
|
|
|
|
|
|
-- ** Droid
|
|
|
|
|
|
|
|
getDroid :: ID -> Text
|
|
|
|
getDroid "2000" = "threepio"
|
|
|
|
getDroid "2001" = "artoo"
|
|
|
|
getDroid _ = ""
|
|
|
|
|
|
|
|
getDroidF :: Applicative f => ID -> f Text
|
|
|
|
getDroidF = pure . getDroid
|
|
|
|
|
|
|
|
getDroidIO :: ID -> IO Text
|
|
|
|
getDroidIO = getDroidF
|