Override response status code #59
Answered
by
enisdenjo
vnagalingam
asked this question in
Q&A
|
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 |
Answered by
enisdenjo
Apr 14, 2023
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' }];
}
},
}); |
0 replies
Answer selected by
enisdenjo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there, starting from v1.18.0, you can use the
formatErrorhandler 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: