question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apollo Error Code lost when using mergeSchemas

See original GitHub issue

It is expected that the error codes and other error extensions maintain themselves when returning to the client. When using mergeSchemas, when a custom error is thrown using ApolloError, the error code is lost. Although it’s understandable why this might happen when merging remote schemas, this even happens with local schemas. An example server (or set of servers) and example curl requests show the difference in results:

To reproduce, run:

mkdir apollo-server-error-test && cd $_
npm i apollo-server graphql
touch index.js

Put the following into index.js:

index.js
const {
  ApolloServer,
  ApolloError,
  makeExecutableSchema,
  mergeSchemas,
  gql
} = require('apollo-server')

const schema = makeExecutableSchema({
  typeDefs: gql`
    type Query {
      available: Boolean!
    }
  `,
  resolvers: {
    Query: {
      available() {
        throw new ApolloError('some error', 'MY_CUSTOM_ERROR_CODE')
      }
    }
  }
})

const apolloServer = new ApolloServer({ schema })
apolloServer.listen(8000)

const apolloServerBadErrors = new ApolloServer({
  schema: mergeSchemas({ schemas: [schema] })
})
apolloServerBadErrors.listen(8001)

Then run NODE_ENV=production node index (production to suppress stack trace from output).

After that, run the following curl commands and you’ll get the respective results:


# From "good" server
curl -X POST http://localhost:8000/graphql \
  -H 'Content-Type:application/json' \
  -H 'Accept:application/json' \
  -d '{"query":"query{available}"}' 
response
{
  "data": null,
  "errors": [
    {
      "message": "some error",
      "locations": [{ "line": 1, "column": 7 }],
      "path": ["available"],
      "extensions": { "code": "MY_CUSTOM_ERROR_CODE" }
    }
  ]
}

# From "bad" server
curl -X POST http://localhost:8001/graphql \
  -H 'Content-Type:application/json' \
  -H 'Accept:application/json' \
  -d '{"query":"query{available}"}' 
response
{
  "data": null,
  "errors": [
    {
      "message": "some error",
      "locations": [{ "line": 1, "column": 7 }],
      "path": ["available"],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "errors": [
            { "message": "some error", "locations": [], "path": ["available"] }
          ]
        }
      }
    }
  ]
}

Perhaps this is as designed, but it would be awesome if there was a way to propagate error codes up through the mergeSchemas “firewall”.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
glassercommented, Feb 9, 2021

As mentioned on #4807, mergeSchemas is part of graphql-tools; the old version “conveniently” re-exported from Apollo Server is not maintained.

0reactions
electeriouscommented, Feb 9, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling - Apollo GraphQL Docs
When Apollo Server formats an error in a response, it sets the code extension to this value if no other code is set....
Read more >
Full Schema Stitching with Apollo Server - TomasAlabes.me
In this post we will see how we can stitch the queries/mutations and subscriptions from an Apollo Server and its Links packages. Plus...
Read more >
Apollo Server requires either an existing schema, modules or ...
For my scenario, I was getting the above error when I passed the argument like below. const server = new ApolloServer(typeDefs, resolvers);.
Read more >
apollostack - Bountysource
When intercepting an error using onError from either apollo-boost or ... I am writing graphql subscription using apollo-client and link packages . the ......
Read more >
The ultimate guide to Schema Stitching in GraphQL - Hasura
It takes an instance of Apollo-Link with the uri of a GraphQL server, ... 3. Merge all the remote schemas using mergeSchemas() : ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found