-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
do not overwrite existing status on validation, if any (#2872)
* 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
1 parent
458ceaf
commit ce6d246
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
61 changes: 61 additions & 0 deletions
61
packages/graphql-yoga/src/plugins/request-validation/use-http-validation-error.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters