Skip to content

Commit

Permalink
fix(graphql): refactor handling of nested collections
Browse files Browse the repository at this point in the history
  • Loading branch information
josef.wagner committed Dec 20, 2023
1 parent 59ea385 commit 899ac0a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
37 changes: 24 additions & 13 deletions src/GraphQl/Resolver/Factory/ResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Metadata\GraphQl\Mutation;
use ApiPlatform\Metadata\GraphQl\Operation;
use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\State\Pagination\ArrayPaginator;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use GraphQL\Type\Definition\ResolveInfo;
Expand All @@ -33,6 +34,11 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operation) {
// Data already fetched and normalized (field or nested resource)
if ($body = $source[$info->fieldName] ?? null) {
// special treatment for nested resources without a resolver/provider
if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && !$operation->getProvider()) {
return $this->process($source, $args, $info, $rootClass, $operation, new ArrayPaginator($body, 0, \count($body)));

Check warning on line 39 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L38-L39

Added lines #L38 - L39 were not covered by tests
}

return $body;
}

Expand All @@ -45,23 +51,28 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
return null;
}

// Handles relay nodes
$operation ??= new Query();
return $this->process($source, $args, $info, $rootClass, $operation, null);
};

Check warning on line 55 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L54-L55

Added lines #L54 - L55 were not covered by tests
}

private function process(?array $source, array $args, ResolveInfo $info, string $rootClass = null, Operation $operation = null, mixed $body)

Check warning on line 58 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L58

Added line #L58 was not covered by tests
{
// Handles relay nodes
$operation ??= new Query();

Check warning on line 61 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L61

Added line #L61 was not covered by tests

$graphQlContext = [];
$context = ['source' => $source, 'args' => $args, 'info' => $info, 'root_class' => $rootClass, 'graphql_context' => &$graphQlContext];
$graphQlContext = [];
$context = ['source' => $source, 'args' => $args, 'info' => $info, 'root_class' => $rootClass, 'graphql_context' => &$graphQlContext];

Check warning on line 64 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L63-L64

Added lines #L63 - L64 were not covered by tests

if (null === $operation->canValidate()) {
$operation = $operation->withValidate($operation instanceof Mutation);
}
if (null === $operation->canValidate()) {
$operation = $operation->withValidate($operation instanceof Mutation);

Check warning on line 67 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L66-L67

Added lines #L66 - L67 were not covered by tests
}

$body = $this->provider->provide($operation, [], $context);
$body ??= $this->provider->provide($operation, [], $context);

Check warning on line 70 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L70

Added line #L70 was not covered by tests

if (null === $operation->canWrite()) {
$operation = $operation->withWrite($operation instanceof Mutation && null !== $body);
}
if (null === $operation->canWrite()) {
$operation = $operation->withWrite($operation instanceof Mutation && null !== $body);

Check warning on line 73 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L72-L73

Added lines #L72 - L73 were not covered by tests
}

return $this->processor->process($body, $operation, [], $context);
};
return $this->processor->process($body, $operation, [], $context);

Check warning on line 76 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Resolver/Factory/ResolverFactory.php#L76

Added line #L76 was not covered by tests
}
}
13 changes: 1 addition & 12 deletions src/GraphQl/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,7 @@ protected function normalizeCollectionOfRelations(ApiProperty $propertyMetadata,
// check for nested collection
$operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation('collection_query');
if ($operation instanceof Query && $operation->getNested() && !$operation->getResolver() && !$operation->getProvider()) {
if (false === $operation->getPaginationEnabled()) {
return [...$attributeValue];
}

$data = ['edges' => []];
$index = 0;
foreach ($attributeValue as $object) {
$data['edges'][$index] = ['node' => $object];
++$index;
}

return $data;
return [...$attributeValue];

Check warning on line 118 in src/GraphQl/Serializer/ItemNormalizer.php

View check run for this annotation

Codecov / codecov/patch

src/GraphQl/Serializer/ItemNormalizer.php#L116-L118

Added lines #L116 - L118 were not covered by tests
}
}

Expand Down

0 comments on commit 899ac0a

Please sign in to comment.