mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-06 07:49:24 +03:00
Schema Parsing: allow leading pipe for union type definitions
This commit is contained in:
parent
30632050a5
commit
a79a51d445
@ -1080,14 +1080,17 @@ class Parser
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* UnionMembers :
|
* UnionMembers :
|
||||||
* - NamedType
|
* - `|`? NamedType
|
||||||
* - UnionMembers | NamedType
|
* - UnionMembers | NamedType
|
||||||
*
|
*
|
||||||
* @return NamedTypeNode[]
|
* @return NamedTypeNode[]
|
||||||
*/
|
*/
|
||||||
function parseUnionMembers()
|
function parseUnionMembers()
|
||||||
{
|
{
|
||||||
|
// Optional leading pipe
|
||||||
|
$this->skip(Token::PIPE);
|
||||||
$members = [];
|
$members = [];
|
||||||
|
|
||||||
do {
|
do {
|
||||||
$members[] = $this->parseNamedType();
|
$members[] = $this->parseNamedType();
|
||||||
} while ($this->skip(Token::PIPE));
|
} while ($this->skip(Token::PIPE));
|
||||||
@ -1213,6 +1216,8 @@ class Parser
|
|||||||
*/
|
*/
|
||||||
function parseDirectiveLocations()
|
function parseDirectiveLocations()
|
||||||
{
|
{
|
||||||
|
// Optional leading pipe
|
||||||
|
$this->skip(Token::PIPE);
|
||||||
$locations = [];
|
$locations = [];
|
||||||
do {
|
do {
|
||||||
$locations[] = $this->parseName();
|
$locations[] = $this->parseName();
|
||||||
|
@ -1,27 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Tests\Language;
|
namespace GraphQL\Tests\Language;
|
||||||
|
|
||||||
use GraphQL\Language\AST\BooleanValueNode;
|
use GraphQL\Error\SyntaxError;
|
||||||
use GraphQL\Language\AST\DocumentNode;
|
|
||||||
use GraphQL\Language\AST\EnumTypeDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\EnumValueDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\FieldDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\InputValueDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\ListTypeNode;
|
|
||||||
use GraphQL\Language\AST\Location;
|
|
||||||
use GraphQL\Language\AST\NameNode;
|
|
||||||
use GraphQL\Language\AST\NamedTypeNode;
|
|
||||||
use GraphQL\Language\AST\Node;
|
|
||||||
use GraphQL\Language\AST\NodeKind;
|
use GraphQL\Language\AST\NodeKind;
|
||||||
use GraphQL\Language\AST\NonNullTypeNode;
|
|
||||||
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\TypeExtensionDefinitionNode;
|
|
||||||
use GraphQL\Language\AST\UnionTypeDefinitionNode;
|
|
||||||
use GraphQL\Language\Parser;
|
use GraphQL\Language\Parser;
|
||||||
use GraphQL\Language\Source;
|
|
||||||
|
|
||||||
class SchemaParserTest extends \PHPUnit_Framework_TestCase
|
class SchemaParserTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
@ -541,6 +523,86 @@ type Hello {
|
|||||||
$this->assertEquals($expected, TestUtils::nodeToArray($doc));
|
$this->assertEquals($expected, TestUtils::nodeToArray($doc));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @it Union with two types and leading pipe
|
||||||
|
*/
|
||||||
|
public function testUnionWithTwoTypesAndLeadingPipe()
|
||||||
|
{
|
||||||
|
$body = 'union Hello = | Wo | Rld';
|
||||||
|
$doc = Parser::parse($body);
|
||||||
|
$expected = [
|
||||||
|
'kind' => 'Document',
|
||||||
|
'definitions' => [
|
||||||
|
[
|
||||||
|
'kind' => 'UnionTypeDefinition',
|
||||||
|
'name' => $this->nameNode('Hello', ['start' => 6, 'end' => 11]),
|
||||||
|
'directives' => [],
|
||||||
|
'types' => [
|
||||||
|
$this->typeNode('Wo', ['start' => 16, 'end' => 18]),
|
||||||
|
$this->typeNode('Rld', ['start' => 21, 'end' => 24]),
|
||||||
|
],
|
||||||
|
'loc' => ['start' => 0, 'end' => 24],
|
||||||
|
'description' => null
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'loc' => ['start' => 0, 'end' => 24],
|
||||||
|
];
|
||||||
|
$this->assertEquals($expected, TestUtils::nodeToArray($doc));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @it Union fails with no types
|
||||||
|
*/
|
||||||
|
public function testUnionFailsWithNoTypes()
|
||||||
|
{
|
||||||
|
$body = 'union Hello = |';
|
||||||
|
try {
|
||||||
|
Parser::parse($body);
|
||||||
|
} catch (SyntaxError $e) {
|
||||||
|
$this->assertContains('Syntax Error GraphQL (1:16) Expected Name, found <EOF>', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @it Union fails with leading douple pipe
|
||||||
|
*/
|
||||||
|
public function testUnionFailsWithLeadingDoublePipe()
|
||||||
|
{
|
||||||
|
$body = 'union Hello = || Wo | Rld';
|
||||||
|
try {
|
||||||
|
Parser::parse($body);
|
||||||
|
} catch (SyntaxError $e) {
|
||||||
|
$this->assertContains('Syntax Error GraphQL (1:16) Expected Name, found |', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @it Union fails with double pipe
|
||||||
|
*/
|
||||||
|
public function testUnionFailsWithDoublePipe()
|
||||||
|
{
|
||||||
|
$body = 'union Hello = Wo || Rld';
|
||||||
|
try {
|
||||||
|
Parser::parse($body);
|
||||||
|
} catch (SyntaxError $e) {
|
||||||
|
$this->assertContains('Syntax Error GraphQL (1:19) Expected Name, found |', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @it Union fails with trailing pipe
|
||||||
|
*/
|
||||||
|
public function testUnionFailsWithTrailingPipe()
|
||||||
|
{
|
||||||
|
$body = 'union Hello = | Wo | Rld |';
|
||||||
|
try {
|
||||||
|
Parser::parse($body);
|
||||||
|
} catch (SyntaxError $e) {
|
||||||
|
$this->assertContains('Syntax Error GraphQL (1:27) Expected Name, found <EOF>', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @it Scalar
|
* @it Scalar
|
||||||
*/
|
*/
|
||||||
|
@ -83,6 +83,8 @@ union Feed = Story | Article | Advert
|
|||||||
|
|
||||||
union AnnotatedUnion @onUnion = A | B
|
union AnnotatedUnion @onUnion = A | B
|
||||||
|
|
||||||
|
union AnnotatedUnionTwo @onUnion = A | B
|
||||||
|
|
||||||
scalar CustomScalar
|
scalar CustomScalar
|
||||||
|
|
||||||
scalar AnnotatedScalar @onScalar
|
scalar AnnotatedScalar @onScalar
|
||||||
@ -117,6 +119,8 @@ type NoFields {}
|
|||||||
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
|
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
|
||||||
|
|
||||||
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
|
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
|
||||||
|
|
||||||
|
directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
|
||||||
';
|
';
|
||||||
$this->assertEquals($expected, $printed);
|
$this->assertEquals($expected, $printed);
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,8 @@ union Feed = Story | Article | Advert
|
|||||||
|
|
||||||
union AnnotatedUnion @onUnion = A | B
|
union AnnotatedUnion @onUnion = A | B
|
||||||
|
|
||||||
|
union AnnotatedUnionTwo @onUnion = | A | B
|
||||||
|
|
||||||
scalar CustomScalar
|
scalar CustomScalar
|
||||||
|
|
||||||
scalar AnnotatedScalar @onScalar
|
scalar AnnotatedScalar @onScalar
|
||||||
@ -74,3 +76,8 @@ directive @include(if: Boolean!)
|
|||||||
on FIELD
|
on FIELD
|
||||||
| FRAGMENT_SPREAD
|
| FRAGMENT_SPREAD
|
||||||
| INLINE_FRAGMENT
|
| INLINE_FRAGMENT
|
||||||
|
|
||||||
|
directive @include2(if: Boolean!) on
|
||||||
|
| FIELD
|
||||||
|
| FRAGMENT_SPREAD
|
||||||
|
| INLINE_FRAGMENT
|
||||||
|
Loading…
x
Reference in New Issue
Block a user