How to return both error and data in a graphql resolver?

前端 未结 3 807
南方客
南方客 2021-01-13 10:30

I was thinking about ways of implementing graphql response that would contain both an error and data.

Is it possible to do so without creating a type that would cont

3条回答
  •  执笔经年
    2021-01-13 11:20

    If you think about combining the GraphQL error - there is a way to do it in Apollo. You need to set errorPolicy to all. That will help you notify users about the error and at the same time have as much data as possible.

    none: This is the default policy to match how Apollo Client 1.0 worked. Any GraphQL Errors are treated the same as network errors and any data is ignored from the response.

    ignore: Ignore allows you to read any data that is returned alongside GraphQL Errors, but doesn’t save the errors or report them to your UI.

    all: Using the all policy is the best way to notify your users of potential issues while still showing as much data as possible from your server. It saves both data and errors into the Apollo Cache so your UI can use them.

    But according to best practices, you shouldn't manipulate it in this way. This is a great article about handling errors in GraphQL.

    So, preferable way is to add "errors" field as part of your response and handle it in JS code.

提交回复
热议问题