2016-02-12 13:27:46 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-05-27 23:18:35 +02:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2019-07-14 05:58:05 +02:00
|
|
|
module Test.StarWars.Schema
|
2020-05-27 23:18:35 +02:00
|
|
|
( schema
|
2019-07-14 05:58:05 +02:00
|
|
|
) where
|
2016-02-12 13:27:46 +01:00
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
import Control.Monad.Catch (MonadThrow(..), SomeException)
|
2020-05-27 23:18:35 +02:00
|
|
|
import Control.Monad.Trans.Reader (asks)
|
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)
|
2020-05-27 23:18:35 +02:00
|
|
|
import Data.Text (Text)
|
2020-06-19 10:53:41 +02:00
|
|
|
import Language.GraphQL.Type
|
2020-06-06 21:22:11 +02:00
|
|
|
import qualified Language.GraphQL.Type.In as In
|
2020-05-24 13:51:00 +02:00
|
|
|
import qualified Language.GraphQL.Type.Out as Out
|
2016-02-12 13:27:46 +01:00
|
|
|
import Test.StarWars.Data
|
2020-05-27 23:18:35 +02:00
|
|
|
import Prelude hiding (id)
|
2016-02-12 13:27:46 +01:00
|
|
|
|
|
|
|
-- See https://github.com/graphql/graphql-js/blob/master/src/__tests__/starWarsSchema.js
|
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
schema :: Schema (Either SomeException)
|
2020-07-15 19:15:31 +02:00
|
|
|
schema = Schema
|
|
|
|
{ query = queryType
|
|
|
|
, mutation = Nothing
|
|
|
|
, subscription = Nothing
|
|
|
|
}
|
2020-05-14 09:17:14 +02:00
|
|
|
where
|
2020-05-26 11:13:55 +02:00
|
|
|
queryType = Out.ObjectType "Query" Nothing [] $ HashMap.fromList
|
2020-07-14 19:37:56 +02:00
|
|
|
[ ("hero", heroFieldResolver)
|
|
|
|
, ("human", humanFieldResolver)
|
|
|
|
, ("droid", droidFieldResolver)
|
2020-05-23 06:46:21 +02:00
|
|
|
]
|
2020-07-14 19:37:56 +02:00
|
|
|
heroField = Out.Field Nothing (Out.NamedObjectType heroObject)
|
|
|
|
$ HashMap.singleton "episode"
|
2020-06-06 21:22:11 +02:00
|
|
|
$ In.Argument Nothing (In.NamedEnumType episodeEnum) Nothing
|
2020-07-14 19:37:56 +02:00
|
|
|
heroFieldResolver = ValueResolver heroField hero
|
|
|
|
humanField = Out.Field Nothing (Out.NamedObjectType heroObject)
|
|
|
|
$ HashMap.singleton "id"
|
2020-06-06 21:22:11 +02:00
|
|
|
$ In.Argument Nothing (In.NonNullScalarType string) Nothing
|
2020-07-14 19:37:56 +02:00
|
|
|
humanFieldResolver = ValueResolver humanField human
|
|
|
|
droidField = Out.Field Nothing (Out.NamedObjectType droidObject) mempty
|
|
|
|
droidFieldResolver = ValueResolver droidField droid
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
heroObject :: Out.ObjectType (Either SomeException)
|
2020-09-25 21:57:25 +02:00
|
|
|
heroObject = Out.ObjectType "Human" Nothing [characterType] $ HashMap.fromList
|
2020-06-29 13:14:23 +02:00
|
|
|
[ ("id", idFieldType)
|
|
|
|
, ("name", nameFieldType)
|
2020-09-25 21:57:25 +02:00
|
|
|
, ("friends", friendsFieldResolver)
|
|
|
|
, ("appearsIn", appearsInFieldResolver)
|
2020-06-29 13:14:23 +02:00
|
|
|
, ("homePlanet", homePlanetFieldType)
|
2020-09-25 21:57:25 +02:00
|
|
|
, ("secretBackstory", secretBackstoryFieldResolver)
|
|
|
|
, ("__typename", typenameFieldResolver)
|
2020-05-27 23:18:35 +02:00
|
|
|
]
|
2020-06-03 07:20:38 +02:00
|
|
|
where
|
2020-07-14 19:37:56 +02:00
|
|
|
homePlanetFieldType
|
|
|
|
= ValueResolver (Out.Field Nothing (Out.NamedScalarType string) mempty)
|
2020-09-25 21:57:25 +02:00
|
|
|
$ defaultResolver "homePlanet"
|
2020-05-27 23:18:35 +02:00
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
droidObject :: Out.ObjectType (Either SomeException)
|
2020-09-25 21:57:25 +02:00
|
|
|
droidObject = Out.ObjectType "Droid" Nothing [characterType] $ HashMap.fromList
|
2020-06-29 13:14:23 +02:00
|
|
|
[ ("id", idFieldType)
|
|
|
|
, ("name", nameFieldType)
|
2020-09-25 21:57:25 +02:00
|
|
|
, ("friends", friendsFieldResolver)
|
|
|
|
, ("appearsIn", appearsInFieldResolver)
|
2020-06-29 13:14:23 +02:00
|
|
|
, ("primaryFunction", primaryFunctionFieldType)
|
2020-09-25 21:57:25 +02:00
|
|
|
, ("secretBackstory", secretBackstoryFieldResolver)
|
|
|
|
, ("__typename", typenameFieldResolver)
|
2020-05-27 23:18:35 +02:00
|
|
|
]
|
2020-06-03 07:20:38 +02:00
|
|
|
where
|
2020-07-14 19:37:56 +02:00
|
|
|
primaryFunctionFieldType
|
|
|
|
= ValueResolver (Out.Field Nothing (Out.NamedScalarType string) mempty)
|
2020-09-25 21:57:25 +02:00
|
|
|
$ defaultResolver "primaryFunction"
|
2020-06-29 13:14:23 +02:00
|
|
|
|
2020-09-25 21:57:25 +02:00
|
|
|
typenameFieldResolver :: Resolver (Either SomeException)
|
|
|
|
typenameFieldResolver
|
2020-07-14 19:37:56 +02:00
|
|
|
= ValueResolver (Out.Field Nothing (Out.NamedScalarType string) mempty)
|
2020-09-25 21:57:25 +02:00
|
|
|
$ defaultResolver "__typename"
|
2020-06-03 07:20:38 +02:00
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
idFieldType :: Resolver (Either SomeException)
|
2020-09-25 21:57:25 +02:00
|
|
|
idFieldType = ValueResolver idField $ defaultResolver "id"
|
2020-06-03 07:20:38 +02:00
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
nameFieldType :: Resolver (Either SomeException)
|
2020-09-25 21:57:25 +02:00
|
|
|
nameFieldType = ValueResolver nameField $ defaultResolver "name"
|
2020-06-03 07:20:38 +02:00
|
|
|
|
2020-09-25 21:57:25 +02:00
|
|
|
friendsFieldResolver :: Resolver (Either SomeException)
|
|
|
|
friendsFieldResolver = ValueResolver friendsField $ defaultResolver "friends"
|
|
|
|
|
|
|
|
characterType :: InterfaceType (Either SomeException)
|
|
|
|
characterType = InterfaceType "Character" Nothing [] $ HashMap.fromList
|
|
|
|
[ ("id", idField)
|
|
|
|
, ("name", nameField)
|
|
|
|
, ("friends", friendsField)
|
|
|
|
, ("appearsIn", appearsInField)
|
|
|
|
, ("secretBackstory", secretBackstoryField)
|
|
|
|
]
|
|
|
|
|
|
|
|
idField :: Field (Either SomeException)
|
|
|
|
idField = Field Nothing (Out.NonNullScalarType id) mempty
|
|
|
|
|
|
|
|
nameField :: Field (Either SomeException)
|
|
|
|
nameField = Field Nothing (Out.NamedScalarType string) mempty
|
2020-06-03 07:20:38 +02:00
|
|
|
|
2020-09-25 21:57:25 +02:00
|
|
|
friendsField :: Field (Either SomeException)
|
|
|
|
friendsField = Field Nothing friendsFieldType mempty
|
2020-06-13 07:20:19 +02:00
|
|
|
where
|
2020-09-25 21:57:25 +02:00
|
|
|
friendsFieldType = Out.ListType (Out.NamedInterfaceType characterType)
|
2020-06-03 07:20:38 +02:00
|
|
|
|
2020-09-25 21:57:25 +02:00
|
|
|
appearsInField :: Field (Either SomeException)
|
|
|
|
appearsInField = Field appearsInDescription appearsInFieldType mempty
|
2020-07-14 19:37:56 +02:00
|
|
|
where
|
2020-09-25 21:57:25 +02:00
|
|
|
appearsInDescription = Just "Which movies they appear in."
|
|
|
|
appearsInFieldType = Out.ListType $ Out.NamedEnumType episodeEnum
|
|
|
|
|
|
|
|
secretBackstoryField :: Field (Either SomeException)
|
|
|
|
secretBackstoryField =
|
|
|
|
Out.Field Nothing (Out.NamedScalarType string) mempty
|
|
|
|
|
|
|
|
appearsInFieldResolver :: Resolver (Either SomeException)
|
|
|
|
appearsInFieldResolver = ValueResolver appearsInField
|
|
|
|
$ defaultResolver "appearsIn"
|
|
|
|
|
|
|
|
secretBackstoryFieldResolver :: Resolver (Either SomeException)
|
|
|
|
secretBackstoryFieldResolver = ValueResolver secretBackstoryField secretBackstory
|
2020-05-27 23:18:35 +02:00
|
|
|
|
2020-09-25 21:57:25 +02:00
|
|
|
defaultResolver :: Text -> Resolve (Either SomeException)
|
|
|
|
defaultResolver f = do
|
2020-07-17 07:05:03 +02:00
|
|
|
v <- asks values
|
2020-05-27 23:18:35 +02:00
|
|
|
let (Object v') = v
|
|
|
|
pure $ v' HashMap.! f
|
|
|
|
|
2020-06-06 21:22:11 +02:00
|
|
|
episodeEnum :: EnumType
|
2020-06-07 06:16:45 +02:00
|
|
|
episodeEnum = EnumType "Episode" (Just description)
|
|
|
|
$ HashMap.fromList [newHope, empire, jedi]
|
|
|
|
where
|
|
|
|
description = "One of the films in the Star Wars Trilogy"
|
|
|
|
newHope = ("NEW_HOPE", EnumValue $ Just "Released in 1977.")
|
|
|
|
empire = ("EMPIRE", EnumValue $ Just "Released in 1980.")
|
|
|
|
jedi = ("JEDI", EnumValue $ Just "Released in 1983.")
|
2020-06-06 21:22:11 +02:00
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
hero :: Resolve (Either SomeException)
|
2020-05-24 13:51:00 +02:00
|
|
|
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-06-13 07:20:19 +02:00
|
|
|
Enum "NEW_HOPE" -> getHero 4
|
2020-05-27 23:18:35 +02:00
|
|
|
Enum "EMPIRE" -> getHero 5
|
|
|
|
Enum "JEDI" -> getHero 6
|
2019-12-31 08:29:03 +01:00
|
|
|
_ -> artoo
|
2016-02-19 19:21:32 +01:00
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
human :: Resolve (Either SomeException)
|
2020-05-24 13:51:00 +02:00
|
|
|
human = do
|
2019-12-31 08:29:03 +01:00
|
|
|
id' <- argument "id"
|
|
|
|
case id' of
|
2020-07-14 19:37:56 +02:00
|
|
|
String i -> pure $ maybe Null character $ getHuman i >>= Just
|
2020-07-17 07:05:03 +02:00
|
|
|
_ -> throwM InvalidArguments
|
2020-07-14 19:37:56 +02:00
|
|
|
|
2020-07-17 07:05:03 +02:00
|
|
|
droid :: Resolve (Either SomeException)
|
2020-05-24 13:51:00 +02:00
|
|
|
droid = do
|
2019-12-31 08:29:03 +01:00
|
|
|
id' <- argument "id"
|
|
|
|
case id' of
|
2020-07-14 19:37:56 +02:00
|
|
|
String i -> pure $ maybe Null character $ getDroid i >>= Just
|
2020-07-17 07:05:03 +02:00
|
|
|
_ -> throwM InvalidArguments
|
2019-07-02 20:07:26 +02:00
|
|
|
|
2020-05-27 23:18:35 +02:00
|
|
|
character :: Character -> Value
|
|
|
|
character char = Object $ HashMap.fromList
|
|
|
|
[ ("id", String $ id_ char)
|
|
|
|
, ("name", String $ name_ char)
|
|
|
|
, ("friends", List $ character <$> getFriends char)
|
|
|
|
, ("appearsIn", List $ Enum <$> catMaybes (getEpisode <$> appearsIn char))
|
|
|
|
, ("homePlanet", String $ either mempty homePlanet char)
|
|
|
|
, ("__typename", String $ typeName char)
|
2019-07-02 20:07:26 +02:00
|
|
|
]
|