Skip to content

Override response status code #59

Answered by enisdenjo
vnagalingam asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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' }];
    }
  },
});

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by enisdenjo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants