2016-02-19 19:21:32 +01:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2016-02-12 13:27:46 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-07-14 05:58:05 +02:00
|
|
|
module Test.StarWars.Schema
|
|
|
|
( character
|
|
|
|
, droid
|
|
|
|
, hero
|
|
|
|
, human
|
|
|
|
, schema
|
|
|
|
) where
|
2016-02-12 13:27:46 +01:00
|
|
|
|
2019-07-02 20:07:26 +02:00
|
|
|
import Control.Monad.Trans.Except (throwE)
|
|
|
|
import Control.Monad.Trans.Class (lift)
|
2019-07-08 10:15:47 +02:00
|
|
|
import Control.Monad.IO.Class (MonadIO(..))
|
2019-08-30 07:26:04 +02:00
|
|
|
import Data.List.NonEmpty (NonEmpty(..))
|
2019-09-01 02:53:15 +02:00
|
|
|
import Data.Maybe (catMaybes)
|
2019-07-07 06:31:53 +02:00
|
|
|
import qualified Language.GraphQL.Schema as Schema
|
2019-07-02 20:07:26 +02:00
|
|
|
import Language.GraphQL.Trans
|
2019-11-03 10:42:10 +01:00
|
|
|
import qualified Language.GraphQL.Type as Type
|
2016-02-12 13:27:46 +01:00
|
|
|
import Test.StarWars.Data
|
|
|
|
|
|
|
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsSchema.js
|
|
|
|
|
2019-08-30 07:26:04 +02:00
|
|
|
schema :: MonadIO m => NonEmpty (Schema.Resolver m)
|
2017-01-28 18:15:14 +01:00
|
|
|
schema = hero :| [human, droid]
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2019-08-30 07:26:04 +02:00
|
|
|
hero :: MonadIO m => Schema.Resolver m
|
2016-02-19 19:21:32 +01:00
|
|
|
hero = Schema.objectA "hero" $ \case
|
|
|
|
[] -> character artoo
|
2019-11-03 10:42:10 +01:00
|
|
|
[Schema.Argument "episode" (Schema.Enum "NEWHOPE")] -> character $ getHero 4
|
|
|
|
[Schema.Argument "episode" (Schema.Enum "EMPIRE" )] -> character $ getHero 5
|
|
|
|
[Schema.Argument "episode" (Schema.Enum "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-08-30 07:26:04 +02:00
|
|
|
human :: MonadIO m => Schema.Resolver m
|
2019-07-05 20:05:04 +02:00
|
|
|
human = Schema.wrappedObjectA "human" $ \case
|
2019-11-03 10:42:10 +01:00
|
|
|
[Schema.Argument "id" (Schema.String i)] -> do
|
2019-07-03 17:54:50 +02:00
|
|
|
humanCharacter <- lift $ return $ getHuman i >>= Just
|
|
|
|
case humanCharacter of
|
2019-11-03 10:42:10 +01:00
|
|
|
Nothing -> return Type.Null
|
|
|
|
Just e -> Type.Named <$> character e
|
2019-07-02 20:07:26 +02:00
|
|
|
_ -> ActionT $ throwE "Invalid arguments."
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2019-08-30 07:26:04 +02:00
|
|
|
droid :: MonadIO m => Schema.Resolver m
|
2016-02-19 19:21:32 +01:00
|
|
|
droid = Schema.objectA "droid" $ \case
|
2019-11-03 10:42:10 +01:00
|
|
|
[Schema.Argument "id" (Schema.String i)] -> character =<< liftIO (getDroid i)
|
2019-07-02 20:07:26 +02:00
|
|
|
_ -> ActionT $ throwE "Invalid arguments."
|
|
|
|
|
2019-08-30 07:26:04 +02:00
|
|
|
character :: MonadIO m => Character -> ActionT m [Schema.Resolver m]
|
2019-07-02 20:07:26 +02:00
|
|
|
character char = return
|
2019-07-05 20:05:04 +02:00
|
|
|
[ Schema.scalar "id" $ return $ id_ char
|
|
|
|
, Schema.scalar "name" $ return $ name char
|
|
|
|
, Schema.wrappedObject "friends"
|
2019-11-03 10:42:10 +01:00
|
|
|
$ traverse character $ Type.List $ Type.Named <$> getFriends char
|
|
|
|
, Schema.wrappedScalar "appearsIn" $ return . Type.List
|
2019-09-01 02:53:15 +02:00
|
|
|
$ catMaybes (getEpisode <$> appearsIn char)
|
2019-07-02 20:07:26 +02:00
|
|
|
, Schema.scalar "secretBackstory" $ secretBackstory char
|
2019-07-05 20:05:04 +02:00
|
|
|
, Schema.scalar "homePlanet" $ return $ either mempty homePlanet char
|
|
|
|
, Schema.scalar "__typename" $ return $ typeName char
|
2019-07-02 20:07:26 +02:00
|
|
|
]
|