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
|
2019-11-03 10:42:10 +01:00
|
|
|
import qualified Language.GraphQL.Type as Type
|
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-23 06:46:21 +02:00
|
|
|
queryType = ObjectType "Query" Nothing $ HashMap.fromList
|
|
|
|
[ ("hero", Field Nothing (ScalarOutputType string) mempty hero)
|
|
|
|
, ("human", Field Nothing (ScalarOutputType string) mempty human)
|
|
|
|
, ("droid", Field Nothing (ScalarOutputType string) mempty droid)
|
|
|
|
]
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2020-05-23 06:46:21 +02:00
|
|
|
hero :: FieldResolver Identity
|
|
|
|
hero = NestingResolver $ 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
|
2019-12-31 08:29:03 +01:00
|
|
|
Schema.Enum "NEWHOPE" -> getHero 4
|
|
|
|
Schema.Enum "EMPIRE" -> getHero 5
|
|
|
|
Schema.Enum "JEDI" -> getHero 6
|
|
|
|
_ -> artoo
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2020-05-23 06:46:21 +02:00
|
|
|
human :: FieldResolver Identity
|
|
|
|
human = NestingResolver $ do
|
2019-12-31 08:29:03 +01:00
|
|
|
id' <- argument "id"
|
|
|
|
case id' of
|
|
|
|
Schema.String i -> do
|
|
|
|
humanCharacter <- lift $ return $ getHuman i >>= Just
|
|
|
|
case humanCharacter of
|
2020-05-23 06:46:21 +02:00
|
|
|
Nothing -> pure Type.Null
|
|
|
|
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-23 06:46:21 +02:00
|
|
|
droid :: FieldResolver Identity
|
|
|
|
droid = NestingResolver $ do
|
2019-12-31 08:29:03 +01:00
|
|
|
id' <- argument "id"
|
|
|
|
case id' of
|
2020-05-23 06:46:21 +02:00
|
|
|
Schema.String i -> getDroid i >>= pure . character
|
2019-12-31 08:29:03 +01:00
|
|
|
_ -> ActionT $ throwE "Invalid arguments."
|
2019-07-02 20:07:26 +02:00
|
|
|
|
2020-05-23 06:46:21 +02:00
|
|
|
character :: Character -> Type.Wrapping (FieldResolver Identity)
|
|
|
|
character char = Schema.object
|
|
|
|
[ Schema.wrappedObject "id" $ pure $ Type.S $ id_ char
|
|
|
|
, Schema.wrappedObject "name" $ pure $ Type.S $ name_ char
|
2019-07-05 20:05:04 +02:00
|
|
|
, Schema.wrappedObject "friends"
|
2020-05-23 06:46:21 +02:00
|
|
|
$ pure
|
|
|
|
$ Type.List
|
|
|
|
$ fmap character
|
|
|
|
$ getFriends char
|
|
|
|
, Schema.wrappedObject "appearsIn" $ pure
|
|
|
|
$ Type.List $ Type.E <$> catMaybes (getEpisode <$> appearsIn char)
|
|
|
|
, Schema.wrappedObject "secretBackstory" $ Type.S <$> secretBackstory char
|
|
|
|
, Schema.wrappedObject "homePlanet" $ pure $ Type.S $ either mempty homePlanet char
|
|
|
|
, Schema.wrappedObject "__typename" $ pure $ Type.S $ typeName char
|
2019-07-02 20:07:26 +02:00
|
|
|
]
|