Skip to content

Commit bb43e63

Browse files
feat: tracking prepared abstract type
1 parent 344b17e commit bb43e63

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/Execution.php

+20-24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GraphQL\Language\AST\OperationDefinitionNode;
1212
use GraphQL\Type\Definition\AbstractType;
1313
use GraphQL\Type\Definition\FieldDefinition;
14+
use GraphQL\Type\Definition\NamedType;
1415
use GraphQL\Type\Definition\ObjectType;
1516
use GraphQL\Type\Definition\ResolveInfo;
1617
use GraphQL\Type\Definition\Type;
@@ -25,9 +26,9 @@
2526
final class Execution
2627
{
2728
/**
28-
* @var \WeakMap<ObjectType>
29+
* @var \WeakMap<NamedType&Type>
2930
*/
30-
private \WeakMap $hijackedResolvers;
31+
private \WeakMap $preparedTypes;
3132

3233
/**
3334
* @var \WeakMap<OperationDefinitionNode>
@@ -38,7 +39,7 @@ private function __construct(
3839
private readonly DelegatorInterface $delegator,
3940
private readonly ?ErrorsReporterInterface $errorsReporter
4041
) {
41-
$this->hijackedResolvers = new \WeakMap();
42+
$this->preparedTypes = new \WeakMap();
4243
$this->delegatedPromises = new \WeakMap();
4344
}
4445

@@ -56,26 +57,10 @@ public static function delegate(
5657
continue;
5758
}
5859

59-
$execution->hijackResolver($operationType);
60+
$execution->prepareType($operationType);
6061
}
6162
}
6263

63-
private function hijackResolver(ObjectType $type): void
64-
{
65-
if (isset($this->hijackedResolvers[$type])) {
66-
return;
67-
}
68-
69-
foreach ($type->getFields() as $fieldDef) {
70-
/** @var FieldDefinition $fieldDef */
71-
$fieldDef->resolveFn = $this->resolve(...);
72-
}
73-
74-
$type->resolveFieldFn = null;
75-
76-
$this->hijackedResolvers[$type] = true;
77-
}
78-
7964
private function resolve(mixed $value, array $args, mixed $context, ResolveInfo $info): Promise
8065
{
8166
$promise = $this->delegatedPromises[$info->operation] ??= $this->delegateToExecute(
@@ -87,7 +72,7 @@ private function resolve(mixed $value, array $args, mixed $context, ResolveInfo
8772

8873
return $promise->then(
8974
function (ExecutionResult $result) use ($info): mixed {
90-
$this->prepareReturnType($info->returnType);
75+
$this->prepareType($info->returnType);
9176

9277
return $this->accessResultByPath($info->path, $result);
9378
}
@@ -135,19 +120,30 @@ function (ExecutionResult $result): ExecutionResult {
135120
);
136121
}
137122

138-
private function prepareReturnType(Type $type): void
123+
private function prepareType(Type $type): void
139124
{
140125
if ($type instanceof WrappingType) {
141126
$type = $type->getInnermostType();
142127
}
143128

129+
if (isset($this->preparedTypes[$type])) {
130+
return;
131+
}
132+
144133
if ($type instanceof ObjectType) {
145-
$this->hijackResolver($type);
134+
foreach ($type->getFields() as $fieldDef) {
135+
/** @var FieldDefinition $fieldDef */
136+
$fieldDef->resolveFn = $this->resolve(...);
137+
}
138+
139+
$type->resolveFieldFn = null;
146140
}
147141

148142
if ($type instanceof AbstractType) {
149143
$type->config['resolveType'] = $this->resolveAbstractType(...);
150144
}
145+
146+
$this->preparedTypes[$type] = true;
151147
}
152148

153149
private function resolveAbstractType(array $value, mixed $context, ResolveInfo $info): Type
@@ -172,7 +168,7 @@ private function resolveAbstractType(array $value, mixed $context, ResolveInfo $
172168

173169
assert($implType instanceof ObjectType);
174170

175-
$this->hijackResolver($implType);
171+
$this->prepareType($implType);
176172

177173
return $implType;
178174
}

0 commit comments

Comments
 (0)