Skip to content

Commit 995a174

Browse files
authored
Make args mapper optional
1 parent c3a36a2 commit 995a174

File tree

9 files changed

+52
-20
lines changed

9 files changed

+52
-20
lines changed

docs/class-reference.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This is the primary facade for fulfilling GraphQL operations.
44
See [related documentation](executing-queries.md).
55

6+
@phpstan-import-type ArgsMapper from Executor
67
@phpstan-import-type FieldResolver from Executor
78

89
@see \GraphQL\Tests\GraphQLTest
@@ -164,6 +165,17 @@ static function getStandardValidationRules(): array
164165
static function setDefaultFieldResolver(callable $fn): void
165166
```
166167

168+
```php
169+
/**
170+
* Set default args mapper implementation.
171+
*
172+
* @phpstan-param ArgsMapper $fn
173+
*
174+
* @api
175+
*/
176+
static function setDefaultArgsMapper(callable $fn): void
177+
```
178+
167179
## GraphQL\Type\Definition\Type
168180

169181
Registry of standard GraphQL types and base class for all other types.

docs/error-handling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ This will make each error entry look like this:
130130
'extensions' => [
131131
'debugMessage' => 'Actual exception message',
132132
'trace' => [
133-
/* Formatted original exception trace */
133+
// Formatted original exception trace
134134
],
135135
],
136136
]

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^SplObjectStorage\\<GraphQL\\\\Type\\\\Definition\\\\ObjectType, SplObjectStorage\\<ArrayObject\\<int, GraphQL\\\\Language\\\\AST\\\\FieldNode\\>, ArrayObject\\<string, ArrayObject\\<int, GraphQL\\\\Language\\\\AST\\\\FieldNode\\>\\>\\>\\> does not accept SplObjectStorage\\<ArrayObject\\<int, GraphQL\\\\Language\\\\AST\\\\FieldNode\\>, ArrayObject\\<string, ArrayObject\\<int, GraphQL\\\\Language\\\\AST\\\\FieldNode\\>\\>\\>\\|SplObjectStorage\\<object, mixed\\>\\.$#"
5-
count: 1
6-
path: src/Executor/ReferenceExecutor.php
7-
83
-
94
message: "#^Unable to resolve the template type TCloneable in call to method static method GraphQL\\\\Language\\\\AST\\\\Node\\:\\:cloneValue\\(\\)$#"
105
count: 1

src/Executor/Executor.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,25 @@ public static function setDefaultFieldResolver(callable $fieldResolver): void
6363
self::$defaultFieldResolver = $fieldResolver;
6464
}
6565

66+
/** @phpstan-return ArgsMapper */
67+
public static function getDefaultArgsMapper(): callable
68+
{
69+
return self::$defaultArgsMapper;
70+
}
71+
6672
/** @phpstan-param ArgsMapper $argsMapper */
6773
public static function setDefaultArgsMapper(callable $argsMapper): void
6874
{
6975
self::$defaultArgsMapper = $argsMapper;
7076
}
7177

72-
public static function getPromiseAdapter(): PromiseAdapter
78+
public static function getDefaultPromiseAdapter(): PromiseAdapter
7379
{
7480
return self::$defaultPromiseAdapter ??= new SyncPromiseAdapter();
7581
}
7682

7783
/** Set a custom default promise adapter. */
78-
public static function setPromiseAdapter(?PromiseAdapter $defaultPromiseAdapter = null): void
84+
public static function setDefaultPromiseAdapter(?PromiseAdapter $defaultPromiseAdapter = null): void
7985
{
8086
self::$defaultPromiseAdapter = $defaultPromiseAdapter;
8187
}

src/Executor/ReferenceExecutor.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ class ReferenceExecutor implements ExecutorImplementation
6161
*/
6262
protected \SplObjectStorage $subFieldCache;
6363

64-
/** @var \SplObjectStorage<FieldDefinition, \SplObjectStorage<FieldNode, mixed>> */
64+
/**
65+
* @var \SplObjectStorage<
66+
* FieldDefinition,
67+
* \SplObjectStorage<FieldNode, mixed>
68+
* >
69+
*/
6570
protected \SplObjectStorage $fieldArgsCache;
6671

6772
protected function __construct(ExecutionContext $context)
@@ -94,7 +99,7 @@ public static function create(
9499
array $variableValues,
95100
?string $operationName,
96101
callable $fieldResolver,
97-
callable $argsMapper
102+
?callable $argsMapper = null // TODO make non-optional in next major release
98103
): ExecutorImplementation {
99104
$exeContext = static::buildExecutionContext(
100105
$schema,
@@ -104,7 +109,7 @@ public static function create(
104109
$variableValues,
105110
$operationName,
106111
$fieldResolver,
107-
$argsMapper,
112+
$argsMapper ?? Executor::getDefaultArgsMapper(),
108113
$promiseAdapter,
109114
);
110115

@@ -128,8 +133,8 @@ public function doExecute(): Promise
128133
}
129134

130135
/**
131-
* Constructs an ExecutionContext object from the arguments passed to
132-
* execute, which we will pass throughout the other execution methods.
136+
* Constructs an ExecutionContext object from the arguments passed to execute,
137+
* which we will pass throughout the other execution methods.
133138
*
134139
* @param mixed $rootValue
135140
* @param mixed $contextValue
@@ -741,7 +746,7 @@ protected function resolveFieldValueOrError(
741746
try {
742747
// Build a map of arguments from the field.arguments AST, using the
743748
// variables scope to fulfill any variable references.
744-
/** @phpstan-ignore-next-line ignored because no way to tell phpstan what are generics of SplObjectStorage without assign it to var first */
749+
// @phpstan-ignore-next-line generics of SplObjectStorage are not inferred from empty instantiation
745750
$this->fieldArgsCache[$fieldDef] ??= new \SplObjectStorage();
746751

747752
$args = $this->fieldArgsCache[$fieldDef][$fieldNode] ??= $argsMapper(Values::getArgumentValues(
@@ -1331,6 +1336,7 @@ protected function collectAndExecuteSubfields(
13311336
*/
13321337
protected function collectSubFields(ObjectType $returnType, \ArrayObject $fieldNodes): \ArrayObject
13331338
{
1339+
// @phpstan-ignore-next-line generics of SplObjectStorage are not inferred from empty instantiation
13341340
$returnTypeCache = $this->subFieldCache[$returnType] ??= new \SplObjectStorage();
13351341

13361342
if (! isset($returnTypeCache[$fieldNodes])) {

src/GraphQL.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* This is the primary facade for fulfilling GraphQL operations.
2525
* See [related documentation](executing-queries.md).
2626
*
27+
* @phpstan-import-type ArgsMapper from Executor
2728
* @phpstan-import-type FieldResolver from Executor
2829
*
2930
* @see \GraphQL\Tests\GraphQLTest
@@ -243,4 +244,16 @@ public static function setDefaultFieldResolver(callable $fn): void
243244
{
244245
Executor::setDefaultFieldResolver($fn);
245246
}
247+
248+
/**
249+
* Set default args mapper implementation.
250+
*
251+
* @phpstan-param ArgsMapper $fn
252+
*
253+
* @api
254+
*/
255+
public static function setDefaultArgsMapper(callable $fn): void
256+
{
257+
Executor::setDefaultArgsMapper($fn);
258+
}
246259
}

src/Server/Helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function validateOperationParams(OperationParams $params): array
197197
*/
198198
public function executeOperation(ServerConfig $config, OperationParams $op)
199199
{
200-
$promiseAdapter = $config->getPromiseAdapter() ?? Executor::getPromiseAdapter();
200+
$promiseAdapter = $config->getPromiseAdapter() ?? Executor::getDefaultPromiseAdapter();
201201
$result = $this->promiseToExecuteOperation($promiseAdapter, $config, $op);
202202

203203
if ($promiseAdapter instanceof SyncPromiseAdapter) {
@@ -222,7 +222,7 @@ public function executeOperation(ServerConfig $config, OperationParams $op)
222222
*/
223223
public function executeBatch(ServerConfig $config, array $operations)
224224
{
225-
$promiseAdapter = $config->getPromiseAdapter() ?? Executor::getPromiseAdapter();
225+
$promiseAdapter = $config->getPromiseAdapter() ?? Executor::getDefaultPromiseAdapter();
226226

227227
$result = [];
228228
foreach ($operations as $operation) {

tests/Executor/ExecutorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class ExecutorTest extends TestCase
2929

3030
public function tearDown(): void
3131
{
32-
Executor::setPromiseAdapter(null);
32+
Executor::setDefaultPromiseAdapter(null);
3333
}
3434

3535
// Execute: Handles basic execution tasks
@@ -398,8 +398,8 @@ public function testArgsMapper(): void
398398
]),
399399
]);
400400
$result = Executor::execute($schema, $docAst);
401-
self::assertEquals(1, $mapperCalledCount);
402-
self::assertEquals(3, $resolverCalledCount);
401+
self::assertSame(1, $mapperCalledCount);
402+
self::assertSame(3, $resolverCalledCount);
403403
self::assertCount(0, $result->errors);
404404
}
405405

tests/Language/VisitorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private function checkVisitorFnArgs(DocumentNode $ast, array $args, bool $isEdit
6666
if ($parent instanceof NodeList) {
6767
self::assertEquals($node, $parent[$key]);
6868
} else {
69-
/** @phpstan-ignore-next-line */
69+
// @phpstan-ignore-next-line dynamic property access
7070
self::assertEquals($node, $parent->{$key});
7171
}
7272

0 commit comments

Comments
 (0)