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\Execution; |
9
|
|
|
use Digia\GraphQL\Execution\ExecutionInterface; |
10
|
|
|
use Digia\GraphQL\Execution\ExecutionResult; |
11
|
|
|
use Digia\GraphQL\Language\Node\DocumentNode; |
12
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
13
|
|
|
use Digia\GraphQL\Language\SchemaBuilder\SchemaBuilderInterface; |
14
|
|
|
use Digia\GraphQL\Language\LexerInterface; |
15
|
|
|
use Digia\GraphQL\Language\ParserInterface; |
16
|
|
|
use Digia\GraphQL\Language\PrinterInterface; |
17
|
|
|
use Digia\GraphQL\Language\Source; |
18
|
|
|
use Digia\GraphQL\Type\SchemaInterface; |
19
|
|
|
use Digia\GraphQL\Util\SerializationInterface; |
20
|
|
|
use Digia\GraphQL\Validation\ValidatorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string|Source $source |
24
|
|
|
* @param array $options |
25
|
|
|
* @return NodeInterface|DocumentNode|SerializationInterface |
26
|
|
|
* @throws InvariantException |
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
|
|
|
*/ |
43
|
|
|
function parseValue($source, array $options = []): NodeInterface |
44
|
|
|
{ |
45
|
|
|
return GraphQL::get(ParserInterface::class)->parseValue( |
46
|
|
|
GraphQL::get(LexerInterface::class) |
47
|
|
|
->setSource($source instanceof Source ? $source : new Source($source)) |
48
|
|
|
->setOptions($options) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string|Source $source |
54
|
|
|
* @param array $options |
55
|
|
|
* @return NodeInterface|SerializationInterface |
56
|
|
|
* @throws InvariantException |
57
|
|
|
*/ |
58
|
|
|
function parseType($source, array $options = []): NodeInterface |
59
|
|
|
{ |
60
|
|
|
return GraphQL::get(ParserInterface::class)->parseType( |
61
|
|
|
GraphQL::get(LexerInterface::class) |
62
|
|
|
->setSource($source instanceof Source ? $source : new Source($source)) |
63
|
|
|
->setOptions($options) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $source |
69
|
|
|
* @param array $options |
70
|
|
|
* @return SchemaInterface |
71
|
|
|
* @throws InvariantException |
72
|
|
|
*/ |
73
|
|
|
function buildSchema(string $source, array $options = []): SchemaInterface |
74
|
|
|
{ |
75
|
|
|
return GraphQL::get(SchemaBuilderInterface::class)->build(parse($source, $options)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param SchemaInterface $schema |
80
|
|
|
* @param DocumentNode $document |
81
|
|
|
* @return array|ValidationException[] |
82
|
|
|
*/ |
83
|
|
|
function validate(SchemaInterface $schema, DocumentNode $document): array |
84
|
|
|
{ |
85
|
|
|
return GraphQL::get(ValidatorInterface::class)->validate($schema, $document); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param NodeInterface $node |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
function printNode(NodeInterface $node): string |
93
|
|
|
{ |
94
|
|
|
return GraphQL::get(PrinterInterface::class)->print($node); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param SchemaInterface $schema |
99
|
|
|
* @param string $source |
100
|
|
|
* @param null $rootValue |
|
|
|
|
101
|
|
|
* @param null $contextValue |
102
|
|
|
* @param null $variableValues |
103
|
|
|
* @param null $operationName |
104
|
|
|
* @param callable|null $fieldResolver |
105
|
|
|
* @return ExecutionResult |
106
|
|
|
* @throws InvariantException |
107
|
|
|
*/ |
108
|
|
|
function graphql( |
109
|
|
|
SchemaInterface $schema, |
110
|
|
|
string $source, |
111
|
|
|
$rootValue = null, |
112
|
|
|
$contextValue = null, |
113
|
|
|
$variableValues = [], |
114
|
|
|
$operationName = null, |
115
|
|
|
callable $fieldResolver = null |
116
|
|
|
): ExecutionResult { |
117
|
|
|
// TODO: Validate schema |
118
|
|
|
|
119
|
|
|
$document = null; |
|
|
|
|
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
$document = parse($source); |
123
|
|
|
} catch (SyntaxErrorException $error) { |
124
|
|
|
return new ExecutionResult([], [$error]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$validationErrors = validate($schema, $document); |
128
|
|
|
if (!empty($validationErrors)) { |
129
|
|
|
return new ExecutionResult([], $validationErrors); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** @noinspection PhpParamsInspection */ |
133
|
|
|
return GraphQL::get(ExecutionInterface::class) |
134
|
|
|
->execute( |
135
|
|
|
$schema, |
136
|
|
|
parse($source), |
137
|
|
|
$rootValue, |
138
|
|
|
$contextValue, |
139
|
|
|
$variableValues, |
140
|
|
|
$operationName, |
141
|
|
|
$fieldResolver |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|