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)
|
2020-02-01 20:46:35 +01:00
|
|
|
import Data.Functor.Identity (Identity)
|
2020-05-23 06:46:21 +02:00
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
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
|
2020-05-14 09:17:14 +02:00
|
|
|
import Language.GraphQL.Type.Definition
|
2020-05-24 13:51:00 +02:00
|
|
|
import qualified Language.GraphQL.Type.In as In
|
|
|
|
import qualified Language.GraphQL.Type.Out as Out
|
2020-05-14 09:17:14 +02:00
|
|
|
import Language.GraphQL.Type.Schema
|
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
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
schema :: Schema Identity
|
|
|
|
schema = Schema { query = queryType, mutation = Nothing }
|
|
|
|
where
|
2020-05-26 11:13:55 +02:00
|
|
|
queryType = Out.ObjectType "Query" Nothing [] $ HashMap.fromList
|
2020-05-25 07:41:21 +02:00
|
|
|
[ ("hero", Out.Field Nothing (Out.NamedScalarType string) mempty hero)
|
|
|
|
, ("human", Out.Field Nothing (Out.NamedScalarType string) mempty human)
|
|
|
|
, ("droid", Out.Field Nothing (Out.NamedScalarType string) mempty droid)
|
2020-05-23 06:46:21 +02:00
|
|
|
]
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2020-05-24 13:51:00 +02:00
|
|
|
hero :: ActionT Identity (Out.Value Identity)
|
|
|
|
hero = do
|
2019-12-31 08:29:03 +01:00
|
|
|
episode <- argument "episode"
|
2020-05-23 06:46:21 +02:00
|
|
|
pure $ character $ case episode of
|
2020-05-24 13:51:00 +02:00
|
|
|
In.Enum "NEWHOPE" -> getHero 4
|
|
|
|
In.Enum "EMPIRE" -> getHero 5
|
|
|
|
In.Enum "JEDI" -> getHero 6
|
2019-12-31 08:29:03 +01:00
|
|
|
_ -> artoo
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2020-05-24 13:51:00 +02:00
|
|
|
human :: ActionT Identity (Out.Value Identity)
|
|
|
|
human = do
|
2019-12-31 08:29:03 +01:00
|
|
|
id' <- argument "id"
|
|
|
|
case id' of
|
2020-05-24 13:51:00 +02:00
|
|
|
In.String i -> do
|
2019-12-31 08:29:03 +01:00
|
|
|
humanCharacter <- lift $ return $ getHuman i >>= Just
|
|
|
|
case humanCharacter of
|
2020-05-24 13:51:00 +02:00
|
|
|
Nothing -> pure Out.Null
|
2020-05-23 06:46:21 +02:00
|
|
|
Just e -> pure $ character e
|
2019-12-31 08:29:03 +01:00
|
|
|
_ -> ActionT $ throwE "Invalid arguments."
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2020-05-24 13:51:00 +02:00
|
|
|
droid :: ActionT Identity (Out.Value Identity)
|
|
|
|
droid = do
|
2019-12-31 08:29:03 +01:00
|
|
|
id' <- argument "id"
|
|
|
|
case id' of
|
2020-05-25 07:41:21 +02:00
|
|
|
In.String i -> character <$> getDroid i
|
2019-12-31 08:29:03 +01:00
|
|
|
_ -> ActionT $ throwE "Invalid arguments."
|
2019-07-02 20:07:26 +02:00
|
|
|
|
2020-05-24 13:51:00 +02:00
|
|
|
character :: Character -> Out.Value Identity
|
2020-05-23 06:46:21 +02:00
|
|
|
character char = Schema.object
|
2020-05-24 13:51:00 +02:00
|
|
|
[ Schema.Resolver "id" $ pure $ Out.String $ id_ char
|
|
|
|
, Schema.Resolver "name" $ pure $ Out.String $ name_ char
|
|
|
|
, Schema.Resolver "friends"
|
2020-05-25 07:41:21 +02:00
|
|
|
$ pure $ Out.List $ character <$> getFriends char
|
2020-05-24 13:51:00 +02:00
|
|
|
, Schema.Resolver "appearsIn" $ pure
|
|
|
|
$ Out.List $ Out.Enum <$> catMaybes (getEpisode <$> appearsIn char)
|
|
|
|
, Schema.Resolver "secretBackstory" $ Out.String
|
|
|
|
<$> secretBackstory char
|
|
|
|
, Schema.Resolver "homePlanet" $ pure $ Out.String
|
|
|
|
$ either mempty homePlanet char
|
|
|
|
, Schema.Resolver "__typename" $ pure $ Out.String
|
|
|
|
$ typeName char
|
2019-07-02 20:07:26 +02:00
|
|
|
]
|