graphql/tests/Test/RootOperationSpec.hs

73 lines
2.5 KiB
Haskell
Raw Permalink Normal View History

{- This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at https://mozilla.org/MPL/2.0/. -}
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 Test.Hspec (Spec, describe, it)
2020-05-14 09:17:14 +02:00
import Text.RawString.QQ (r)
2020-06-19 10:53:41 +02:00
import Language.GraphQL.Type
2020-05-24 13:51:00 +02:00
import qualified Language.GraphQL.Type.Out as Out
import Test.Hspec.GraphQL
2020-05-14 09:17:14 +02:00
hatType :: Out.ObjectType IO
hatType = Out.ObjectType "Hat" Nothing []
$ HashMap.singleton "circumference"
$ ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
$ pure $ Int 60
2020-05-21 10:20:59 +02:00
2020-09-28 07:06:15 +02:00
garmentSchema :: Schema IO
garmentSchema = schema queryType (Just mutationType) Nothing mempty
2020-05-14 09:17:14 +02:00
where
queryType = Out.ObjectType "Query" Nothing [] hatFieldResolver
mutationType = Out.ObjectType "Mutation" Nothing [] incrementFieldResolver
garment = pure $ Object $ HashMap.fromList
[ ("circumference", Int 60)
2020-05-14 09:17:14 +02:00
]
incrementFieldResolver = HashMap.singleton "incrementCircumference"
$ ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
$ pure $ Int 61
hatField = Out.Field Nothing (Out.NamedObjectType hatType) mempty
hatFieldResolver =
HashMap.singleton "garment" $ ValueResolver hatField 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 = HashMap.singleton "data"
$ object
2020-05-14 09:17:14 +02:00
[ "garment" .= object
[ "circumference" .= (60 :: Int)
]
]
2020-09-28 07:06:15 +02:00
actual <- graphql garmentSchema querySource
actual `shouldResolveTo` expected
2020-05-14 09:17:14 +02:00
it "chooses Mutation" $ do
let querySource = [r|
mutation {
incrementCircumference
}
|]
expected = HashMap.singleton "data"
$ object
2020-05-14 09:17:14 +02:00
[ "incrementCircumference" .= (61 :: Int)
]
2020-09-28 07:06:15 +02:00
actual <- graphql garmentSchema querySource
actual `shouldResolveTo` expected