Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apollo Error Code lost when using mergeSchemas #1582

Closed
ksmithut opened this issue Aug 27, 2018 · 8 comments
Closed

Apollo Error Code lost when using mergeSchemas #1582

ksmithut opened this issue Aug 27, 2018 · 8 comments
Assignees

Comments

@ksmithut
Copy link

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".

@ghost ghost added the 🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository. label Aug 27, 2018
@ph55
Copy link

ph55 commented Dec 27, 2018

I've opened bug in graphql-tools, not sure on whom side the fix.
ardatan/graphql-tools#1037

@arc-008
Copy link

arc-008 commented Jan 9, 2019

Resolved for me after importing "extractExtensionDefinitions". ardatan/graphql-tools#1037 (comment)

@robertontiu
Copy link

Is there a fix for this? We are providing some more information besides code under extensions but everything seems to be lost now that we are using mergeSchemas.

@abernix
Copy link
Member

abernix commented Feb 20, 2019

@robertontiu I don't believe there's fix for this yet. Would it be something you'd be interested in looking into? I suspect (though I'm not certain; debugging would be required), the problem lies in the graphql-tools repository, possibly within one of the three different prongs of the conditional which starts off within the schemas.forEach loop here:

https://github.com/apollographql/graphql-tools/blob/641d7058fdf4b7af08a92a770c0b9a4f0febea29/src/stitching/mergeSchemas.ts#L100-L101

There are different forms of schemas that graphql-tools accepts, each with subtle intentional nuances. One of those might be off?

@hwillson hwillson self-assigned this Mar 7, 2019
@hwillson
Copy link
Member

hwillson commented Mar 7, 2019

Quick note - the original reproduction listed here no longer fails when using a modern day version of apollo-server (it was likely fixed by ardatan/graphql-tools#637), but this problem still persists when the error is thrown a bit differently. Here's a new reproduction that shows the problem: https://github.com/hwillson/graphql-tools-stitching-errors

I'll work on addressing this in graphql-tools, and will post back here when the fix is ready.

yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Jun 12, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Jun 18, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
@jbaxleyiii jbaxleyiii added 🚧👷‍♀️👷‍♂️🚧 in triage Issue currently being triaged and removed 🪲 bug 🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository. labels Jul 8, 2019
@jbaxleyiii
Copy link
Contributor

This is fixed in newer versions

@abernix abernix removed 🚧👷‍♀️👷‍♂️🚧 in triage Issue currently being triaged labels Jul 9, 2019
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Sep 22, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Sep 22, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Sep 22, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Oct 3, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Oct 25, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Oct 25, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Nov 4, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Dec 31, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Dec 31, 2019
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Jan 8, 2020
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Jan 21, 2020
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Feb 27, 2020
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Mar 26, 2020
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
yaacovCR pushed a commit to yaacovCR/graphql-tools-fork that referenced this issue Mar 26, 2020
Use new relocatedError function to update the original GraphQLErrors
with the new path. Addresses ardatan#743, ardatan#1037, ardatan#1046,
apollographql/apollo-server#1582.
@electerious
Copy link

It still seems to be a thing #4807

@glasser
Copy link
Member

glasser commented 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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants