Skip to content

Commit 6d97b96

Browse files
committed
refactor(graphql): share private field extraction logic
Signed-off-by: psihius <arvids.godjuks@gmail.com>
1 parent e4452a4 commit 6d97b96

File tree

20 files changed

+2329
-210
lines changed

20 files changed

+2329
-210
lines changed

src/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,37 +133,34 @@ protected function normalizeCollectionOfRelations(ApiProperty $propertyMetadata,
133133
// Handle relationships for mercure subscriptions
134134
if ($operation instanceof QueryCollection && 'mercure_subscription' === $context['graphql_operation_name'] && $attributeValue instanceof Collection && !$attributeValue->isEmpty()) {
135135
$relationContext = $context;
136-
// Grab collection attributes
137136
$relationContext['attributes'] = $context['attributes']['collection'];
138-
// Iterate over the collection and normalize each item
139-
$data['collection'] = $attributeValue
140-
->map(fn ($item) => $this->normalize($item, $format, $relationContext))
141-
// Convert the collection to an array
142-
->toArray();
143-
144-
// Handle pagination if it's enabled in the query
145-
return $this->addPagination($attributeValue, $data, $context);
137+
$data['collection'] = [];
138+
foreach ($attributeValue as $item) {
139+
$data['collection'][] = $this->normalize($item, $format, $relationContext);
140+
}
141+
142+
return $this->addPagination($attributeValue->count(), $data, $context);
146143
}
147144

148145
// to-many are handled directly by the GraphQL resolver
149146
return [];
150147
}
151148

152-
private function addPagination(Collection $collection, array $data, array $context): array
149+
private function addPagination(int $totalCount, array $data, array $context): array
153150
{
154151
if ($context['attributes']['paginationInfo'] ?? false) {
155152
$data['paginationInfo'] = [];
156153
if (\array_key_exists('hasNextPage', $context['attributes']['paginationInfo'])) {
157-
$data['paginationInfo']['hasNextPage'] = $collection->count() > ($context['pagination']['itemsPerPage'] ?? 10);
154+
$data['paginationInfo']['hasNextPage'] = $totalCount > ($context['pagination']['itemsPerPage'] ?? 10);
158155
}
159156
if (\array_key_exists('itemsPerPage', $context['attributes']['paginationInfo'])) {
160157
$data['paginationInfo']['itemsPerPage'] = $context['pagination']['itemsPerPage'] ?? 10;
161158
}
162159
if (\array_key_exists('lastPage', $context['attributes']['paginationInfo'])) {
163-
$data['paginationInfo']['lastPage'] = (int) ceil($collection->count() / ($context['pagination']['itemsPerPage'] ?? 10));
160+
$data['paginationInfo']['lastPage'] = (int) ceil($totalCount / ($context['pagination']['itemsPerPage'] ?? 10));
164161
}
165162
if (\array_key_exists('totalCount', $context['attributes']['paginationInfo'])) {
166-
$data['paginationInfo']['totalCount'] = $collection->count();
163+
$data['paginationInfo']['totalCount'] = $totalCount;
167164
}
168165
}
169166

src/GraphQl/State/Processor/SubscriptionProcessor.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use ApiPlatform\GraphQl\Subscription\MercureSubscriptionIriGeneratorInterface;
1717
use ApiPlatform\GraphQl\Subscription\OperationAwareSubscriptionManagerInterface;
1818
use ApiPlatform\GraphQl\Subscription\SubscriptionManagerInterface;
19-
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
2019
use ApiPlatform\Metadata\GraphQl\Subscription;
2120
use ApiPlatform\Metadata\Operation;
2221
use ApiPlatform\State\ProcessorInterface;
@@ -50,9 +49,6 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
5049

5150
$hub = \is_array($mercure) ? ($mercure['hub'] ?? null) : null;
5251
$data['mercureUrl'] = $this->mercureSubscriptionIriGenerator->generateMercureUrl($subscriptionId, $hub);
53-
if ($operation instanceof Subscription) {
54-
$data['isCollection'] = $operation->isCollection();
55-
}
5652
}
5753

5854
return $data;

0 commit comments

Comments
 (0)