Apollo Error Code lost when using mergeSchemas
See original GitHub issueIt 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:
- Created 5 years ago
- Reactions:5
- Comments:8 (4 by maintainers)
Top GitHub Comments
As mentioned on #4807, mergeSchemas is part of graphql-tools; the old version “conveniently” re-exported from Apollo Server is not maintained.
It still seems to be a thing https://github.com/apollographql/apollo-server/issues/4807