Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

javascript - Is there a better way of forwarding custom errors in apollo-client w/ apollo-link-error?

Goal

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 Error instance).

Problem

Apollo doesn't seem to support the forwarding of custom errors.

Attempt

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 useApolloClient() —?apolloClient.mutate() and 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.

Question

Is there a better way? Can't imagine that this isn't a common use-case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

28 comments

37.1k users

...