webonyx / graphql-php

PHP implementation of the GraphQL specification based on the reference implementation in JavaScript
https://webonyx.github.io/graphql-php
MIT License
4.64k stars 564 forks source link

Passing array to misspelled query/mutation results in error #1571

Closed Jemt closed 4 months ago

Jemt commented 4 months ago

Hi,

Today I accidentally misspelled addUser in the mutation below (missing r at the end).

mutation {
  addUse(
    username: "Test001"
    password: "P@ssw0rd"
    email: "test@localhost"
    name: "James"
    surname: "Bond"
    permissions: ["crm", "intranet"]
  ) { id }
}

This resulted in a very confusing error:

Fatal error:
Uncaught TypeError: GraphQL\Type\Definition\Type::getNullableType(): Argument #1 ($type) must be of type GraphQL\Type\Definition\Type, null given, called in /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Validator/Rules/ValuesOfCorrectType.php on line 75 and defined in /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Type/Definition/Type.php:294
Stack trace:
#0 /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Validator/Rules/ValuesOfCorrectType.php(75): GraphQL\Type\Definition\Type::getNullableType(NULL)
#1 /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Language/Visitor.php(414): GraphQL\Validator\Rules\ValuesOfCorrectType->GraphQL\Validator\Rules\{closure}(Object(GraphQL\Language\AST\ListValueNode), 'value', Object(GraphQL\Language\AST\ArgumentNode), Array, Array)
#2 /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Language/Visitor.php(470): GraphQL\Language\Visitor::GraphQL\Language\{closure}(Object(GraphQL\Language\AST\ListValueNode), 'value', Object(GraphQL\Language\AST\ArgumentNode), Array, Array)
#3 /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Language/Visitor.php(277): GraphQL\Language\Visitor::GraphQL\Language\{closure}(Object(GraphQL\Language\AST\ListValueNode), 'value', Object(GraphQL\Language\AST\ArgumentNode), Array, Array)
#4 /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Validator/DocumentValidator.php(224): GraphQL\Language\Visitor::visit(Object(GraphQL\Language\AST\DocumentNode), Array)
#5 /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Validator/DocumentValidator.php(116): GraphQL\Validator\DocumentValidator::visitUsingRules(Object(GraphQL\Type\Schema), Object(GraphQL\Utils\TypeInfo), Object(GraphQL\Language\AST\DocumentNode), Array)
#6 /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/GraphQL.php(153): GraphQL\Validator\DocumentValidator::validate(Object(GraphQL\Type\Schema), Object(GraphQL\Language\AST\DocumentNode), Array)
#7 /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/GraphQL.php(94): GraphQL\GraphQL::promiseToExecute(Object(GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter), Object(GraphQL\Type\Schema), 'mutation {\n  ad...', Array, NULL, Array, NULL, NULL, NULL)
#8 /Users/jemt/www/WebApp-Webonyx-test/Server/WebApp/GraphQl.php(123): GraphQL\GraphQL::executeQuery(Object(GraphQL\Type\Schema), 'mutation {\n  ad...', Array, NULL, Array, NULL, NULL)
#9 /Users/jemt/www/WebApp-Webonyx-test/Server/Api/Users/Users.php(15): WebApp\GraphQl\Endpoint->Start()
#10 {main}
  thrown in /Users/jemt/www/WebApp-Webonyx-test/Server/vendor/webonyx/graphql-php/src/Type/Definition/Type.php
on line 294

Turns out the problem was related to the presence of the string array: permissions: ["crm", "intranet"]. Once I removed that argument, I got a helpful message pointing me in the direction of a misspelled field:

{
    "errors": [
        {
            "message": "Cannot query field \"addUse\" on type \"Mutation\". Did you mean \"addUser\"?",
            "extensions": {
                "category": "graphql"
            },
            "locations": [
                {
                    "line": 2,
                    "column": 3
                }
            ]
        }
    ]
}

It took me quite a while to realize that the problem was related to a misspelled field, so I think it could save us all some time if this could be improved 🤞 Thanks 😊

Jemt commented 4 months ago

I was using version 14.11.3. Upgrading to 15.12.1 solved the problem. Sorry about that.