fix(server): handle graphql bad input error (#10854)

This commit is contained in:
fengmk2
2025-03-14 07:37:29 +00:00
parent 95cbdc7900
commit f61b166e61
3 changed files with 31 additions and 6 deletions

View File

@@ -1,3 +1,5 @@
import assert from 'node:assert/strict';
import {
applyDecorators,
Body,
@@ -130,7 +132,12 @@ function gql(app: INestApplication, query: string) {
return request(app.getHttpServer())
.post('/graphql')
.send({ query })
.expect(200);
.expect(res => {
assert(
res.status === 200 || res.status === 400,
'GraphQL query should return 200 or 400'
);
});
}
test.before(async ({ context }) => {
@@ -199,6 +206,15 @@ test('should be able to handle validation error in graphql query', async t => {
t.true(t.context.logger.error.notCalled);
});
test('should be able to handle graphql input error', async t => {
const res = await gql(t.context.app, `mutation { validate(email: 123) }`);
const err = res.body.errors[0];
t.is(err.message, 'String cannot represent a non string value: 123');
t.is(err.extensions.status, HttpStatus.BAD_REQUEST);
t.is(err.extensions.name, 'GRAPHQL_BAD_REQUEST');
t.true(t.context.logger.error.notCalled);
});
test('should be able to respond request', async t => {
const res = await request(t.context.app.getHttpServer())
.get('/ok')