From 8a8728c3c236923d9997cfb296c1ecd8a732b18c Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Thu, 23 Apr 2020 15:58:31 +0200 Subject: [PATCH] Fix serializer context attributes for page-based pagination --- CHANGELOG.md | 2 +- features/graphql/collection.feature | 11 +++++++++++ src/GraphQl/Serializer/SerializerContextBuilder.php | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a864b0fea9..1a01e43b677 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ * MongoDB: Mercure support (#3290) * GraphQL: Subscription support with Mercure (#3321) * GraphQL: Allow to format GraphQL errors based on exceptions (#3063) -* GraphQL: Add page-based pagination (#3175) +* GraphQL: Add page-based pagination (#3175, #3517) * GraphQL: Possibility to add a custom description for queries, mutations and subscriptions (#3477, #3514) * GraphQL: Support for field name conversion (serialized name) (#3455, #3516) * OpenAPI: Add PHP default values to the documentation (#2386) diff --git a/features/graphql/collection.feature b/features/graphql/collection.feature index 1080036b166..b6cdd7ef027 100644 --- a/features/graphql/collection.feature +++ b/features/graphql/collection.feature @@ -690,6 +690,7 @@ Feature: GraphQL collection support fooDummies(page: 1) { collection { id + name } paginationInfo { itemsPerPage @@ -703,8 +704,11 @@ Feature: GraphQL collection support And the response should be in JSON And the JSON node "data.fooDummies.collection" should have 3 elements And the JSON node "data.fooDummies.collection[0].id" should exist + And the JSON node "data.fooDummies.collection[0].name" should exist And the JSON node "data.fooDummies.collection[1].id" should exist + And the JSON node "data.fooDummies.collection[1].name" should exist And the JSON node "data.fooDummies.collection[2].id" should exist + And the JSON node "data.fooDummies.collection[2].name" should exist And the JSON node "data.fooDummies.paginationInfo.itemsPerPage" should be equal to the number 3 And the JSON node "data.fooDummies.paginationInfo.lastPage" should be equal to the number 2 And the JSON node "data.fooDummies.paginationInfo.totalCount" should be equal to the number 5 @@ -714,6 +718,7 @@ Feature: GraphQL collection support fooDummies(page: 2) { collection { id + name } } } @@ -727,6 +732,7 @@ Feature: GraphQL collection support fooDummies(page: 3) { collection { id + name } } } @@ -744,6 +750,7 @@ Feature: GraphQL collection support fooDummies(page: 1, itemsPerPage: 2) { collection { id + name } } } @@ -752,13 +759,16 @@ Feature: GraphQL collection support And the response should be in JSON And the JSON node "data.fooDummies.collection" should have 2 elements And the JSON node "data.fooDummies.collection[0].id" should exist + And the JSON node "data.fooDummies.collection[0].name" should exist And the JSON node "data.fooDummies.collection[1].id" should exist + And the JSON node "data.fooDummies.collection[1].name" should exist When I send the following GraphQL request: """ { fooDummies(page: 2, itemsPerPage: 2) { collection { id + name } } } @@ -772,6 +782,7 @@ Feature: GraphQL collection support fooDummies(page: 3, itemsPerPage: 2) { collection { id + name } } } diff --git a/src/GraphQl/Serializer/SerializerContextBuilder.php b/src/GraphQl/Serializer/SerializerContextBuilder.php index a822c35474d..5a4b353a4d1 100644 --- a/src/GraphQl/Serializer/SerializerContextBuilder.php +++ b/src/GraphQl/Serializer/SerializerContextBuilder.php @@ -78,7 +78,7 @@ private function fieldsToAttributes(?string $resourceClass, ?ResourceMetadata $r $fields = $info->getFieldSelection(PHP_INT_MAX); } - $attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields, $resourceClass, $context); + $attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields['collection'] ?? $fields, $resourceClass, $context); if ($resolverContext['is_mutation'] || $resolverContext['is_subscription']) { if (!$resourceMetadata) {