Skip to content

Commit

Permalink
do not overwrite existing status on validation, if any (#2872)
Browse files Browse the repository at this point in the history
* do not overwrite existing status on validation, if any

* add changeset

* add unit test

* fix one more case and add test to validate

* lint fixes
  • Loading branch information
nescalante committed Jun 18, 2023
1 parent 458ceaf commit ce6d246
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-ravens-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-yoga': patch
---

Avoid overriding http status on extensions when using a plugin that modifies error prop
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { AfterValidateEventPayload } from '@envelop/core'
import { createSchema } from '../../schema.js'
import { createYoga } from '../../server.js'

describe('useHTTPValidationError', () => {
describe('when doing a request with an invalid query', () => {
it('does not overrite status code set on custom plugin', async () => {
const schemaFactory = async () => {
return createSchema({
typeDefs: /* GraphQL */ `
type Query {
foo: String
}
`,
resolvers: {
Query: {
foo: () => 'bar',
},
},
})
}
const yoga = createYoga({
schema: schemaFactory,
plugins: [
{
onValidate() {
return ({
valid,
result,
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
AfterValidateEventPayload<Record<string, any>>) => {
if (!valid) {
for (const error of result) {
error.extensions.http = {
status: 400,
spec: false,
headers: {
'custom-header': 'custom-value',
},
}
}
}
}
},
},
],
})
const result = await yoga.fetch('http://yoga/graphql', {
method: 'POST',
body: JSON.stringify({
query: 'query{bar}',
}),
headers: {
'Content-Type': 'application/json',
},
})
expect(result.status).toEqual(400)
expect(result.headers.get('custom-header')).toEqual('custom-value')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export function useHTTPValidationError<
if (!valid) {
for (const error of result) {
error.extensions.http = {
spec: true,
status: 400,
...error.extensions.http,
spec: error.extensions.http?.spec ?? true,
status: error.extensions.http?.status ?? 400,
}
}
}
Expand Down

0 comments on commit ce6d246

Please sign in to comment.