Skip to content
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

Fix request variables validation #239

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Execution/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Youshido\GraphQL\Execution;


use Youshido\GraphQL\Exception\Parser\InvalidRequestException;
use Youshido\GraphQL\Exception\Parser\SyntaxErrorException;
use Youshido\GraphQL\Exception\ResolveException;
use Youshido\GraphQL\Execution\Container\Container;
use Youshido\GraphQL\Execution\Context\ExecutionContext;
Expand Down Expand Up @@ -564,6 +566,13 @@ protected function resolveComposite(FieldInterface $field, AstFieldInterface $as
});
}

/**
* @param mixed $payload
* @param array $variables
*
* @throws InvalidRequestException
* @throws SyntaxErrorException
*/
protected function parseAndCreateRequest($payload, $variables = [])
{
if (empty($payload)) {
Expand Down
12 changes: 0 additions & 12 deletions src/Execution/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ public function __construct($data = [], $variables = [])
}

if (array_key_exists('variableReferences', $data)) {
foreach ($data['variableReferences'] as $ref) {
if (!array_key_exists($ref->getName(), $variables)) {
/** @var Variable $variable */
$variable = $ref->getVariable();
if ($variable->hasDefaultValue()) {
$variables[$variable->getName()] = $variable->getDefaultValue()->getValue();
continue;
}
throw new InvalidRequestException(sprintf("Variable %s hasn't been submitted", $ref->getName()), $ref->getLocation());
}
}

$this->addVariableReferences($data['variableReferences']);
}

Expand Down
37 changes: 31 additions & 6 deletions src/Validator/RequestValidator/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,75 @@
class RequestValidator implements RequestValidatorInterface
{

/**
* @param Request $request
*
* @throws InvalidRequestException
*/
public function validate(Request $request)
{
$this->assertFragmentReferencesValid($request);
$this->assetFragmentsUsed($request);
$this->assertFragmentsUsed($request);
$this->assertAllVariablesExists($request);
$this->assertAllVariablesUsed($request);
}

private function assetFragmentsUsed(Request $request)
/**
* @param Request $request
*
* @throws InvalidRequestException
*/
private function assertFragmentsUsed(Request $request)
{
foreach ($request->getFragmentReferences() as $fragmentReference) {
$request->getFragment($fragmentReference->getName())->setUsed(true);
}

foreach ($request->getFragments() as $fragment) {
if (!$fragment->isUsed()) {
throw new InvalidRequestException(sprintf('Fragment "%s" not used', $fragment->getName()), $fragment->getLocation());
throw new InvalidRequestException(sprintf('Fragment "%s" is not used', $fragment->getName()), $fragment->getLocation());
}
}
}

/**
* @param Request $request
*
* @throws InvalidRequestException
*/
private function assertFragmentReferencesValid(Request $request)
{
foreach ($request->getFragmentReferences() as $fragmentReference) {
if (!$request->getFragment($fragmentReference->getName())) {
throw new InvalidRequestException(sprintf('Fragment "%s" not defined in query', $fragmentReference->getName()), $fragmentReference->getLocation());
throw new InvalidRequestException(sprintf('Fragment "%s" is not defined in query', $fragmentReference->getName()), $fragmentReference->getLocation());
}
}
}

/**
* @param Request $request
*
* @throws InvalidRequestException
*/
private function assertAllVariablesExists(Request $request)
{
foreach ($request->getVariableReferences() as $variableReference) {
if (!$variableReference->getVariable()) {
throw new InvalidRequestException(sprintf('Variable "%s" not exists', $variableReference->getName()), $variableReference->getLocation());
throw new InvalidRequestException(sprintf('Variable "%s" does not exists', $variableReference->getName()), $variableReference->getLocation());
}
}
}

/**
* @param Request $request
*
* @throws InvalidRequestException
*/
private function assertAllVariablesUsed(Request $request)
{
foreach ($request->getQueryVariables() as $queryVariable) {
if (!$queryVariable->isUsed()) {
throw new InvalidRequestException(sprintf('Variable "%s" not used', $queryVariable->getName()), $queryVariable->getLocation());
throw new InvalidRequestException(sprintf('Variable "%s" is not used', $queryVariable->getName()), $queryVariable->getLocation());
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions tests/Issues/Issue238/MissingVariablesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Youshido\Tests\Issues\Issue238;

use PHPUnit\Framework\TestCase;
use Youshido\GraphQL\Execution\Processor;
use Youshido\Tests\StarWars\Schema\StarWarsSchema;

class MissingVariablesTest extends TestCase
{

/**
* @see https://github.com/youshido-php/GraphQL/issues/238
*/
public function testProperExceptionOnMissingVariables()
{
$payload = <<<GQL
query GetHero {
hero(episode:\$episode) {
name
}
}
GQL;

$schema = new StarWarsSchema();
$processor = new Processor($schema);

$processor->processPayload($payload);
$response = $processor->getResponseData();

$this->assertEquals([
'errors' => [
[
'message' => 'Variable "episode" does not exists',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leoloso this is the error you expect, right?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the more important error is that the variable is not defined in the operation name, which is mandatory in GraphQL. Hence the error should be something like Variable "episode" has not been defined in the operation name

'locations' => [
[
'line' => 2,
'column' => 18,
],
],
],
],
], $response);
}
}
2 changes: 1 addition & 1 deletion tests/Schema/VariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function queries()
[
'errors' => [
[
'message' => 'Fragment "someFragment" not defined in query',
'message' => 'Fragment "someFragment" is not defined in query',
'locations' => [
[
'line' => 3,
Expand Down