2016-02-12 13:27:46 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Test.StarWars.Schema where
|
|
|
|
|
2016-02-17 18:13:10 +01:00
|
|
|
import Control.Applicative ((<|>), Alternative, empty)
|
2016-02-12 13:27:46 +01:00
|
|
|
|
|
|
|
import Data.GraphQL.Schema
|
|
|
|
|
|
|
|
import Test.StarWars.Data
|
|
|
|
|
|
|
|
-- * Schema
|
|
|
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsSchema.js
|
|
|
|
|
|
|
|
schema :: (Alternative m, Monad m) => Schema m
|
|
|
|
schema = Schema query
|
|
|
|
|
2016-02-17 18:13:10 +01:00
|
|
|
query :: (Alternative m, Monad m) => ResolverM m
|
|
|
|
query fld =
|
|
|
|
withField "hero" hero fld
|
|
|
|
<|> withField "human" human fld
|
|
|
|
<|> withField "droid" droid fld
|
2016-02-12 13:27:46 +01:00
|
|
|
|
2016-02-17 18:13:10 +01:00
|
|
|
hero :: Alternative f => [Argument] -> ResolverO f
|
2016-02-12 13:27:46 +01:00
|
|
|
hero [] = characterFields artoo
|
2016-02-17 18:13:10 +01:00
|
|
|
hero args =
|
|
|
|
case withArgument "episode" args of
|
|
|
|
Just (ScalarInt n) -> characterFields $ getHero n
|
|
|
|
_ -> const empty
|
|
|
|
|
|
|
|
human :: (Alternative m, Monad m) => [Argument] -> ResolverO m
|
|
|
|
human args flds =
|
|
|
|
case withArgument "id" args of
|
|
|
|
Just (ScalarString i) -> flip characterFields flds =<< getHuman i
|
|
|
|
_ -> empty
|
|
|
|
|
|
|
|
droid :: (Alternative m, Monad m) => [Argument] -> ResolverO m
|
|
|
|
droid args flds =
|
|
|
|
case withArgument "id" args of
|
|
|
|
Just (ScalarString i) -> flip characterFields flds =<< getDroid i
|
|
|
|
_ -> empty
|
|
|
|
|
|
|
|
characterField :: Alternative f => Character -> ResolverM f
|
|
|
|
characterField char fld =
|
|
|
|
withFieldFinal "id" (OutputScalar . ScalarString . id_ $ char) fld
|
|
|
|
<|> withFieldFinal "name" (OutputScalar . ScalarString . name $ char) fld
|
|
|
|
<|> withField "friends" friends' fld
|
|
|
|
<|> withField "appearsIn" appears' fld
|
|
|
|
where
|
|
|
|
friends' [] flds = outputTraverse (`characterFields` flds) $ getFriends char
|
|
|
|
friends' _ _ = empty
|
|
|
|
|
|
|
|
appears' [] [] = outputTraverse (fmap OutputEnum . getEpisode) $ appearsIn char
|
|
|
|
appears' _ _ = empty
|
|
|
|
|
|
|
|
characterFields :: Alternative f => Character -> ResolverO f
|
|
|
|
characterFields = withFields . characterField
|