Skip to content

Commit

Permalink
chore: sort code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
vuongxuongminh committed Mar 20, 2024
1 parent e60bc5b commit e57994c
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions src/Execution.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,60 @@ public static function delegate(
}
}

private function prepareType(Type $type): void
{
if ($type instanceof WrappingType) {
$type = $type->getInnermostType();
}

if (isset($this->preparedTypes[$type])) {
return;
}

if ($type instanceof ObjectType) {
foreach ($type->getFields() as $fieldDef) {
/** @var FieldDefinition $fieldDef */
$fieldDef->resolveFn = $this->resolve(...);
}

$type->resolveFieldFn = null;
}

if ($type instanceof AbstractType) {
$resolveType = fn(array $value, mixed $context, ResolveInfo $info) => $this->resolveAbstractType(
$type,
$value,
$context,
$info,
);

$type->config['resolveType'] = $resolveType;
}

$this->preparedTypes[$type] = true;
}

private function resolveAbstractType(AbstractType $abstractType, array $value, mixed $context, ResolveInfo $info): Type
{
/// __typename field should be existed in $value
/// because we have added it to delegated query
$typename = $value[Introspection::TYPE_NAME_FIELD_NAME];

if (!$info->schema->hasType($typename)) {
throw new LogicException(
sprintf('Expect type: `%s` implementing `%s` should be exist in schema', $typename, $abstractType)
);
}

$implType = $info->schema->getType($typename);

assert($implType instanceof ObjectType);

$this->prepareType($implType);

return $implType;
}

private function resolve(mixed $value, array $args, mixed $context, ResolveInfo $info): Promise
{
$promise = $this->delegatedPromises[$info->operation] ??= $this->delegateToExecute(
Expand Down Expand Up @@ -120,60 +174,6 @@ function (ExecutionResult $result): ExecutionResult {
);
}

private function prepareType(Type $type): void
{
if ($type instanceof WrappingType) {
$type = $type->getInnermostType();
}

if (isset($this->preparedTypes[$type])) {
return;
}

if ($type instanceof ObjectType) {
foreach ($type->getFields() as $fieldDef) {
/** @var FieldDefinition $fieldDef */
$fieldDef->resolveFn = $this->resolve(...);
}

$type->resolveFieldFn = null;
}

if ($type instanceof AbstractType) {
$resolveType = fn(array $value, mixed $context, ResolveInfo $info) => $this->resolveAbstractType(
$type,
$value,
$context,
$info,
);

$type->config['resolveType'] = $resolveType;
}

$this->preparedTypes[$type] = true;
}

private function resolveAbstractType(AbstractType $abstractType, array $value, mixed $context, ResolveInfo $info): Type
{
/// __typename field should be existed in $value
/// because we have added it to delegated query
$typename = $value[Introspection::TYPE_NAME_FIELD_NAME];

if (!$info->schema->hasType($typename)) {
throw new LogicException(
sprintf('Expect type: `%s` implementing `%s` should be exist in schema', $typename, $abstractType)
);
}

$implType = $info->schema->getType($typename);

assert($implType instanceof ObjectType);

$this->prepareType($implType);

return $implType;
}

/**
* @throws Error
*/
Expand Down

0 comments on commit e57994c

Please sign in to comment.