aboutsummaryrefslogtreecommitdiff
path: root/tests/Test/FragmentSpec.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Test/FragmentSpec.hs')
-rw-r--r--tests/Test/FragmentSpec.hs107
1 files changed, 48 insertions, 59 deletions
diff --git a/tests/Test/FragmentSpec.hs b/tests/Test/FragmentSpec.hs
index 2924e63..089b721 100644
--- a/tests/Test/FragmentSpec.hs
+++ b/tests/Test/FragmentSpec.hs
@@ -1,23 +1,22 @@
+{- 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 Test.FragmentSpec
( spec
) where
-import Data.Aeson (object, (.=))
+import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text)
import Language.GraphQL
import Language.GraphQL.Type
import qualified Language.GraphQL.Type.Out as Out
-import Test.Hspec
- ( Spec
- , describe
- , it
- , shouldBe
- , shouldNotSatisfy
- )
+import Test.Hspec (Spec, describe, it)
+import Test.Hspec.GraphQL
import Text.RawString.QQ (r)
size :: (Text, Value)
@@ -46,33 +45,33 @@ inlineQuery = [r|{
}
}|]
-hasErrors :: Aeson.Value -> Bool
-hasErrors (Aeson.Object object') = HashMap.member "errors" object'
-hasErrors _ = True
-
shirtType :: Out.ObjectType IO
shirtType = Out.ObjectType "Shirt" Nothing []
$ HashMap.fromList
- [ ("size", Out.Resolver sizeFieldType $ pure $ snd size)
- , ("circumference", Out.Resolver circumferenceFieldType $ pure $ snd circumference)
+ [ ("size", sizeFieldType)
+ , ("circumference", circumferenceFieldType)
]
hatType :: Out.ObjectType IO
hatType = Out.ObjectType "Hat" Nothing []
$ HashMap.fromList
- [ ("size", Out.Resolver sizeFieldType $ pure $ snd size)
- , ("circumference", Out.Resolver circumferenceFieldType $ pure $ snd circumference)
+ [ ("size", sizeFieldType)
+ , ("circumference", circumferenceFieldType)
]
-circumferenceFieldType :: Out.Field IO
-circumferenceFieldType = Out.Field Nothing (Out.NamedScalarType int) mempty
+circumferenceFieldType :: Out.Resolver IO
+circumferenceFieldType
+ = Out.ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
+ $ pure $ snd circumference
-sizeFieldType :: Out.Field IO
-sizeFieldType = Out.Field Nothing (Out.NamedScalarType string) mempty
+sizeFieldType :: Out.Resolver IO
+sizeFieldType
+ = Out.ValueResolver (Out.Field Nothing (Out.NamedScalarType string) mempty)
+ $ pure $ snd size
toSchema :: Text -> (Text, Value) -> Schema IO
toSchema t (_, resolve) = Schema
- { query = queryType, mutation = Nothing }
+ { query = queryType, mutation = Nothing, subscription = Nothing }
where
unionMember = if t == "Hat" then hatType else shirtType
typeNameField = Out.Field Nothing (Out.NamedScalarType string) mempty
@@ -83,8 +82,8 @@ toSchema t (_, resolve) = Schema
"size" -> shirtType
_ -> Out.ObjectType "Query" Nothing []
$ HashMap.fromList
- [ ("garment", Out.Resolver garmentField $ pure resolve)
- , ("__typename", Out.Resolver typeNameField $ pure $ String "Shirt")
+ [ ("garment", ValueResolver garmentField (pure resolve))
+ , ("__typename", ValueResolver typeNameField (pure $ String "Shirt"))
]
spec :: Spec
@@ -92,25 +91,23 @@ spec = do
describe "Inline fragment executor" $ do
it "chooses the first selection if the type matches" $ do
actual <- graphql (toSchema "Hat" $ garment "Hat") inlineQuery
- let expected = object
- [ "data" .= object
- [ "garment" .= object
+ let expected = HashMap.singleton "data"
+ $ Aeson.object
+ [ "garment" .= Aeson.object
[ "circumference" .= (60 :: Int)
]
]
- ]
- in actual `shouldBe` expected
+ in actual `shouldResolveTo` expected
it "chooses the last selection if the type matches" $ do
actual <- graphql (toSchema "Shirt" $ garment "Shirt") inlineQuery
- let expected = object
- [ "data" .= object
- [ "garment" .= object
+ let expected = HashMap.singleton "data"
+ $ Aeson.object
+ [ "garment" .= Aeson.object
[ "size" .= ("L" :: Text)
]
]
- ]
- in actual `shouldBe` expected
+ in actual `shouldResolveTo` expected
it "embeds inline fragments without type" $ do
let sourceQuery = [r|{
@@ -124,15 +121,14 @@ spec = do
resolvers = ("garment", Object $ HashMap.fromList [circumference, size])
actual <- graphql (toSchema "garment" resolvers) sourceQuery
- let expected = object
- [ "data" .= object
- [ "garment" .= object
+ let expected = HashMap.singleton "data"
+ $ Aeson.object
+ [ "garment" .= Aeson.object
[ "circumference" .= (60 :: Int)
, "size" .= ("L" :: Text)
]
]
- ]
- in actual `shouldBe` expected
+ in actual `shouldResolveTo` expected
it "evaluates fragments on Query" $ do
let sourceQuery = [r|{
@@ -140,9 +136,7 @@ spec = do
size
}
}|]
-
- actual <- graphql (toSchema "size" size) sourceQuery
- actual `shouldNotSatisfy` hasErrors
+ in graphql (toSchema "size" size) `shouldResolve` sourceQuery
describe "Fragment spread executor" $ do
it "evaluates fragment spreads" $ do
@@ -157,12 +151,11 @@ spec = do
|]
actual <- graphql (toSchema "circumference" circumference) sourceQuery
- let expected = object
- [ "data" .= object
+ let expected = HashMap.singleton "data"
+ $ Aeson.object
[ "circumference" .= (60 :: Int)
]
- ]
- in actual `shouldBe` expected
+ in actual `shouldResolveTo` expected
it "evaluates nested fragments" $ do
let sourceQuery = [r|
@@ -182,19 +175,16 @@ spec = do
|]
actual <- graphql (toSchema "Hat" $ garment "Hat") sourceQuery
- let expected = object
- [ "data" .= object
- [ "garment" .= object
+ let expected = HashMap.singleton "data"
+ $ Aeson.object
+ [ "garment" .= Aeson.object
[ "circumference" .= (60 :: Int)
]
]
- ]
- in actual `shouldBe` expected
+ in actual `shouldResolveTo` expected
it "rejects recursive fragments" $ do
- let expected = object
- [ "data" .= object []
- ]
+ let expected = HashMap.singleton "data" $ Aeson.object []
sourceQuery = [r|
{
...circumferenceFragment
@@ -206,7 +196,7 @@ spec = do
|]
actual <- graphql (toSchema "circumference" circumference) sourceQuery
- actual `shouldBe` expected
+ actual `shouldResolveTo` expected
it "considers type condition" $ do
let sourceQuery = [r|
@@ -223,12 +213,11 @@ spec = do
size
}
|]
- expected = object
- [ "data" .= object
- [ "garment" .= object
+ expected = HashMap.singleton "data"
+ $ Aeson.object
+ [ "garment" .= Aeson.object
[ "circumference" .= (60 :: Int)
]
]
- ]
actual <- graphql (toSchema "Hat" $ garment "Hat") sourceQuery
- actual `shouldBe` expected
+ actual `shouldResolveTo` expected