Giter Club home page Giter Club logo

Comments (12)

Jalle19 avatar Jalle19 commented on June 8, 2024

Can you provide the full stacktrace?

from graphql.

leoloso avatar leoloso commented on June 8, 2024
[12-Jan-2020 10:00:26 UTC] PHP Warning:  array_key_exists() expects parameter 2 to be array, null given in /Users/leo/Sites/PoPAPIWP/vendor/youshido/graphql/src/Execution/Request.php on line 66
[12-Jan-2020 10:00:26 UTC] PHP Fatal error:  Uncaught Error: Call to a member function hasDefaultValue() on null in /Users/leo/Sites/PoPAPIWP/vendor/youshido/graphql/src/Execution/Request.php:69
Stack trace:
#0 /Users/leo/GitRepos/GitHub/Libraries/getpop/api-graphql-query/src/Schema/GraphQLQueryConvertor.php(255): Youshido\GraphQL\Execution\Request->__construct(Array, NULL)
#1 /Users/leo/GitRepos/GitHub/Libraries/getpop/api-graphql-query/src/Schema/GraphQLQueryConvertor.php(40): PoP\GraphQLAPIQuery\Schema\GraphQLQueryConvertor->parseAndCreateRequest('# query {\n#   _...', NULL)
#2 /Users/leo/GitRepos/GitHub/Libraries/getpop/api-graphql-request/src/Hooks/VarsHooks.php(60): PoP\GraphQLAPIQuery\Schema\GraphQLQueryConvertor->convertFromGraphQLToFieldQuery('# query {\n#   _...', NULL)
#3 /Users/leo/GitRepos/GitHub/Libraries/getpop/api-graphql-request/src/Hooks/VarsHooks.php(30): PoP\GraphQLAPIRequest\Hooks\VarsHooks->processURLParamVars(Array)
#4 /Users/leo/Sites/PoPAPIWP/wp/wp-includes/class-wp-hook.php(288): PoP\GraphQLAPIRequest\Hooks\VarsHooks->addURLPar in /Users/leo/Sites/PoPAPIWP/vendor/youshido/graphql/src/Execution/Request.php on line 69

My code, from which the error happens, is this line:

$request = new Request($parsedData, $variables);

from this code in my library (not Youshido):

protected function parseAndCreateRequest($payload, $variables = []): Request
{
    if (empty($payload)) {
        throw new InvalidArgumentException($this->translationAPI->__('Must provide an operation.'));
    }

    $parser  = new Parser();
    $parsedData = $parser->parse($payload);
    $request = new Request($parsedData, $variables);

    // If the validation fails, it will throw an exception
    (new RequestValidator())->validate($request);

    // Return the request
    return $request;
}

from graphql.

Jalle19 avatar Jalle19 commented on June 8, 2024

Try setting the default value of $variables to null instead of [].

from graphql.

leoloso avatar leoloso commented on June 8, 2024

I just did, it still fails, same exception

from graphql.

leoloso avatar leoloso commented on June 8, 2024

Please notice that if you pass a variables dictionary including the variable date it doesn't throw an exception. It only does when variable date is not present in the dictionary (and not defined in the operation name)

from graphql.

Jalle19 avatar Jalle19 commented on June 8, 2024

I have to try reproducing on my own, it should fail with a better error, like "Variable $date not submitted".

from graphql.

leoloso avatar leoloso commented on June 8, 2024

Currently it fails with a better error when either the variable is defined in the operation name but not in the variables, or the other way around. Only when both are missing it throws an exception.

I believe it should be straightforward to reproduce, since there's nothing special in my code.

from graphql.

Jalle19 avatar Jalle19 commented on June 8, 2024

I fixed it in #239. It will still error out of course, but the exception will come from the request validator instead of the parser.

from graphql.

leoloso avatar leoloso commented on June 8, 2024

This problem can be fixed by checking in Execution/Request.php, function __construct, that the variable that produces the exception is not null.

Currently:

$variable = $ref->getVariable();
if ($variable->hasDefaultValue()) {

Fix:

$variable = $ref->getVariable();
if (!is_null($variable) && $variable->hasDefaultValue()) {

If it is null, then the ensuing error message "Variable %s hasn't been submitted" is good enough (at least for the time being). Would you agree? Should I submit a PR? (I need to fix it, since this problem is happening in my PROD)

from graphql.

leoloso avatar leoloso commented on June 8, 2024

Ops, I closed by mistake.

This branch in my fork handles this solution a bit better:

https://github.com/getpop/graphql-parser/tree/fix-exception-when-no-args-declared

from graphql.

Jalle19 avatar Jalle19 commented on June 8, 2024

Feel free to make a PR. I don't remember what the issue was with my previous attempt anymore, I got a feeling it's not as easy to fix as it sounds like. Maybe your solution works better though.

from graphql.

leoloso avatar leoloso commented on June 8, 2024

My solution is simple, because it notices that, if the variable was not defined in the operation, then this line will return null:

$variable = $ref->getVariable();

Maybe this could be handled more elegantly, but this solution, at least, avoids the RuntimeException that was breaking my server.

I'm submitting the PR now

from graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.