Passed
Pull Request — master (#345)
by Sam
04:47 queued 02:01
created

graphqlAsync()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 8
dl 0
loc 27
rs 9.8333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
    $resultPromise->then(function (ExecutionResult $result) use (&$data) {
158
        $data = $result;
159
    });
160
    if ($data === null) {
161
        $data = new ExecutionResult(null, [
162
            new GraphQLException('It\'s looks like you are using Event Loop. Please use `executeAsync` method instead.')
163
        ]);
164
    }
165
    return $data;
166
}
167
168
/**
169
 * @param Schema                     $schema
170
 * @param DocumentNode               $document
171
 * @param mixed|null                 $rootValue
172
 * @param mixed|null                 $contextValue
173
 * @param array                      $variableValues
174
 * @param mixed|null                 $operationName
175
 * @param callable|null              $fieldResolver
176
 * @param ErrorHandlerInterface|null $errorHandler
177
 * @return PromiseInterface
178
 */
179
function executeAsync(
180
    Schema $schema,
181
    DocumentNode $document,
182
    $rootValue = null,
183
    $contextValue = null,
184
    array $variableValues = [],
185
    $operationName = null,
186
    callable $fieldResolver = null,
187
    $errorHandler = null
188
): PromiseInterface {
189
    return GraphQL::execute(
190
        $schema,
191
        $document,
192
        $rootValue,
193
        $contextValue,
194
        $variableValues,
195
        $operationName,
196
        $fieldResolver,
197
        $errorHandler
198
    );
199
}
200
201
/**
202
 * @param NodeInterface $node
203
 * @return string
204
 */
205
function printNode(NodeInterface $node): string
206
{
207
    return GraphQL::print($node);
208
}
209
210
/**
211
 * @param Schema                              $schema
212
 * @param string                              $source
213
 * @param mixed                               $rootValue
214
 * @param mixed                               $contextValue
215
 * @param array                               $variableValues
216
 * @param string|null                         $operationName
217
 * @param callable|null                       $fieldResolver
218
 * @param ErrorHandlerInterface|callable|null $errorHandler
219
 * @return array
220
 * @throws InvariantException
221
 * @throws SyntaxErrorException
222
 */
223
function graphql(
224
    Schema $schema,
225
    string $source,
226
    $rootValue = null,
227
    $contextValue = null,
228
    array $variableValues = [],
229
    ?string $operationName = null,
230
    ?callable $fieldResolver = null,
231
    $errorHandler = null
232
): array {
233
    if (\is_callable($errorHandler)) {
234
        $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

234
        $errorHandler = new ErrorHandler([new CallableMiddleware(/** @scrutinizer ignore-type */ $errorHandler)]);
Loading history...
235
    }
236
237
    $resultPromise = GraphQL::process(
238
        $schema,
239
        $source,
240
        $rootValue,
241
        $contextValue,
242
        $variableValues,
243
        $operationName,
244
        $fieldResolver,
245
        $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

245
        /** @scrutinizer ignore-type */ $errorHandler
Loading history...
246
    );
247
248
    $data = null;
249
    $resultPromise->then(function (ExecutionResult $result) use (&$data) {
250
        $data = $result;
251
    });
252
    if ($data === null) {
253
        $data = new ExecutionResult(null, [
254
            new GraphQLException('It\'s looks like you are using Event Loop. Please use `graphqlAsync` method instead.')
255
        ]);
256
    }
257
258
    return $data->toArray();
259
}
260
261
/**
262
 * @param Schema                              $schema
263
 * @param string                              $source
264
 * @param mixed                               $rootValue
265
 * @param mixed                               $contextValue
266
 * @param array                               $variableValues
267
 * @param string|null                         $operationName
268
 * @param callable|null                       $fieldResolver
269
 * @param ErrorHandlerInterface|callable|null $errorHandler
270
 * @return PromiseInterface
271
 * @throws InvariantException
272
 * @throws SyntaxErrorException
273
 */
274
function graphqlAsync(
275
    Schema $schema,
276
    string $source,
277
    $rootValue = null,
278
    $contextValue = null,
279
    array $variableValues = [],
280
    ?string $operationName = null,
281
    ?callable $fieldResolver = null,
282
    $errorHandler = null
283
): PromiseInterface {
284
    if (\is_callable($errorHandler)) {
285
        $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

285
        $errorHandler = new ErrorHandler([new CallableMiddleware(/** @scrutinizer ignore-type */ $errorHandler)]);
Loading history...
286
    }
287
288
    $resultPromise = GraphQL::process(
289
        $schema,
290
        $source,
291
        $rootValue,
292
        $contextValue,
293
        $variableValues,
294
        $operationName,
295
        $fieldResolver,
296
        $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

296
        /** @scrutinizer ignore-type */ $errorHandler
Loading history...
297
    );
298
299
    return $resultPromise->then(function (ExecutionResult $result) {
300
        return $result->toArray();
301
    });
302
}
303