From a961b168dbc0d594dab4eb38e14f0295af6d69b4 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 8 Nov 2023 20:05:51 +0100 Subject: [PATCH] Add a test for the input object coercion issue --- CHANGELOG.md | 1 + graphql.cabal | 2 +- tests/Language/GraphQL/ExecuteSpec.hs | 43 +++++++++++++++++++-------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08ee22d..3a87e7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to ### Fixed - `gql` removes not only leading `\n` but also `\r`. - Fix non nullable type string representation in executor error messages. +- Fix input objects not being coerced to lists. ## [1.2.0.1] - 2023-04-25 ### Fixed diff --git a/graphql.cabal b/graphql.cabal index 104b757..63498d9 100644 --- a/graphql.cabal +++ b/graphql.cabal @@ -21,7 +21,7 @@ extra-source-files: CHANGELOG.md README.md tested-with: - GHC == 9.2.8, + GHC == 9.4.7, GHC == 9.6.3 source-repository head diff --git a/tests/Language/GraphQL/ExecuteSpec.hs b/tests/Language/GraphQL/ExecuteSpec.hs index 58e0187..52bd410 100644 --- a/tests/Language/GraphQL/ExecuteSpec.hs +++ b/tests/Language/GraphQL/ExecuteSpec.hs @@ -69,6 +69,7 @@ queryType = Out.ObjectType "Query" Nothing [] , ("throwing", ValueResolver throwingField throwingResolver) , ("count", ValueResolver countField countResolver) , ("sequence", ValueResolver sequenceField sequenceResolver) + , ("withInputObject", ValueResolver withInputObjectField withInputObjectResolver) ] where philosopherField = @@ -89,6 +90,17 @@ queryType = Out.ObjectType "Query" Nothing [] let fieldType = Out.ListType $ Out.NonNullScalarType int in Out.Field Nothing fieldType HashMap.empty sequenceResolver = pure intSequence + withInputObjectResolver = pure $ Type.Int 0 + withInputObjectField = + Out.Field Nothing (Out.NonNullScalarType int) $ HashMap.fromList + [("values", In.Argument Nothing withInputObjectArgumentType Nothing)] + withInputObjectArgumentType = In.NonNullListType + $ In.NonNullInputObjectType inputObjectType + +inputObjectType :: In.InputObjectType +inputObjectType = In.InputObjectType "InputObject" Nothing $ + HashMap.singleton "name" $ + In.InputField Nothing (In.NonNullScalarType int) Nothing intSequence :: Value intSequence = Type.List [Type.Int 1, Type.Int 2, Type.Int 3] @@ -328,18 +340,6 @@ spec = sourceQuery = "{ philosopher { majorWork { title } } }" in sourceQuery `shouldResolveTo` expected - it "gives location information for invalid scalar arguments" $ - let data'' = Object $ HashMap.singleton "philosopher" Null - executionErrors = pure $ Error - { message = - "Argument \"id\" has invalid type. Expected type ID, found: True." - , locations = [Location 1 15] - , path = [Segment "philosopher"] - } - expected = Response data'' executionErrors - sourceQuery = "{ philosopher(id: true) { lastName } }" - in sourceQuery `shouldResolveTo` expected - it "gives location information for failed result coercion" $ let data'' = Object $ HashMap.singleton "philosopher" Null executionErrors = pure $ Error @@ -389,6 +389,25 @@ spec = sourceQuery = "{ sequence }" in sourceQuery `shouldResolveTo` expected + context "Arguments" $ do + it "gives location information for invalid scalar arguments" $ + let data'' = Object $ HashMap.singleton "philosopher" Null + executionErrors = pure $ Error + { message = + "Argument \"id\" has invalid type. Expected type ID, found: True." + , locations = [Location 1 15] + , path = [Segment "philosopher"] + } + expected = Response data'' executionErrors + sourceQuery = "{ philosopher(id: true) { lastName } }" + in sourceQuery `shouldResolveTo` expected + + it "puts an object in a list if needed" $ + let data'' = Object $ HashMap.singleton "withInputObject" $ Type.Int 0 + expected = Response data'' mempty + sourceQuery = "{ withInputObject(values: { name: 0 }) }" + in sourceQuery `shouldResolveTo` expected + context "queryError" $ do let namedQuery name = "query " <> name <> " { philosopher(id: \"1\") { interest } }" twoQueries = namedQuery "A" <> " " <> namedQuery "B"