diff options
Diffstat (limited to 'tests/Language')
| -rw-r--r-- | tests/Language/GraphQL/ExecuteSpec.hs | 55 | ||||
| -rw-r--r-- | tests/Language/GraphQL/Validate/RulesSpec.hs | 54 |
2 files changed, 86 insertions, 23 deletions
diff --git a/tests/Language/GraphQL/ExecuteSpec.hs b/tests/Language/GraphQL/ExecuteSpec.hs index 65033ae..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] @@ -295,7 +307,7 @@ spec = let data'' = Object $ HashMap.singleton "philosopher" Null executionErrors = pure $ Error { message = - "Value completion error. Expected type !School, found: EXISTENTIALISM." + "Value completion error. Expected type School!, found: EXISTENTIALISM." , locations = [Location 1 17] , path = [Segment "philosopher", Segment "school"] } @@ -307,7 +319,7 @@ spec = let data'' = Object $ HashMap.singleton "philosopher" Null executionErrors = pure $ Error { message = - "Value completion error. Expected type !Interest, found: { instrument: \"piano\" }." + "Value completion error. Expected type Interest!, found: { instrument: \"piano\" }." , locations = [Location 1 17] , path = [Segment "philosopher", Segment "interest"] } @@ -319,7 +331,7 @@ spec = let data'' = Object $ HashMap.singleton "philosopher" Null executionErrors = pure $ Error { message - = "Value completion error. Expected type !Work, found:\ + = "Value completion error. Expected type Work!, found:\ \ { title: \"Also sprach Zarathustra: Ein Buch f\252r Alle und Keinen\" }." , locations = [Location 1 17] , path = [Segment "philosopher", Segment "majorWork"] @@ -328,22 +340,10 @@ 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 - { message = "Unable to coerce result to !Int." + { message = "Unable to coerce result to Int!." , locations = [Location 1 26] , path = [Segment "philosopher", Segment "century"] } @@ -364,7 +364,7 @@ spec = it "sets data to null if a root field isn't nullable" $ let executionErrors = pure $ Error - { message = "Unable to coerce result to !Int." + { message = "Unable to coerce result to Int!." , locations = [Location 1 3] , path = [Segment "count"] } @@ -375,7 +375,7 @@ spec = it "detects nullability errors" $ let data'' = Object $ HashMap.singleton "philosopher" Null executionErrors = pure $ Error - { message = "Value completion error. Expected type !String, found: null." + { message = "Value completion error. Expected type String!, found: null." , locations = [Location 1 26] , path = [Segment "philosopher", Segment "firstLanguage"] } @@ -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" diff --git a/tests/Language/GraphQL/Validate/RulesSpec.hs b/tests/Language/GraphQL/Validate/RulesSpec.hs index 7a5f4cc..7bdbd86 100644 --- a/tests/Language/GraphQL/Validate/RulesSpec.hs +++ b/tests/Language/GraphQL/Validate/RulesSpec.hs @@ -29,6 +29,7 @@ queryType = ObjectType "Query" Nothing [] $ HashMap.fromList [ ("dog", dogResolver) , ("cat", catResolver) , ("findDog", findDogResolver) + , ("findCats", findCatsResolver) ] where dogField = Field Nothing (Out.NamedObjectType dogType) mempty @@ -39,6 +40,11 @@ queryType = ObjectType "Query" Nothing [] $ HashMap.fromList findDogResolver = ValueResolver findDogField $ pure Null catField = Field Nothing (Out.NamedObjectType catType) mempty catResolver = ValueResolver catField $ pure Null + findCatsArguments = HashMap.singleton "commands" + $ In.Argument Nothing (In.NonNullListType $ In.NonNullEnumType catCommandType) + $ Just $ List [] + findCatsField = Field Nothing (Out.NonNullListType $ Out.NonNullObjectType catType) findCatsArguments + findCatsResolver = ValueResolver findCatsField $ pure $ List [] catCommandType :: EnumType catCommandType = EnumType "CatCommand" Nothing $ HashMap.fromList @@ -538,7 +544,7 @@ spec = } in validate queryString `shouldContain` [expected] - context "noUndefinedVariablesRule" $ + context "noUndefinedVariablesRule" $ do it "rejects undefined variables" $ let queryString = [gql| query variableIsNotDefinedUsedInSingleFragment { @@ -560,7 +566,35 @@ spec = } in validate queryString `shouldBe` [expected] - context "noUnusedVariablesRule" $ + it "gets variable location inside an input object" $ + let queryString = [gql| + query { + findDog (complex: { name: $name }) { + name + } + } + |] + expected = Error + { message = "Variable \"$name\" is not defined." + , locations = [AST.Location 2 29] + } + in validate queryString `shouldBe` [expected] + + it "gets variable location inside an array" $ + let queryString = [gql| + query { + findCats (commands: [JUMP, $command]) { + name + } + } + |] + expected = Error + { message = "Variable \"$command\" is not defined." + , locations = [AST.Location 2 30] + } + in validate queryString `shouldBe` [expected] + + context "noUnusedVariablesRule" $ do it "rejects unused variables" $ let queryString = [gql| query variableUnused($atOtherHomes: Boolean) { @@ -577,6 +611,16 @@ spec = } in validate queryString `shouldBe` [expected] + it "detects variables in properties of input objects" $ + let queryString = [gql| + query withVar ($name: String!) { + findDog (complex: { name: $name }) { + name + } + } + |] + in validate queryString `shouldBe` [] + context "uniqueInputFieldNamesRule" $ it "rejects duplicate fields in input objects" $ let queryString = [gql| @@ -878,7 +922,7 @@ spec = { message = "Variable \"$dogCommandArg\" of type \ \\"DogCommand\" used in position expecting type \ - \\"!DogCommand\"." + \\"DogCommand!\"." , locations = [AST.Location 1 26] } in validate queryString `shouldBe` [expected] @@ -925,7 +969,7 @@ spec = |] expected = Error { message = - "Value 3 cannot be coerced to type \"!CatCommand\"." + "Value 3 cannot be coerced to type \"CatCommand!\"." , locations = [AST.Location 3 36] } in validate queryString `shouldBe` [expected] @@ -940,7 +984,7 @@ spec = |] expected = Error { message = - "Value 3 cannot be coerced to type \"!String\"." + "Value 3 cannot be coerced to type \"String!\"." , locations = [AST.Location 2 28] } in validate queryString `shouldBe` [expected] |
