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