Override response status code #59
-
Hi, For errors, how can I format error message and change the response status code? With express-graph, there was an option to set customFormatErrorFn. Is there anything similar in graphql-http? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey there, starting from v1.18.0, you can use the On the other note, status codes shouldn't be changed when dealing with handled errors since graphql-http wants to respect the GraphQL over HTTP spec. However, for anything requiring custom responses, you can use the hooks return value - for example handling auth: import { createHandler } from 'graphql-http';
import { checkAuth } from './my-auth';
const handler = createHandler({
async onSubscribe(req) {
const isAuthorized = await checkAuth(req);
if (!isAuthorized) {
return ['Who are you?', { status: 401, statusText: 'Unauthorized' }];
}
},
}); |
Beta Was this translation helpful? Give feedback.
Hey there, starting from v1.18.0, you can use the
formatError
handler option to format errors to your requirement.On the other note, status codes shouldn't be changed when dealing with handled errors since graphql-http wants to respect the GraphQL over HTTP spec. However, for anything requiring custom responses, you can use the hooks return value - for example handling auth: