Passed
Pull Request — master (#355)
by Hugo
04:14
created

validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL;
4
5
use Digia\GraphQL\Error\GraphQLException;
6
use Digia\GraphQL\Error\Handler\CallableMiddleware;
7
use Digia\GraphQL\Error\Handler\ErrorHandler;
8
use Digia\GraphQL\Error\Handler\ErrorHandlerInterface;
9
use Digia\GraphQL\Error\InvariantException;
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\Node\TypeNodeInterface;
14
use Digia\GraphQL\Language\Node\ValueNodeInterface;
15
use Digia\GraphQL\Language\Source;
16
use Digia\GraphQL\Language\StringSourceBuilder;
17
use Digia\GraphQL\Language\SyntaxErrorException;
18
use Digia\GraphQL\Schema\Resolver\ResolverRegistryInterface;
19
use Digia\GraphQL\Schema\Schema;
20
use Digia\GraphQL\Schema\Validation\SchemaValidationException;
21
use Digia\GraphQL\Validation\ValidationException;
22
use React\Promise\PromiseInterface;
23
24
/**
25
 * @param string|Source                   $source
26
 * @param array|ResolverRegistryInterface $resolverRegistry
27
 * @param array                           $options
28
 * @return Schema
29
 * @throws InvariantException
30
 */
31
function buildSchema($source, $resolverRegistry = [], array $options = []): Schema
32
{
33
    if (\is_string($source)) {
34
        $source = (new StringSourceBuilder($source))->build();
35
    }
36
37
    return GraphQL::buildSchema($source, $resolverRegistry, $options);
38
}
39
40
/**
41
 * @param Schema                          $schema
42
 * @param string|Source                   $source
43
 * @param array|ResolverRegistryInterface $resolverRegistry
44
 * @param array                           $options
45
 * @return Schema
46
 * @throws InvariantException
47
 */
48
function extendSchema(Schema $schema, $source, $resolverRegistry = [], array $options = []): Schema
49
{
50
    if (\is_string($source)) {
51
        $source = (new StringSourceBuilder($source))->build();
52
    }
53
54
    return GraphQL::extendSchema($schema, $source, $resolverRegistry, $options);
55
}
56
57
/**
58
 * @param Schema $schema
59
 * @return SchemaValidationException[]
60
 */
61
function validateSchema(Schema $schema): array
62
{
63
    return GraphQL::validateSchema($schema);
64
}
65
66
/**
67
 * @param string|Source $source
68
 * @param array         $options
69
 * @return DocumentNode
70
 * @throws InvariantException
71
 * @throws SyntaxErrorException
72
 */
73
function parse($source, array $options = []): DocumentNode
74
{
75
    if (\is_string($source)) {
76
        $source = (new StringSourceBuilder($source))->build();
77
    }
78
79
    return GraphQL::parse($source, $options);
80
}
81
82
/**
83
 * @param string|Source $source
84
 * @param array         $options
85
 * @return ValueNodeInterface
86
 * @throws InvariantException
87
 * @throws SyntaxErrorException
88
 */
89
function parseValue($source, array $options = []): ValueNodeInterface
90
{
91
    if (\is_string($source)) {
92
        $source = (new StringSourceBuilder($source))->build();
93
    }
94
95
    return GraphQL::parseValue($source, $options);
96
}
97
98
/**
99
 * @param string|Source $source
100
 * @param array         $options
101
 * @return TypeNodeInterface
102
 * @throws InvariantException
103
 * @throws SyntaxErrorException
104
 */
105
function parseType($source, array $options = []): TypeNodeInterface
106
{
107
    if (\is_string($source)) {
108
        $source = (new StringSourceBuilder($source))->build();
109
    }
110
111
    return GraphQL::parseType($source, $options);
112
}
113
114
/**
115
 * @param Schema       $schema
116
 * @param DocumentNode $document
117
 * @return array|ValidationException[]
118
 */
119
function validate(Schema $schema, DocumentNode $document): array
120
{
121
    return GraphQL::validate($schema, $document);
122
}
123
124
/**
125
 * @param Schema                     $schema
126
 * @param DocumentNode               $document
127
 * @param mixed|null                 $rootValue
128
 * @param mixed|null                 $contextValue
129
 * @param array                      $variableValues
130
 * @param mixed|null                 $operationName
131
 * @param callable|null              $fieldResolver
132
 * @param ErrorHandlerInterface|null $errorHandler
133
 * @return ExecutionResult
134
 */
135
function execute(
136
    Schema $schema,
137
    DocumentNode $document,
138
    $rootValue = null,
139
    $contextValue = null,
140
    array $variableValues = [],
141
    $operationName = null,
142
    callable $fieldResolver = null,
143
    $errorHandler = null
144
): ExecutionResult {
145
    $resultPromise = GraphQL::execute(
146
        $schema,
147
        $document,
148
        $rootValue,
149
        $contextValue,
150
        $variableValues,
151
        $operationName,
152
        $fieldResolver,
153
        $errorHandler
154
    );
155
156
    $data = null;
157
158
    $resultPromise->then(function (ExecutionResult $result) use (&$data) {
159
        $data = $result;
160
    });
161
162
    if (null === $data) {
163
        $data = new ExecutionResult(null, [
164
            new GraphQLException('Looks like you are using Event Loop. Please use `executeAsync` method instead.')
165
        ]);
166
    }
167
168
    return $data;
169
}
170
171
/**
172
 * @param Schema                     $schema
173
 * @param DocumentNode               $document
174
 * @param mixed|null                 $rootValue
175
 * @param mixed|null                 $contextValue
176
 * @param array                      $variableValues
177
 * @param mixed|null                 $operationName
178
 * @param callable|null              $fieldResolver
179
 * @param ErrorHandlerInterface|null $errorHandler
180
 * @return PromiseInterface
181
 */
182
function executeAsync(
183
    Schema $schema,
184
    DocumentNode $document,
185
    $rootValue = null,
186
    $contextValue = null,
187
    array $variableValues = [],
188
    $operationName = null,
189
    callable $fieldResolver = null,
190
    $errorHandler = null
191
): PromiseInterface {
192
    return GraphQL::execute(
193
        $schema,
194
        $document,
195
        $rootValue,
196
        $contextValue,
197
        $variableValues,
198
        $operationName,
199
        $fieldResolver,
200
        $errorHandler
201
    );
202
}
203
204
/**
205
 * @param NodeInterface $node
206
 * @return string
207
 */
208
function printNode(NodeInterface $node): string
209
{
210
    return GraphQL::print($node);
211
}
212
213
/**
214
 * @param Schema                              $schema
215
 * @param string                              $source
216
 * @param mixed                               $rootValue
217
 * @param mixed                               $contextValue
218
 * @param array                               $variableValues
219
 * @param string|null                         $operationName
220
 * @param callable|null                       $fieldResolver
221
 * @param ErrorHandlerInterface|callable|null $errorHandler
222
 * @return array
223
 * @throws InvariantException
224
 * @throws SyntaxErrorException
225
 */
226
function graphql(
227
    Schema $schema,
228
    string $source,
229
    $rootValue = null,
230
    $contextValue = null,
231
    array $variableValues = [],
232
    ?string $operationName = null,
233
    ?callable $fieldResolver = null,
234
    $errorHandler = null
235
): array {
236
    if (\is_callable($errorHandler)) {
237
        $errorHandler = new ErrorHandler([new CallableMiddleware($errorHandler)]);
0 ignored issues
show
Bug introduced by
It seems like $errorHandler can also be of type Digia\GraphQL\Error\Handler\ErrorHandlerInterface and null; however, parameter $handleCallback of Digia\GraphQL\Error\Hand...ddleware::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
        $errorHandler = new ErrorHandler([new CallableMiddleware(/** @scrutinizer ignore-type */ $errorHandler)]);
Loading history...
238
    }
239
240
    $resultPromise = GraphQL::process(
241
        $schema,
242
        $source,
243
        $rootValue,
244
        $contextValue,
245
        $variableValues,
246
        $operationName,
247
        $fieldResolver,
248
        $errorHandler
0 ignored issues
show
Bug introduced by
It seems like $errorHandler can also be of type callable; however, parameter $errorHandler of Digia\GraphQL\GraphQL::process() does only seem to accept Digia\GraphQL\Error\Hand...orHandlerInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

248
        /** @scrutinizer ignore-type */ $errorHandler
Loading history...
249
    );
250
251
    $data = null;
252
253
    $resultPromise->then(function (ExecutionResult $result) use (&$data) {
254
        $data = $result;
255
    });
256
257
    if (null === $data) {
258
        $data = new ExecutionResult(null, [
259
            new GraphQLException('Looks like you are using Event Loop. Please use `graphqlAsync` method instead.')
260
        ]);
261
    }
262
263
    return $data->toArray();
264
}
265
266
/**
267
 * @param Schema                              $schema
268
 * @param string                              $source
269
 * @param mixed                               $rootValue
270
 * @param mixed                               $contextValue
271
 * @param array                               $variableValues
272
 * @param string|null                         $operationName
273
 * @param callable|null                       $fieldResolver
274
 * @param ErrorHandlerInterface|callable|null $errorHandler
275
 * @return PromiseInterface
276
 * @throws InvariantException
277
 * @throws SyntaxErrorException
278
 */
279
function graphqlAsync(
280
    Schema $schema,
281
    string $source,
282
    $rootValue = null,
283
    $contextValue = null,
284
    array $variableValues = [],
285
    ?string $operationName = null,
286
    ?callable $fieldResolver = null,
287
    $errorHandler = null
288
): PromiseInterface {
289
    if (\is_callable($errorHandler)) {
290
        $errorHandler = new ErrorHandler([new CallableMiddleware($errorHandler)]);
0 ignored issues
show
Bug introduced by
It seems like $errorHandler can also be of type Digia\GraphQL\Error\Handler\ErrorHandlerInterface and null; however, parameter $handleCallback of Digia\GraphQL\Error\Hand...ddleware::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

290
        $errorHandler = new ErrorHandler([new CallableMiddleware(/** @scrutinizer ignore-type */ $errorHandler)]);
Loading history...
291
    }
292
293
    $resultPromise = GraphQL::process(
294
        $schema,
295
        $source,
296
        $rootValue,
297
        $contextValue,
298
        $variableValues,
299
        $operationName,
300
        $fieldResolver,
301
        $errorHandler
0 ignored issues
show
Bug introduced by
It seems like $errorHandler can also be of type callable; however, parameter $errorHandler of Digia\GraphQL\GraphQL::process() does only seem to accept Digia\GraphQL\Error\Hand...orHandlerInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

301
        /** @scrutinizer ignore-type */ $errorHandler
Loading history...
302
    );
303
304
    return $resultPromise->then(function (ExecutionResult $result) {
305
        return $result->toArray();
306
    });
307
}
308