From e8c54810f8978b29e136ac0e1d91db8545a3f5f5 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 12 Jun 2020 07:58:08 +0200 Subject: Merge selection sets --- tests/Language/GraphQL/ExecuteSpec.hs | 47 +++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'tests/Language/GraphQL') diff --git a/tests/Language/GraphQL/ExecuteSpec.hs b/tests/Language/GraphQL/ExecuteSpec.hs index d0e7a66..62c6f25 100644 --- a/tests/Language/GraphQL/ExecuteSpec.hs +++ b/tests/Language/GraphQL/ExecuteSpec.hs @@ -22,21 +22,54 @@ schema = Schema {query = queryType, mutation = Nothing} queryType :: Out.ObjectType Identity queryType = Out.ObjectType "Query" Nothing [] - $ HashMap.singleton "count" - $ Out.Resolver countField + $ HashMap.singleton "philosopher" + $ Out.Resolver philosopherField $ pure - $ Int 8 + $ Object mempty where - countField = Out.Field Nothing (Out.NonNullScalarType int) HashMap.empty + philosopherField = + Out.Field Nothing (Out.NonNullObjectType philosopherType) HashMap.empty + +philosopherType :: Out.ObjectType Identity +philosopherType = Out.ObjectType "Philosopher" Nothing [] + $ HashMap.fromList resolvers + where + resolvers = + [ ("firstName", firstNameResolver) + , ("lastName", lastNameResolver) + ] + firstNameResolver = Out.Resolver firstNameField $ pure $ String "Friedrich" + lastNameResolver = Out.Resolver lastNameField $ pure $ String "Nietzsche" + firstNameField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty + lastNameField = Out.Field Nothing (Out.NonNullScalarType string) HashMap.empty spec :: Spec spec = - describe "execute" $ + describe "execute" $ do it "skips unknown fields" $ let expected = Aeson.object - ["data" .= Aeson.object ["count" .= (8 :: Int)]] + [ "data" .= Aeson.object + [ "philosopher" .= Aeson.object + [ "firstName" .= ("Friedrich" :: String) + ] + ] + ] + execute' = execute schema (mempty :: HashMap Name Aeson.Value) + actual = runIdentity + $ either parseError execute' + $ parse document "" "{ philosopher { firstName surname } }" + in actual `shouldBe` expected + it "merges selections" $ + let expected = Aeson.object + [ "data" .= Aeson.object + [ "philosopher" .= Aeson.object + [ "firstName" .= ("Friedrich" :: String) + , "lastName" .= ("Nietzsche" :: String) + ] + ] + ] execute' = execute schema (mempty :: HashMap Name Aeson.Value) actual = runIdentity $ either parseError execute' - $ parse document "" "{ count number }" + $ parse document "" "{ philosopher { firstName } philosopher { lastName } }" in actual `shouldBe` expected -- cgit v1.2.3