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 abab343
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
36 changes: 23 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,10 @@ 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 +50,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 54 in src/GraphQl/Resolver/Factory/ResolverFactory.php

View check run for this annotation

Codecov / codecov/patch

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

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

$graphQlContext = [];
$context = ['source' => $source, 'args' => $args, 'info' => $info, 'root_class' => $rootClass, 'graphql_context' => &$graphQlContext];
private function process(?array $source, array $args, ResolveInfo $info, string $rootClass = null, Operation $operation = null, mixed $body) {

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L59 was not covered by tests

if (null === $operation->canValidate()) {
$operation = $operation->withValidate($operation instanceof Mutation);
}
$graphQlContext = [];
$context = ['source' => $source, 'args' => $args, 'info' => $info, 'root_class' => $rootClass, 'graphql_context' => &$graphQlContext];

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L61 - L62 were not covered by tests

$body = $this->provider->provide($operation, [], $context);
if (null === $operation->canValidate()) {
$operation = $operation->withValidate($operation instanceof Mutation);

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L64 - L65 were not covered by tests
}

if (null === $operation->canWrite()) {
$operation = $operation->withWrite($operation instanceof Mutation && null !== $body);
}
$body ??= $this->provider->provide($operation, [], $context);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L68 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L70 - L71 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L74 was not covered by tests

return $this->processor->process($body, $operation, [], $context);
};
}
}
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 abab343

Please sign in to comment.