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 OperationDefinitionASTBuilder extends AbstractASTBuilder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @inheritdoc |
14
|
|
|
*/ |
15
|
|
|
public function supportsBuilder(string $kind): bool |
16
|
|
|
{ |
17
|
|
|
return $kind === ASTKindEnum::OPERATION_DEFINITION; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritdoc |
22
|
|
|
*/ |
23
|
|
|
public function build(LexerInterface $lexer, array $params): ?array |
24
|
|
|
{ |
25
|
|
|
$start = $lexer->getToken(); |
26
|
|
|
|
27
|
|
|
if ($this->peek($lexer, TokenKindEnum::BRACE_L)) { |
28
|
|
|
return [ |
29
|
|
|
'kind' => NodeKindEnum::OPERATION_DEFINITION, |
30
|
|
|
'operation' => 'query', |
31
|
|
|
'name' => null, |
32
|
|
|
'variableDefinitions' => [], |
33
|
|
|
'directives' => [], |
34
|
|
|
'selectionSet' => $this->buildAST(ASTKindEnum::SELECTION_SET, $lexer), |
35
|
|
|
'loc' => $this->buildLocation($lexer, $start), |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$operation = $this->parseOperationType($lexer); |
40
|
|
|
|
41
|
|
|
if ($this->peek($lexer, TokenKindEnum::NAME)) { |
42
|
|
|
$name = $this->buildAST(ASTKindEnum::NAME, $lexer); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return [ |
46
|
|
|
'kind' => NodeKindEnum::OPERATION_DEFINITION, |
47
|
|
|
'operation' => $operation, |
48
|
|
|
'name' => $name ?? null, |
49
|
|
|
'variableDefinitions' => $this->buildAST(ASTKindEnum::VARIABLE_DEFINITION, $lexer), |
50
|
|
|
'directives' => $this->buildAST(ASTKindEnum::DIRECTIVES, $lexer, ['isConst' => false]), |
51
|
|
|
'selectionSet' => $this->buildAST(ASTKindEnum::SELECTION_SET, $lexer), |
52
|
|
|
'loc' => $this->buildLocation($lexer, $start), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param LexerInterface $lexer |
58
|
|
|
* @return string |
59
|
|
|
* @throws SyntaxErrorException |
60
|
|
|
*/ |
61
|
|
|
protected function parseOperationType(LexerInterface $lexer): string |
62
|
|
|
{ |
63
|
|
|
$token = $this->expect($lexer, TokenKindEnum::NAME); |
64
|
|
|
$value = $token->getValue(); |
65
|
|
|
|
66
|
|
|
if ($this->isOperation($value)) { |
67
|
|
|
return $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw $this->unexpected($lexer, $token); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $value |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
protected function isOperation(string $value): bool |
78
|
|
|
{ |
79
|
|
|
return \in_array($value, ['query', 'mutation', 'subscription'], true); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|