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
|
|
|
use Digia\GraphQL\Language\TokenKindEnum; |
10
|
|
|
|
11
|
|
|
class DocumentASTBuilder extends AbstractASTBuilder |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param LexerInterface $lexer |
15
|
|
|
* @return bool |
16
|
|
|
*/ |
17
|
|
|
public function supportsBuilder(string $kind): bool |
18
|
|
|
{ |
19
|
|
|
return $kind === ASTKindEnum::DOCUMENT; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
* @throws SyntaxErrorException |
25
|
|
|
*/ |
26
|
|
|
public function build(LexerInterface $lexer, array $params): ?array |
27
|
|
|
{ |
28
|
|
|
$start = $lexer->getToken(); |
29
|
|
|
|
30
|
|
|
$this->expect($lexer, TokenKindEnum::SOF); |
31
|
|
|
|
32
|
|
|
$definitions = []; |
33
|
|
|
|
34
|
|
|
do { |
35
|
|
|
$definitions[] = $this->parseDefinition($lexer); |
36
|
|
|
} while (!$this->skip($lexer, TokenKindEnum::EOF)); |
37
|
|
|
|
38
|
|
|
return [ |
39
|
|
|
'kind' => NodeKindEnum::DOCUMENT, |
40
|
|
|
'definitions' => $definitions, |
41
|
|
|
'loc' => $this->buildLocation($lexer, $start), |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param LexerInterface $lexer |
47
|
|
|
* @return array |
48
|
|
|
* @throws SyntaxErrorException |
49
|
|
|
* @throws \ReflectionException |
50
|
|
|
*/ |
51
|
|
|
protected function parseDefinition(LexerInterface $lexer): array |
52
|
|
|
{ |
53
|
|
|
if ($this->peek($lexer, TokenKindEnum::NAME)) { |
54
|
|
|
switch ($lexer->getTokenValue()) { |
55
|
|
|
case KeywordEnum::QUERY: |
56
|
|
|
case KeywordEnum::MUTATION: |
57
|
|
|
case KeywordEnum::SUBSCRIPTION: |
58
|
|
|
case KeywordEnum::FRAGMENT: |
59
|
|
|
return $this->buildAST(ASTKindEnum::EXECUTABLE_DEFINITION, $lexer); |
|
|
|
|
60
|
|
|
case KeywordEnum::SCHEMA: |
61
|
|
|
case KeywordEnum::SCALAR: |
62
|
|
|
case KeywordEnum::TYPE: |
63
|
|
|
case KeywordEnum::INTERFACE: |
64
|
|
|
case KeywordEnum::UNION: |
65
|
|
|
case KeywordEnum::ENUM: |
66
|
|
|
case KeywordEnum::INPUT: |
67
|
|
|
case KeywordEnum::EXTEND: |
68
|
|
|
case KeywordEnum::DIRECTIVE: |
69
|
|
|
// Note: The schema definition language is an experimental addition. |
70
|
|
|
return $this->buildAST(ASTKindEnum::TYPE_SYSTEM_DEFINITION, $lexer); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} elseif ($this->peek($lexer, TokenKindEnum::BRACE_L)) { |
73
|
|
|
return $this->buildAST(ASTKindEnum::EXECUTABLE_DEFINITION, $lexer); |
|
|
|
|
74
|
|
|
} elseif ($this->peekDescription($lexer)) { |
75
|
|
|
// Note: The schema definition language is an experimental addition. |
76
|
|
|
return $this->buildAST(ASTKindEnum::TYPE_SYSTEM_DEFINITION, $lexer); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
throw $this->unexpected($lexer); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|