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

Support repeatable directives #643

Merged
merged 9 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
23 changes: 4 additions & 19 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,6 @@ parameters:
count: 2
path: src/Server/OperationParams.php

-
message: "#^Variable property access on \\$this\\(GraphQL\\\\Type\\\\Definition\\\\Directive\\)\\.$#"
count: 1
path: src/Type/Definition/Directive.php

-
message: "#^Only booleans are allowed in a negated boolean, ArrayObject\\<string, GraphQL\\\\Type\\\\Definition\\\\EnumValueDefinition\\> given\\.$#"
count: 1
Expand Down Expand Up @@ -890,16 +885,6 @@ parameters:
count: 1
path: src/Utils/SchemaExtender.php

-
message: "#^Anonymous function should have native return typehint \"bool\"\\.$#"
count: 3
path: src/Utils/SchemaPrinter.php

-
message: "#^Anonymous function should have native return typehint \"string\"\\.$#"
count: 7
path: src/Utils/SchemaPrinter.php

-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 2
Expand Down Expand Up @@ -1496,22 +1481,22 @@ parameters:
path: src/Validator/Rules/ValuesOfCorrectType.php

-
message: "#^Only booleans are allowed in \\|\\|, GraphQL\\\\Type\\\\Definition\\\\EnumType\\|GraphQL\\\\Type\\\\Definition\\\\InputObjectType\\|GraphQL\\\\Type\\\\Definition\\\\ListOfType\\|GraphQL\\\\Type\\\\Definition\\\\NonNull\\|GraphQL\\\\Type\\\\Definition\\\\ScalarType given on the left side\\.$#"
message: "#^Anonymous function should have native return typehint \"string\"\\.$#"
count: 1
path: src/Validator/Rules/ValuesOfCorrectType.php

-
message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Type\\\\Definition\\\\EnumValueDefinition\\|null given\\.$#"
message: "#^Only booleans are allowed in \\|\\|, GraphQL\\\\Type\\\\Definition\\\\EnumType\\|GraphQL\\\\Type\\\\Definition\\\\InputObjectType\\|GraphQL\\\\Type\\\\Definition\\\\ListOfType\\|GraphQL\\\\Type\\\\Definition\\\\NonNull\\|GraphQL\\\\Type\\\\Definition\\\\ScalarType given on the left side\\.$#"
count: 1
path: src/Validator/Rules/ValuesOfCorrectType.php

-
message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Type\\\\Definition\\\\EnumType\\|GraphQL\\\\Type\\\\Definition\\\\InputObjectType\\|GraphQL\\\\Type\\\\Definition\\\\ListOfType\\|GraphQL\\\\Type\\\\Definition\\\\NonNull\\|GraphQL\\\\Type\\\\Definition\\\\ScalarType given\\.$#"
message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Type\\\\Definition\\\\EnumValueDefinition\\|null given\\.$#"
count: 1
path: src/Validator/Rules/ValuesOfCorrectType.php

-
message: "#^Anonymous function should have native return typehint \"string\"\\.$#"
message: "#^Only booleans are allowed in a negated boolean, GraphQL\\\\Type\\\\Definition\\\\EnumType\\|GraphQL\\\\Type\\\\Definition\\\\InputObjectType\\|GraphQL\\\\Type\\\\Definition\\\\ListOfType\\|GraphQL\\\\Type\\\\Definition\\\\NonNull\\|GraphQL\\\\Type\\\\Definition\\\\ScalarType given\\.$#"
count: 1
path: src/Validator/Rules/ValuesOfCorrectType.php

Expand Down
9 changes: 6 additions & 3 deletions src/Language/AST/DirectiveDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ class DirectiveDefinitionNode extends Node implements TypeSystemDefinitionNode
/** @var NameNode */
public $name;

/** @var StringValueNode|null */
public $description;

/** @var ArgumentNode[] */
public $arguments;

/** @var bool */
public $repeatable;

/** @var NameNode[] */
public $locations;

/** @var StringValueNode|null */
public $description;
}
57 changes: 33 additions & 24 deletions src/Language/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,26 +327,39 @@ private function expect(string $kind) : Token
}

/**
* If the next token is a keyword with the given value, return that token after
* advancing the parser. Otherwise, do not change the parser state and return
* false.
* If the next token is a keyword with the given value, advance the lexer.
* Otherwise, throw an error.
*
* @throws SyntaxError
*/
private function expectKeyword(string $value) : Token
private function expectKeyword(string $value) : void
{
$token = $this->lexer->token;
if ($token->kind !== Token::NAME || $token->value !== $value) {
throw new SyntaxError(
$this->lexer->source,
$token->start,
'Expected "' . $value . '", found ' . $token->getDescription()
);
}

$this->lexer->advance();
}

