Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Fix type for customFormatErrorFn #699

Closed

Conversation

hokaccha
Copy link

@hokaccha hokaccha commented Oct 8, 2020

customFormatErrorFn may be passed HttpError as well as GraphQLError. So the argument type of customFormatErrorFn should be GraphQLError | HttpError.

Example

import { graphqlHTTP } from "express-graphql";
import express from "express";
import { buildSchema, formatError, GraphQLError, GraphQLFormattedError } from "graphql";
import { HttpError } from "http-errors";

const app = express();

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

app.use(
  "/graphql",
  graphqlHTTP(() => {
    return {
      schema,
      customFormatErrorFn: (err: GraphQLError): GraphQLFormattedError => {
        (err as any).extensions = {
          name: err.name,
          isHttpError: err instanceof HttpError,
        };
        return formatError(err);
      },
    };
  })
);

app.listen(8000);
$ curl -s -X POST http://localhost:8000/graphql | jq
{
  "errors": [
    {
      "message": "Must provide query string.",
      "extensions": {
        "name": "BadRequestError",
        "isHttpError": true
      }
    }
  ]
}

IvanGoncharov added a commit to IvanGoncharov/express-graphql that referenced this pull request Nov 9, 2020
IvanGoncharov added a commit that referenced this pull request Nov 9, 2020
@IvanGoncharov
Copy link
Member

@hokaccha Thanks for the detailed report 👍
I change code to always wrap HTTP errors in GraphQLError in #716

@hokaccha
Copy link
Author

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants