graphql-tools: Stitching with formatError is very limited...
If you want to format your errors when using stitching, you can’t tell the error type from the error.
The issue seems to be located here:
https://github.com/apollographql/graphql-tools/blob/master/src/stitching/errors.ts#L80
So packages like apollo-errors
don’t work because you can’t get the type from it.
One solution I think I see that’s somewhat possible is to check if it’s a schema created for a remote server and if so, run checkResultAndHandleErrors
, otherwise, just throw the error as is since checkResultAndHandleErrors
looks to be made for remote responses, and if it’s local, the errors should just be thrown normally
Thoughts?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 19
- Comments: 37 (3 by maintainers)
In my case, I have micro-services connected to an api gateway that makes the stitching. The orignal error was deep buried:
error.orignalError.errors[0].originalError.errors[0]
Here is how I solved the issue:In User micro-service
directrives/auth.js
In Api Gateway
helpers/searchOriginalError.js
In Api Gateway
index.js
I hope, this helps.
Unfortunately @OlivierCuyp’s solution didn’t work in our case either. The upstream service errors weren’t being exposed to
formatError
.This flow, (flow 1)
makeRemoteExecutableSchema => createResolver => checkResultsAndHandleErrors
raised the upstream error.However the subsequent (flow 2)
MergeSchemas => delegateToSchema => applies CheckResultsAndHandleErrors as transform
raised an error that had lost the upstream error context.Narrowed it down to this line in checkResultAndHandleErrors. When this error is thrown, it’s received by the
mergeSchemas
flow but has lost the real error context.Workaround was to add an error handler to the
httpLink
that appended an additional error so that aCombinedError
got thrown fromcheckResultAndHandleErrors
like soThen flow 2 received an error with the right context that was then conveyed to
formatError
.Why wouldn’t stitched errors be appended, unaltered, to the errors array? Let the stitched schema dictate its errors’ format.
I am still having issues viewing any extra properties associated with an error inside formatError:
Inside
formatError
I would expect to find the data property but I don’t:output:
This only occurs when using
mergeSchemas
Update: I managed to finally find my error object here:
This is also impacting us.
We were unable to pass custom error codes because the merged schema swallows the thrown errors.
+1
Thanks @brycesteinhoff, I’ve got something working based on @DavidStummer’s solution.
Posting it here for the next person who faces this issue 😃
I’m having an issue with this also.
I’ve created remote executable schemas and merged them with
mergeSchemas
. I’m trying to useformatError
function when executing against the merged schema, but the error passed as an argument only has themessage
,locations
, andpath
properties, not other attributes of the error returned from the remote schema.Is there a way to make properties of errors on remote schemas available to the merged schema? Or does the logic in
mergeSchemas
swallow them?For us, using formatError and simply returning the error with NO changes impacts behavior. That’s silly right? E.g.
formatError: (error) => error
This ^ behaves differently than no formatError
We are using schema stitching… it seems like data is stripped or removed from the errors only if we have a formatError function defined.
I ran into this today. Needs a fix. Very hard to debug.
I had a subscription field returning a list which was returning this in graphql:
[null, null, null]
.The issue was one of the fields in the list items missing. There is no way to see the error without turning off stitching.
This should be labelled as bug, not enhancement.
@lakshyaranganath, it seems this issue is actually with
makeRemoteExecutableSchema
, notmergeSchemas
only. The workaround I’ve come up with for now is to intercept GraphQL errors on the remote schema, serialize the properties all into the error message as JSON, then format the errors by deserializing back to properties.