When using startServerAndCreateH3Handler() to handle GraphQL requests, I am not sure how to set the response status code within the resolver.
I have tried several patterns and the result is either a 200 OK returned or a server error.
// 1. 200 OK is returned
setResponseStatus(context.event, 400);
throw new Error('Error message');
// 2. 200 OK is returned
throw createError({
statusCode: 400,
statusMessage: 'Error message',
});
// 3. 400 is returned but a server error occurs
// [nuxt] [request error] [unhandled] [500] Cannot set headers after they are sent to the client
context.event.res.statusCode = 400;
context.event.res.end('Error message');
// 4. 400 is returned but a server error occurs
return sendError(context.event, createError({
statusCode: 400,
statusMessage: 'Error message',
}));
I was able to achieve my goal with the exact sample code on the page you showed me!
I see, I should refer to the apollo docs. I will look at this from now on. Thanks for letting me know.
When using
startServerAndCreateH3Handler()
to handle GraphQL requests, I am not sure how to set the response status code within the resolver. I have tried several patterns and the result is either a 200 OK returned or a server error.