Skip to content

Commit 679cbe1

Browse files
authored
Optimize and deduplicate ReferenceExecutor
1 parent d8ea59b commit 679cbe1

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

src/Executor/ReferenceExecutor.php

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ protected static function buildExecutionContext(
205205
return $errors;
206206
}
207207

208-
assert($operation instanceof OperationDefinitionNode, 'Has operation if no errors.');
209-
assert(\is_array($variableValues), 'Has variables if no errors.');
208+
\assert($operation instanceof OperationDefinitionNode, 'Has operation if no errors.');
209+
\assert(\is_array($variableValues), 'Has variables if no errors.');
210210

211211
return new ExecutionContext(
212212
$schema,
@@ -536,18 +536,17 @@ protected function executeFieldsSerially(ObjectType $parentType, $rootValue, arr
536536
\array_keys($fields->getArrayCopy()),
537537
function ($results, $responseName) use ($contextValue, $path, $unaliasedPath, $parentType, $rootValue, $fields) {
538538
$fieldNodes = $fields[$responseName];
539-
assert($fieldNodes instanceof \ArrayObject, 'The keys of $fields populate $responseName');
539+
\assert($fieldNodes instanceof \ArrayObject, 'The keys of $fields populate $responseName');
540540

541-
$fieldPath = $path;
542-
$fieldPath[] = $responseName;
543-
544-
$fieldNode = $fieldNodes[0];
545-
assert($fieldNode instanceof FieldNode, '$fieldNodes is non-empty');
546-
547-
$fieldUnaliasedPath = $unaliasedPath;
548-
$fieldUnaliasedPath[] = $fieldNode->name->value;
549-
550-
$result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath, $fieldUnaliasedPath, $this->maybeScopeContext($contextValue));
541+
$result = $this->resolveField(
542+
$parentType,
543+
$rootValue,
544+
$fieldNodes,
545+
$responseName,
546+
$path,
547+
$unaliasedPath,
548+
$this->maybeScopeContext($contextValue)
549+
);
551550
if ($result === static::$UNDEFINED) {
552551
return $results;
553552
}
@@ -600,18 +599,29 @@ function ($results, $responseName) use ($contextValue, $path, $unaliasedPath, $p
600599
*
601600
* @return array<mixed>|\Throwable|mixed|null
602601
*/
603-
protected function resolveField(ObjectType $parentType, $rootValue, \ArrayObject $fieldNodes, array $path, array $unaliasedPath, $contextValue)
604-
{
602+
protected function resolveField(
603+
ObjectType $parentType,
604+
$rootValue,
605+
\ArrayObject $fieldNodes,
606+
string $responseName,
607+
array $path,
608+
array $unaliasedPath,
609+
$contextValue
610+
) {
605611
$exeContext = $this->exeContext;
612+
606613
$fieldNode = $fieldNodes[0];
607-
assert($fieldNode instanceof FieldNode, '$fieldNodes is non-empty');
614+
\assert($fieldNode instanceof FieldNode, '$fieldNodes is non-empty');
608615

609616
$fieldName = $fieldNode->name->value;
610617
$fieldDef = $this->getFieldDef($exeContext->schema, $parentType, $fieldName);
611618
if ($fieldDef === null || ! $fieldDef->isVisible()) {
612619
return static::$UNDEFINED;
613620
}
614621

622+
$path[] = $responseName;
623+
$unaliasedPath[] = $fieldName;
624+
615625
$returnType = $fieldDef->getType();
616626
// The resolve function's optional 3rd argument is a context value that
617627
// is provided to every resolve function within an execution. It is commonly
@@ -892,11 +902,11 @@ protected function completeValue(
892902
return $this->completeListValue($returnType, $fieldNodes, $info, $path, $unaliasedPath, $result, $contextValue);
893903
}
894904

895-
assert($returnType instanceof NamedType, 'Wrapping types should return early');
905+
\assert($returnType instanceof NamedType, 'Wrapping types should return early');
896906

897907
// Account for invalid schema definition when typeLoader returns different
898908
// instance than `resolveType` or $field->getType() or $arg->getType()
899-
assert(
909+
\assert(
900910
$returnType === $this->exeContext->schema->getType($returnType->name),
901911
SchemaValidationContext::duplicateType($this->exeContext->schema, "{$info->parentType}.{$info->fieldName}", $returnType->name)
902912
);
@@ -1235,7 +1245,7 @@ protected function completeObjectValue(
12351245
});
12361246
}
12371247

1238-
assert(is_bool($isTypeOf), 'Promise would return early');
1248+
\assert(is_bool($isTypeOf), 'Promise would return early');
12391249
if (! $isTypeOf) {
12401250
throw $this->invalidReturnTypeError($returnType, $result, $fieldNodes);
12411251
}
@@ -1350,16 +1360,15 @@ protected function executeFields(ObjectType $parentType, $rootValue, array $path
13501360
$containsPromise = false;
13511361
$results = [];
13521362
foreach ($fields as $responseName => $fieldNodes) {
1353-
$fieldPath = $path;
1354-
$fieldPath[] = $responseName;
1355-
1356-
$fieldNode = $fieldNodes[0];
1357-
assert($fieldNode instanceof FieldNode, '$fieldNodes is non-empty');
1358-
1359-
$fieldUnaliasedPath = $unaliasedPath;
1360-
$fieldUnaliasedPath[] = $fieldNode->name->value;
1361-
1362-
$result = $this->resolveField($parentType, $rootValue, $fieldNodes, $fieldPath, $fieldUnaliasedPath, $this->maybeScopeContext($contextValue));
1363+
$result = $this->resolveField(
1364+
$parentType,
1365+
$rootValue,
1366+
$fieldNodes,
1367+
$responseName,
1368+
$path,
1369+
$unaliasedPath,
1370+
$this->maybeScopeContext($contextValue)
1371+
);
13631372
if ($result === static::$UNDEFINED) {
13641373
continue;
13651374
}
@@ -1449,12 +1458,12 @@ protected function ensureValidRuntimeType(
14491458
throw new InvariantViolation("Runtime Object type \"{$runtimeType}\" is not a possible type for \"{$returnType}\".");
14501459
}
14511460

1452-
assert(
1461+
\assert(
14531462
$this->exeContext->schema->getType($runtimeType->name) !== null,
14541463
"Schema does not contain type \"{$runtimeType}\". This can happen when an object type is only referenced indirectly through abstract types and never directly through fields.List the type in the option \"types\" during schema construction, see https://webonyx.github.io/graphql-php/schema-definition/#configuration-options."
14551464
);
14561465

1457-
assert(
1466+
\assert(
14581467
$runtimeType === $this->exeContext->schema->getType($runtimeType->name),
14591468
"Schema must contain unique named types but contains multiple types named \"{$runtimeType}\". Make sure that `resolveType` function of abstract type \"{$returnType}\" returns the same type instance as referenced anywhere else within the schema (see https://webonyx.github.io/graphql-php/type-definitions/#type-registry)."
14601469
);

0 commit comments

Comments
 (0)