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 overriding directives #245

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ protected function parseOperation($type = Token::TYPE_QUERY)
$this->eatMulti([Token::TYPE_COMMA]);

$operation = $this->parseBodyItem($type, true);
$operation->setDirectives($directives);
$operation->setDirectives(array_merge(
$directives,
$operation->getDirectives()
));

$fields[] = $operation;
}
Expand Down
58 changes: 58 additions & 0 deletions tests/Issues/Issue242/Issue242Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Youshido\Tests\Issue242;

use PHPUnit\Framework\TestCase;
use Youshido\GraphQL\Parser\Parser;
use Youshido\GraphQL\Parser\Location;
use Youshido\GraphQL\Parser\Ast\Field;
use Youshido\GraphQL\Parser\Ast\Query;
use Youshido\GraphQL\Parser\Ast\Argument;
use Youshido\GraphQL\Parser\Ast\Directive;
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Literal;

class Issue242Test extends TestCase
{
public function testDoNotOverrideFirstLevelQueryDirectives()
{
/**
* Without the patch, the operation query will override the first level
* query directives.
* In this case, directive `@include(if:false)` would be lost
* Please notice: the bug happens only when adding "query" at the beginning:
* query { ... }
* So this query had no issue:
* $payload = '{ user @include(if:false) { name } }';
*/
$payload = 'query { user @include(if:false) { name } }';
$parser = new Parser();
$data = $parser->parse($payload);
$this->assertEquals([
'queries' => [
new Query(
'user',
null,
[],
[
new Field('name', null, [], [], new Location(1, 35)),
],
[
new Directive(
'include',
[
new Argument('if', new Literal(false, new Location(1, 26)), new Location(1, 23)),
],
new Location(1, 15)
),
],
new Location(1, 9)
)
],
'mutations' => [],
'fragments' => [],
'fragmentReferences' => [],
'variables' => [],
'variableReferences' => [],
], $data);
}
}