Skip to content

Commit 3fb81ef

Browse files
authored
fix(graphql): nextPage and prevPage are non nullable even though they can be null sometimes (#12201)
This PR introduced #11952 improvement for graphql schema with making fields of the `Paginated<T>` 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. <img width="873" alt="image" src="https://github.com/user-attachments/assets/04d04b13-ac26-4fc1-b421-b5f86efc9b65" />
1 parent 3c9ee5d commit 3fb81ef

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/graphql/src/schema/buildPaginatedListType.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export const buildPaginatedListType = (name, docType) =>
1010
hasNextPage: { type: new GraphQLNonNull(GraphQLBoolean) },
1111
hasPrevPage: { type: new GraphQLNonNull(GraphQLBoolean) },
1212
limit: { type: new GraphQLNonNull(GraphQLInt) },
13-
nextPage: { type: new GraphQLNonNull(GraphQLInt) },
13+
nextPage: { type: GraphQLInt },
1414
offset: { type: GraphQLInt },
1515
page: { type: new GraphQLNonNull(GraphQLInt) },
1616
pagingCounter: { type: new GraphQLNonNull(GraphQLInt) },
17-
prevPage: { type: new GraphQLNonNull(GraphQLInt) },
17+
prevPage: { type: GraphQLInt },
1818
totalDocs: { type: new GraphQLNonNull(GraphQLInt) },
1919
totalPages: { type: new GraphQLNonNull(GraphQLInt) },
2020
},

test/graphql/int.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,50 @@ describe('graphql', () => {
104104

105105
expect(res.hyphenated_name).toStrictEqual('example-hyphenated-name')
106106
})
107+
108+
it('should not error because of non nullable fields', async () => {
109+
await payload.delete({ collection: 'posts', where: {} })
110+
111+
// this is an array if any errors
112+
const res_1 = await restClient
113+
.GRAPHQL_POST({
114+
body: JSON.stringify({
115+
query: `
116+
query {
117+
Posts {
118+
docs {
119+
title
120+
}
121+
prevPage
122+
}
123+
}
124+
`,
125+
}),
126+
})
127+
.then((res) => res.json())
128+
expect(res_1.errors).toBeFalsy()
129+
130+
await payload.create({
131+
collection: 'posts',
132+
data: { title: 'any-title' },
133+
})
134+
135+
const res_2 = await restClient
136+
.GRAPHQL_POST({
137+
body: JSON.stringify({
138+
query: `
139+
query {
140+
Posts(limit: 1) {
141+
docs {
142+
title
143+
}
144+
}
145+
}
146+
`,
147+
}),
148+
})
149+
.then((res) => res.json())
150+
expect(res_2.errors).toBeFalsy()
151+
})
107152
})
108153
})

0 commit comments

Comments
 (0)