Skip to content

Commit 9e9c35a

Browse files
fix: respect disabled GraphQL config, v3 backport (#17228)
Backport of #17200 to the `3.x` branch. This applies the same fix to return a 404 response when GraphQL is disabled.
1 parent 908fba1 commit 9e9c35a

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

packages/next/src/routes/graphql/handler.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,19 @@ export const POST =
104104
request,
105105
})
106106

107+
const { payload } = req
108+
109+
if (payload.config.graphQL?.disable) {
110+
return new Response(null, {
111+
status: 404,
112+
})
113+
}
114+
107115
await addDataAndFileToRequest(req)
108116
addLocalesToRequestFromData(req)
109117

110118
const { schema, validationRules } = await getGraphql(config)
111119

112-
const { payload } = req
113-
114120
const headers = {}
115121
const apiResponse = await createHandler({
116122
context: { headers, req },

test/graphql/int.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ describe('graphql', () => {
2525
})
2626

2727
describe('graphql', () => {
28+
it('should return 404 when GraphQL is disabled', async () => {
29+
const originalDisable = payload.config.graphQL?.disable
30+
31+
payload.config.graphQL.disable = true
32+
33+
try {
34+
const response = await restClient.GRAPHQL_POST({
35+
body: JSON.stringify({
36+
query: `query {
37+
Posts {
38+
docs {
39+
id
40+
}
41+
}
42+
}`,
43+
}),
44+
})
45+
46+
expect(response.status).toBe(404)
47+
} finally {
48+
payload.config.graphQL.disable = originalDisable
49+
}
50+
})
51+
2852
it('should not be able to query introspection', async () => {
2953
const query = `query {
3054
__schema {

0 commit comments

Comments
 (0)