summaryrefslogtreecommitdiff
path: root/tests/Language/GraphQL/RootOperationSpec.hs
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2021-11-20 07:20:31 +0100
committerEugen Wissner <belka@caraus.de>2021-11-20 07:20:31 +0100
commitfdd627bf5d7ae7a78f716ffb2245710e5e200477 (patch)
treed8af1fe82a38782c4313cc8d0a0a9a261bb3750b /tests/Language/GraphQL/RootOperationSpec.hs
parentb387c10d750783822e0ce40fb813355f132c50c9 (diff)
downloadgraphql-spice-fdd627bf5d7ae7a78f716ffb2245710e5e200477.tar.gz
Move functional tests
Diffstat (limited to 'tests/Language/GraphQL/RootOperationSpec.hs')
-rw-r--r--tests/Language/GraphQL/RootOperationSpec.hs72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/Language/GraphQL/RootOperationSpec.hs b/tests/Language/GraphQL/RootOperationSpec.hs
new file mode 100644
index 0000000..3c4779d
--- /dev/null
+++ b/tests/Language/GraphQL/RootOperationSpec.hs
@@ -0,0 +1,72 @@
+{- 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/. -}
+
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE QuasiQuotes #-}
+module Language.GraphQL.RootOperationSpec
+ ( spec
+ ) where
+
+import Data.Aeson ((.=), object)
+import qualified Data.HashMap.Strict as HashMap
+import Language.GraphQL
+import Test.Hspec (Spec, describe, it)
+import Language.GraphQL.TH
+import Language.GraphQL.Type
+import qualified Language.GraphQL.Type.Out as Out
+import Test.Hspec.GraphQL
+
+hatType :: Out.ObjectType IO
+hatType = Out.ObjectType "Hat" Nothing []
+ $ HashMap.singleton "circumference"
+ $ ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
+ $ pure $ Int 60
+
+garmentSchema :: Schema IO
+garmentSchema = schema queryType (Just mutationType) Nothing mempty
+ where
+ queryType = Out.ObjectType "Query" Nothing [] hatFieldResolver
+ mutationType = Out.ObjectType "Mutation" Nothing [] incrementFieldResolver
+ garment = pure $ Object $ HashMap.fromList
+ [ ("circumference", Int 60)
+ ]
+ 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
+
+spec :: Spec
+spec =
+ describe "Root operation type" $ do
+ it "returns objects from the root resolvers" $ do
+ let querySource = [gql|
+ {
+ garment {
+ circumference
+ }
+ }
+ |]
+ expected = HashMap.singleton "data"
+ $ object
+ [ "garment" .= object
+ [ "circumference" .= (60 :: Int)
+ ]
+ ]
+ actual <- graphql garmentSchema querySource
+ actual `shouldResolveTo` expected
+
+ it "chooses Mutation" $ do
+ let querySource = [gql|
+ mutation {
+ incrementCircumference
+ }
+ |]
+ expected = HashMap.singleton "data"
+ $ object
+ [ "incrementCircumference" .= (61 :: Int)
+ ]
+ actual <- graphql garmentSchema querySource
+ actual `shouldResolveTo` expected