1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language\ASTBuilder; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\SyntaxErrorException; |
6
|
|
|
use Digia\GraphQL\Language\LexerInterface; |
7
|
|
|
use Digia\GraphQL\Language\Node\NodeKindEnum; |
8
|
|
|
use Digia\GraphQL\Language\TokenKindEnum; |
9
|
|
|
|
10
|
|
|
class FragmentASTBuilder extends AbstractASTBuilder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @inheritdoc |
14
|
|
|
*/ |
15
|
|
|
public function supportsBuilder(string $kind): bool |
16
|
|
|
{ |
17
|
|
|
return $kind === ASTKindEnum::FRAGMENT; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritdoc |
22
|
|
|
*/ |
23
|
|
|
public function build(LexerInterface $lexer, array $params): ?array |
24
|
|
|
{ |
25
|
|
|
$start = $lexer->getToken(); |
26
|
|
|
|
27
|
|
|
$this->expect($lexer, TokenKindEnum::SPREAD); |
28
|
|
|
|
29
|
|
|
$tokenValue = $lexer->getTokenValue(); |
30
|
|
|
|
31
|
|
|
if ($tokenValue !== 'on' && $this->peek($lexer, TokenKindEnum::NAME)) { |
32
|
|
|
return [ |
33
|
|
|
'kind' => NodeKindEnum::FRAGMENT_SPREAD, |
34
|
|
|
'name' => $this->parseFragmentName($lexer), |
35
|
|
|
'directives' => $this->buildAST(ASTKindEnum::DIRECTIVES, $lexer), |
36
|
|
|
'loc' => $this->buildLocation($lexer, $start), |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($tokenValue === 'on') { |
41
|
|
|
$lexer->advance(); |
42
|
|
|
|
43
|
|
|
$typeCondition = $this->buildAST(ASTKindEnum::NAMED_TYPE, $lexer); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return [ |
47
|
|
|
'kind' => NodeKindEnum::INLINE_FRAGMENT, |
48
|
|
|
'typeCondition' => $typeCondition ?? null, |
49
|
|
|
'directives' => $this->buildAST(ASTKindEnum::DIRECTIVES, $lexer), |
50
|
|
|
'selectionSet' => $this->buildAST(ASTKindEnum::SELECTION_SET, $lexer), |
51
|
|
|
'loc' => $this->buildLocation($lexer, $start), |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param LexerInterface $lexer |
57
|
|
|
* @return array |
58
|
|
|
* @throws SyntaxErrorException |
59
|
|
|
*/ |
60
|
|
|
protected function parseFragmentName(LexerInterface $lexer): array |
61
|
|
|
{ |
62
|
|
|
if ($lexer->getTokenValue() === 'on') { |
63
|
|
|
throw $this->unexpected($lexer); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->buildAST(ASTKindEnum::NAME, $lexer); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|