From 3fb81ef43bca83ecbba9bb0a6f54825d648485e3 Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Mon, 5 May 2025 16:12:44 +0300 Subject: [PATCH] fix(graphql): `nextPage` and `prevPage` are non nullable even though they can be `null` sometimes (#12201) This PR introduced https://github.com/payloadcms/payload/pull/11952 improvement for graphql schema with making fields of the `Paginated` interface non-nullable. However, there are a few special ones - `nextPage` and `prevPage`. They can be `null` when: The result returned 0 docs. The result returned `x` docs, but in the DB we don't have `x+1` doc. Thus, `nextPage` will be `null`. The result will have `nextPage: null`. Finally, when we query 1st page, `prevPage` is `null` as well. image --- .../src/schema/buildPaginatedListType.ts | 4 +- test/graphql/int.spec.ts | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/graphql/src/schema/buildPaginatedListType.ts b/packages/graphql/src/schema/buildPaginatedListType.ts index 343fd28cba..2a8a64c5d1 100644 --- a/packages/graphql/src/schema/buildPaginatedListType.ts +++ b/packages/graphql/src/schema/buildPaginatedListType.ts @@ -10,11 +10,11 @@ export const buildPaginatedListType = (name, docType) => hasNextPage: { type: new GraphQLNonNull(GraphQLBoolean) }, hasPrevPage: { type: new GraphQLNonNull(GraphQLBoolean) }, limit: { type: new GraphQLNonNull(GraphQLInt) }, - nextPage: { type: new GraphQLNonNull(GraphQLInt) }, + nextPage: { type: GraphQLInt }, offset: { type: GraphQLInt }, page: { type: new GraphQLNonNull(GraphQLInt) }, pagingCounter: { type: new GraphQLNonNull(GraphQLInt) }, - prevPage: { type: new GraphQLNonNull(GraphQLInt) }, + prevPage: { type: GraphQLInt }, totalDocs: { type: new GraphQLNonNull(GraphQLInt) }, totalPages: { type: new GraphQLNonNull(GraphQLInt) }, }, diff --git a/test/graphql/int.spec.ts b/test/graphql/int.spec.ts index ab4cfbdd69..6006887fb7 100644 --- a/test/graphql/int.spec.ts +++ b/test/graphql/int.spec.ts @@ -104,5 +104,50 @@ describe('graphql', () => { expect(res.hyphenated_name).toStrictEqual('example-hyphenated-name') }) + + it('should not error because of non nullable fields', async () => { + await payload.delete({ collection: 'posts', where: {} }) + + // this is an array if any errors + const res_1 = await restClient + .GRAPHQL_POST({ + body: JSON.stringify({ + query: ` +query { + Posts { + docs { + title + } + prevPage + } +} + `, + }), + }) + .then((res) => res.json()) + expect(res_1.errors).toBeFalsy() + + await payload.create({ + collection: 'posts', + data: { title: 'any-title' }, + }) + + const res_2 = await restClient + .GRAPHQL_POST({ + body: JSON.stringify({ + query: ` +query { + Posts(limit: 1) { + docs { + title + } + } +} + `, + }), + }) + .then((res) => res.json()) + expect(res_2.errors).toBeFalsy() + }) }) })