2020-07-15 19:15:31 +02:00
|
|
|
{- 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
|
2020-07-19 07:36:06 +02:00
|
|
|
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
|
2020-07-19 07:36:06 +02:00
|
|
|
import Test.Hspec.GraphQL
|
2020-05-14 09:17:14 +02:00
|
|
|
|
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-27 23:18:35 +02:00
|
|
|
$ HashMap.singleton "circumference"
|
2020-07-14 19:37:56 +02:00
|
|
|
$ 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
|
2020-10-07 05:24:51 +02:00
|
|
|
garmentSchema = schema queryType (Just mutationType) Nothing mempty
|
2020-05-14 09:17:14 +02:00
|
|
|
where
|
2020-10-07 05:24:51 +02:00
|
|
|
queryType = Out.ObjectType "Query" Nothing [] hatFieldResolver
|
|
|
|
mutationType = Out.ObjectType "Mutation" Nothing [] incrementFieldResolver
|
2020-05-27 23:18:35 +02:00
|
|
|
garment = pure $ Object $ HashMap.fromList
|
|
|
|
[ ("circumference", Int 60)
|
2020-05-14 09:17:14 +02:00
|
|
|
]
|
2020-07-14 19:37:56 +02:00
|
|
|
incrementFieldResolver = HashMap.singleton "incrementCircumference"
|
|
|
|
$ ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
|
2020-05-27 23:18:35 +02:00
|
|
|
$ pure $ Int 61
|
2020-07-14 19:37:56 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2020-07-19 07:36:06 +02:00
|
|
|
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
|
2020-07-19 07:36:06 +02:00
|
|
|
actual `shouldResolveTo` expected
|
2020-05-14 09:17:14 +02:00
|
|
|
|
|
|
|
it "chooses Mutation" $ do
|
|
|
|
let querySource = [r|
|
|
|
|
mutation {
|
|
|
|
incrementCircumference
|
|
|
|
}
|
|
|
|
|]
|
2020-07-19 07:36:06 +02:00
|
|
|
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
|
2020-07-19 07:36:06 +02:00
|
|
|
actual `shouldResolveTo` expected
|