/**
* If the next token is a given keyword, return "true" after advancing
* the lexer. Otherwise, do not change the parser state and return "false".
*/
private function expectOptionalKeyword(string $value) : bool
{
$token = $this->lexer->token;
if ($token->kind === Token::NAME && $token->value === $value) {
$this->lexer->advance();

return $token;
return true;
}
throw new SyntaxError(
$this->lexer->source,
$token->start,
'Expected "' . $value . '", found ' . $token->getDescription()
);

return false;
}

private function unexpected(?Token $atToken = null) : SyntaxError
Expand Down Expand Up @@ -716,22 +729,17 @@ private function parseFragment() : SelectionNode
$start = $this->lexer->token;
$this->expect(Token::SPREAD);

if ($this->peek(Token::NAME) && $this->lexer->token->value !== 'on') {
$hasTypeCondition = $this->expectOptionalKeyword('on');
if (! $hasTypeCondition && $this->peek(Token::NAME)) {
return new FragmentSpreadNode([
'name' => $this->parseFragmentName(),
'directives' => $this->parseDirectives(false),
'loc' => $this->loc($start),
]);
}

$typeCondition = null;
if ($this->lexer->token->value === 'on') {
$this->lexer->advance();
$typeCondition = $this->parseNamedType();
}

return new InlineFragmentNode([
'typeCondition' => $typeCondition,
'typeCondition' => $hasTypeCondition ? $this->parseNamedType() : null,
'directives' => $this->parseDirectives(false),
'selectionSet' => $this->parseSelectionSet(),
'loc' => $this->loc($start),
Expand Down Expand Up @@ -1172,8 +1180,7 @@ private function parseObjectTypeDefinition() : ObjectTypeDefinitionNode
private function parseImplementsInterfaces() : array
{
$types = [];
if ($this->lexer->token->value === 'implements') {
$this->lexer->advance();
if ($this->expectOptionalKeyword('implements')) {
// Optional leading ampersand
$this->skip(Token::AMP);
do {
Expand Down Expand Up @@ -1668,7 +1675,7 @@ private function parseInputObjectTypeExtension() : InputObjectTypeExtensionNode

/**
* DirectiveDefinition :
* - directive @ Name ArgumentsDefinition? on DirectiveLocations
* - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
*
* @throws SyntaxError
*/
Expand All @@ -1678,17 +1685,19 @@ private function parseDirectiveDefinition() : DirectiveDefinitionNode
$description = $this->parseDescription();
$this->expectKeyword('directive');
$this->expect(Token::AT);
$name = $this->parseName();
$args = $this->parseArgumentsDefinition();
$name = $this->parseName();
$args = $this->parseArgumentsDefinition();
$repeatable = $this->expectOptionalKeyword('repeatable');
$this->expectKeyword('on');
$locations = $this->parseDirectiveLocations();

return new DirectiveDefinitionNode([
'name' => $name,
'description' => $description,
'arguments' => $args,
'repeatable' => $repeatable,
'locations' => $locations,
'loc' => $this->loc($start),
'description' => $description,
]);
}

Expand Down
1 change: 1 addition & 0 deletions src/Language/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ function (InterfaceTypeDefinitionNode $def) {
. ($noIndent
? $this->wrap('(', $this->join($def->arguments, ', '), ')')
: $this->wrap("(\n", $this->indent($this->join($def->arguments, "\n")), "\n"))
. ($def->repeatable ? ' repeatable' : '')
. ' on ' . $this->join($def->locations, ' | ');
}),
],
Expand Down
30 changes: 21 additions & 9 deletions src/Type/Definition/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace GraphQL\Type\Definition;

use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\DirectiveDefinitionNode;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Utils\Utils;
use function array_key_exists;
use function is_array;

Expand All @@ -31,12 +31,15 @@ class Directive
/** @var string|null */
public $description;

/** @var string[] */
public $locations;

/** @var FieldArgument[] */
public $args = [];

/** @var bool */
public $isRepeatable;

/** @var string[] */
public $locations;

/** @var DirectiveDefinitionNode|null */
public $astNode;

Expand All @@ -48,6 +51,13 @@ class Directive
*/
public function __construct(array $config)
{
if (! isset($config['name'])) {
throw new InvariantViolation('Directive must be named.');
}
$this->name = $config['name'];

$this->description = $config['description'] ?? null;

if (isset($config['args'])) {
$args = [];
foreach ($config['args'] as $name => $arg) {
Expand All @@ -58,14 +68,16 @@ public function __construct(array $config)
}
}
$this->args = $args;
unset($config['args']);
}
foreach ($config as $key => $value) {
$this->{$key} = $value;

if (! isset($config['locations']) || ! is_array($config['locations'])) {
throw new InvariantViolation('Must provide locations for directive.');
}
$this->locations = $config['locations'];

$this->isRepeatable = $config['isRepeatable'] ?? false;
$this->astNode = $config['astNode'] ?? null;

Utils::invariant($this->name, 'Directive must be named.');
Utils::invariant(is_array($this->locations), 'Must provide locations for directive.');
$this->config = $config;
}

Expand Down
Loading