1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\InvariantException; |
6
|
|
|
use Digia\GraphQL\Error\SyntaxErrorException; |
7
|
|
|
use Digia\GraphQL\Error\ValidationException; |
8
|
|
|
use Digia\GraphQL\Execution\ExecutionInterface; |
9
|
|
|
use Digia\GraphQL\Execution\ExecutionResult; |
10
|
|
|
use Digia\GraphQL\Language\LexerInterface; |
11
|
|
|
use Digia\GraphQL\Language\Node\DocumentNode; |
12
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
13
|
|
|
use Digia\GraphQL\Language\ParserInterface; |
14
|
|
|
use Digia\GraphQL\Language\PrinterInterface; |
15
|
|
|
use Digia\GraphQL\Language\SchemaBuilder\SchemaBuilderInterface; |
16
|
|
|
use Digia\GraphQL\Language\Source; |
17
|
|
|
use Digia\GraphQL\Type\SchemaInterface; |
18
|
|
|
use Digia\GraphQL\Util\SerializationInterface; |
19
|
|
|
use Digia\GraphQL\Validation\ValidatorInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string|Source $source |
23
|
|
|
* @param array $options |
24
|
|
|
* @return NodeInterface|DocumentNode|SerializationInterface |
25
|
|
|
* @throws InvariantException |
26
|
|
|
* @throws SyntaxErrorException |
27
|
|
|
*/ |
28
|
|
|
function parse($source, array $options = []): NodeInterface |
29
|
|
|
{ |
30
|
|
|
return GraphQL::get(ParserInterface::class)->parse( |
31
|
|
|
GraphQL::get(LexerInterface::class) |
32
|
|
|
->setSource($source instanceof Source ? $source : new Source($source)) |
33
|
|
|
->setOptions($options) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string|Source $source |
39
|
|
|
* @param array $options |
40
|
|
|
* @return NodeInterface|SerializationInterface |
41
|
|
|
* @throws InvariantException |
42
|
|
|
* @throws SyntaxErrorException |
43
|
|
|
*/ |
44
|
|
|
function parseValue($source, array $options = []): NodeInterface |
45
|
|
|
{ |
46
|
|
|
return GraphQL::get(ParserInterface::class)->parseValue( |
47
|
|
|
GraphQL::get(LexerInterface::class) |
48
|
|
|
->setSource($source instanceof Source ? $source : new Source($source)) |
49
|
|
|
->setOptions($options) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|Source $source |
55
|
|
|
* @param array $options |
56
|
|
|
* @return NodeInterface|SerializationInterface |
57
|
|
|
* @throws InvariantException |
58
|
|
|
* @throws SyntaxErrorException |
59
|
|
|
*/ |
60
|
|
|
function parseType($source, array $options = []): NodeInterface |
61
|
|
|
{ |
62
|
|
|
return GraphQL::get(ParserInterface::class)->parseType( |
63
|
|
|
GraphQL::get(LexerInterface::class) |
64
|
|
|
->setSource($source instanceof Source ? $source : new Source($source)) |
65
|
|
|
->setOptions($options) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $source |
71
|
|
|
* @param array $options |
72
|
|
|
* @return SchemaInterface |
73
|
|
|
* @throws InvariantException |
74
|
|
|
*/ |
75
|
|
|
function buildSchema(string $source, array $options = []): SchemaInterface |
76
|
|
|
{ |
77
|
|
|
return GraphQL::get(SchemaBuilderInterface::class)->build(parse($source, $options)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param SchemaInterface $schema |
82
|
|
|
* @param DocumentNode $document |
83
|
|
|
* @return array|ValidationException[] |
84
|
|
|
*/ |
85
|
|
|
function validate(SchemaInterface $schema, DocumentNode $document): array |
86
|
|
|
{ |
87
|
|
|
return GraphQL::get(ValidatorInterface::class)->validate($schema, $document); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param SchemaInterface $schema |
92
|
|
|
* @param DocumentNode $document |
93
|
|
|
* @param mixed|null $rootValue |
94
|
|
|
* @param mixed|null $contextValue |
95
|
|
|
* @param array $variableValues |
96
|
|
|
* @param mixed|null $operationName |
97
|
|
|
* @param callable|null $fieldResolver |
98
|
|
|
* @return ExecutionResult |
99
|
|
|
*/ |
100
|
|
|
function execute( |
101
|
|
|
SchemaInterface $schema, |
102
|
|
|
DocumentNode $document, |
103
|
|
|
$rootValue = null, |
104
|
|
|
$contextValue = null, |
105
|
|
|
array $variableValues = [], |
106
|
|
|
$operationName = null, |
107
|
|
|
callable $fieldResolver = null |
108
|
|
|
): ExecutionResult { |
109
|
|
|
return GraphQL::get(ExecutionInterface::class) |
110
|
|
|
->execute( |
111
|
|
|
$schema, |
112
|
|
|
$document, |
113
|
|
|
$rootValue, |
114
|
|
|
$contextValue, |
115
|
|
|
$variableValues, |
116
|
|
|
$operationName, |
117
|
|
|
$fieldResolver |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param NodeInterface $node |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
function printNode(NodeInterface $node): string |
126
|
|
|
{ |
127
|
|
|
return GraphQL::get(PrinterInterface::class)->print($node); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param SchemaInterface $schema |
132
|
|
|
* @param string $source |
133
|
|
|
* @param mixed|null $rootValue |
134
|
|
|
* @param mixed|null $contextValue |
135
|
|
|
* @param array $variableValues |
136
|
|
|
* @param mixed|null $operationName |
137
|
|
|
* @param callable|null $fieldResolver |
138
|
|
|
* @return ExecutionResult |
139
|
|
|
* @throws InvariantException |
140
|
|
|
*/ |
141
|
|
|
function graphql( |
142
|
|
|
SchemaInterface $schema, |
143
|
|
|
string $source, |
144
|
|
|
$rootValue = null, |
145
|
|
|
$contextValue = null, |
146
|
|
|
array $variableValues = [], |
147
|
|
|
$operationName = null, |
148
|
|
|
callable $fieldResolver = null |
149
|
|
|
): ExecutionResult { |
150
|
|
|
// TODO: Validate schema |
151
|
|
|
|
152
|
|
|
$document = null; |
|
|
|
|
153
|
|
|
|
154
|
|
|
try { |
155
|
|
|
$document = parse($source); |
156
|
|
|
} catch (SyntaxErrorException $error) { |
157
|
|
|
return new ExecutionResult([], [$error]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$validationErrors = validate($schema, $document); |
161
|
|
|
if (!empty($validationErrors)) { |
162
|
|
|
return new ExecutionResult([], $validationErrors); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return execute( |
166
|
|
|
$schema, |
167
|
|
|
$document, |
168
|
|
|
$rootValue, |
169
|
|
|
$contextValue, |
170
|
|
|
$variableValues, |
171
|
|
|
$operationName, |
172
|
|
|
$fieldResolver |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|