-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHP Fatal error when a variable is missing from operation name and dictionary of values #238
Comments
Can you provide the full stacktrace? |
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;
} |
Try setting the default value of |
I just did, it still fails, same exception |
Please notice that if you pass a variables dictionary including the variable |
I have to try reproducing on my own, it should fail with a better error, like "Variable $date not submitted". |
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. |
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. |
This problem can be fixed by checking in Currently: $variable = $ref->getVariable();
if ($variable->hasDefaultValue()) { Fix: $variable = $ref->getVariable();
if (!is_null($variable) && $variable->hasDefaultValue()) { If it is |
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 |
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. |
My solution is simple, because it notices that, if the variable was not defined in the operation, then this line will return $variable = $ref->getVariable(); Maybe this could be handled more elegantly, but this solution, at least, avoids the I'm submitting the PR now |
I get the following exception:
When executing a query which includes a variable, but:
Click on this link and run the query to reproduce the error (it doesn't show the PHP exception error though, only that there was an internal server error)
The text was updated successfully, but these errors were encountered: