2016-02-19 19:21:32 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2016-02-12 13:27:46 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Test.StarWars.Schema where
|
|
|
|
|
2019-07-02 20:07:26 +02:00
|
|
|
import Control.Monad (MonadPlus(..))
|
|
|
|
import Control.Monad.Trans.Except (throwE)
|
|
|
|
import Control.Monad.Trans.Class (lift)
|
2017-01-28 18:15:14 +01:00
|
|
|
import Data.List.NonEmpty (NonEmpty((:|)))
|
2019-06-28 11:12:28 +02:00
|
|
|
import Data.GraphQL.Schema ( Schema
|
|
|
|
, Resolver
|
|
|
|
, Argument(..)
|
|
|
|
, Value(..)
|
|
|
|
)
|
2016-02-19 19:21:32 +01:00
|
|
|
import qualified Data.GraphQL.Schema as Schema
|
2019-07-02 20:07:26 +02:00
|
|
|
import Language.GraphQL.Trans
|
2016-02-12 13:27:46 +01:00
|
|
|
import Test.StarWars.Data
|
|
|
|
|
|
|
|
-- * Schema
|
|
|
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsSchema.js
|
|
|
|
|
2019-06-28 11:12:28 +02:00
|
|
|
schema :: MonadPlus m => Schema m
|
2017-01-28 18:15:14 +01:00
|
|
|
schema = hero :| [human, droid]
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2019-06-28 11:12:28 +02:00
|
|
|
hero :: MonadPlus m => Resolver m
|
2016-02-19 19:21:32 +01:00
|
|
|
hero = Schema.objectA "hero" $ \case
|
|
|
|
[] -> character artoo
|
2017-03-02 02:04:13 +01:00
|
|
|
[Argument "episode" (ValueInt n)] -> character . getHero $ fromIntegral n
|
|
|
|
[Argument "episode" (ValueEnum "NEWHOPE")] -> character $ getHero 4
|
|
|
|
[Argument "episode" (ValueEnum "EMPIRE" )] -> character $ getHero 5
|
|
|
|
[Argument "episode" (ValueEnum "JEDI" )] -> character $ getHero 6
|
2019-07-02 20:07:26 +02:00
|
|
|
_ -> ActionT $ throwE "Invalid arguments."
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2019-06-28 11:12:28 +02:00
|
|
|
human :: MonadPlus m => Resolver m
|
2016-02-19 19:21:32 +01:00
|
|
|
human = Schema.objectA "human" $ \case
|
2019-07-02 20:07:26 +02:00
|
|
|
[Argument "id" (ValueString i)] -> character =<< lift (getHuman i)
|
|
|
|
_ -> ActionT $ throwE "Invalid arguments."
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2019-06-28 11:12:28 +02:00
|
|
|
droid :: MonadPlus m => Resolver m
|
2016-02-19 19:21:32 +01:00
|
|
|
droid = Schema.objectA "droid" $ \case
|
2019-07-02 20:07:26 +02:00
|
|
|
[Argument "id" (ValueString i)] -> character =<< lift (getDroid i)
|
|
|
|
_ -> ActionT $ throwE "Invalid arguments."
|
|
|
|
|
|
|
|
character :: MonadPlus m => Character -> ActionT m [Resolver m]
|
|
|
|
character char = return
|
|
|
|
[ Schema.scalar "id" $ return $ id_ char
|
|
|
|
, Schema.scalar "name" $ return $ name char
|
|
|
|
, Schema.array "friends" $ traverse character $ getFriends char
|
|
|
|
, Schema.enum "appearsIn" $ return $ foldMap getEpisode $ appearsIn char
|
|
|
|
, Schema.scalar "secretBackstory" $ secretBackstory char
|
|
|
|
, Schema.scalar "homePlanet" $ return $ either mempty homePlanet char
|
|
|
|
, Schema.scalar "__typename" $ return $ typeName char
|
|
|
|
]
|