2020-05-14 09:17:14 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
module Test.RootOperationSpec
|
|
|
|
( spec
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.Aeson ((.=), object)
|
2020-05-21 10:20:59 +02:00
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
2020-05-14 09:17:14 +02:00
|
|
|
import Language.GraphQL
|
|
|
|
import qualified Language.GraphQL.Schema as Schema
|
|
|
|
import Test.Hspec (Spec, describe, it, shouldBe)
|
|
|
|
import Text.RawString.QQ (r)
|
|
|
|
import Language.GraphQL.Type.Definition
|
2020-05-24 13:51:00 +02:00
|
|
|
import qualified Language.GraphQL.Type.Out as Out
|
2020-05-14 09:17:14 +02:00
|
|
|
import Language.GraphQL.Type.Schema
|
|
|
|
|
2020-05-25 07:41:21 +02:00
|
|
|
hatType :: Out.ObjectType IO
|
2020-05-26 11:13:55 +02:00
|
|
|
hatType = Out.ObjectType "Hat" Nothing []
|
2020-05-21 10:20:59 +02:00
|
|
|
$ HashMap.singleton resolverName
|
2020-05-25 07:41:21 +02:00
|
|
|
$ Out.Field Nothing (Out.NamedScalarType int) mempty resolve
|
2020-05-21 10:20:59 +02:00
|
|
|
where
|
|
|
|
(Schema.Resolver resolverName resolve) =
|
2020-05-24 13:51:00 +02:00
|
|
|
Schema.Resolver "circumference" $ pure $ Out.Int 60
|
2020-05-21 10:20:59 +02:00
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
schema :: Schema IO
|
|
|
|
schema = Schema
|
2020-05-26 11:13:55 +02:00
|
|
|
(Out.ObjectType "Query" Nothing [] hatField)
|
|
|
|
(Just $ Out.ObjectType "Mutation" Nothing [] incrementField)
|
2020-05-14 09:17:14 +02:00
|
|
|
where
|
2020-05-24 13:51:00 +02:00
|
|
|
garment = pure $ Schema.object
|
|
|
|
[ Schema.Resolver "circumference" $ pure $ Out.Int 60
|
2020-05-14 09:17:14 +02:00
|
|
|
]
|
2020-05-23 06:46:21 +02:00
|
|
|
incrementField = HashMap.singleton "incrementCircumference"
|
2020-05-25 07:41:21 +02:00
|
|
|
$ Out.Field Nothing (Out.NamedScalarType int) mempty
|
2020-05-24 13:51:00 +02:00
|
|
|
$ pure $ Out.Int 61
|
2020-05-23 06:46:21 +02:00
|
|
|
hatField = HashMap.singleton "garment"
|
2020-05-25 07:41:21 +02:00
|
|
|
$ Out.Field Nothing (Out.NamedObjectType hatType) mempty garment
|
2020-05-14 09:17:14 +02:00
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec =
|
|
|
|
describe "Root operation type" $ do
|
|
|
|
it "returns objects from the root resolvers" $ do
|
|
|
|
let querySource = [r|
|
|
|
|
{
|
|
|
|
garment {
|
|
|
|
circumference
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expected = object
|
|
|
|
[ "data" .= object
|
|
|
|
[ "garment" .= object
|
|
|
|
[ "circumference" .= (60 :: Int)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
actual <- graphql schema querySource
|
|
|
|
actual `shouldBe` expected
|
|
|
|
|
|
|
|
it "chooses Mutation" $ do
|
|
|
|
let querySource = [r|
|
|
|
|
mutation {
|
|
|
|
incrementCircumference
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expected = object
|
|
|
|
[ "data" .= object
|
|
|
|
[ "incrementCircumference" .= (61 :: Int)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
actual <- graphql schema querySource
|
|
|
|
actual `shouldBe` expected
|