Using Apollo, I want to intercept all GraphQL server-response errors; do something w/ each error (like run each through a centralized error-service); and then forward a custom error to the feature that made the original server-request (really, I just want to be able to communicate some metadata to the feature —?it doesn't necessarily have to be a custom
Apollo doesn't seem to support the forwarding of custom errors.
I set up an error-link to:
- intercept each GraphQL error
- run it through some centralized error-service
- transform the error into a custom error object w/ extra metadata
- serialize the custom error object into a JSON string
- overwrite the
message property of the first element of the
response.errors w/ the serialized error
Then, I can parse the JSON back into a custom error instance whenever I need it further downstream.
This works fine for the Apollo hooks but breaks for the methods on the
apolloClient instance that you get from
apolloClient.query(). Specifically, the
apolloClient methods overwrite my custom error string w/ the original server-error's message.
You can see this issue for more details on the implementation.
Is there a better way? Can't imagine that this isn't a common use-case.