如何在graphql解析器中同时返回错误和数据? [英] How to return both error and data in a graphql resolver?

查看:43
本文介绍了如何在graphql解析器中同时返回错误和数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑实现同时包含错误和数据的 graphql 响应的方法.

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

是否可以在不创建包含 error 的类型的情况下这样做?

Is it possible to do so without creating a type that would contain error?

例如

Mutation addMembersToTeam(membersIds: [ID!]! teamId: ID!): [Member] 向某个团队添加成员.假设使用以下 membersIds 调用此更改:[1, 2, 3].

Mutation addMembersToTeam(membersIds: [ID!]! teamId: ID!): [Member] adds members to some team. Suppose this mutation is called with the following membersIds: [1, 2, 3].

id 1 和 2 的成员已经在团队中,因此必须抛出无法添加这些成员的错误,但应添加 id 3 的成员,因为他不在团队中.

Members with ids 1 and 2 are already in the team, so an error must be thrown that these members cannot be added, but member with an id 3 should be added as he is not in the team.

我正在考虑使用 formatResponse 但似乎我无法在那里得到错误.

I was thinking about using formatResponse but seems that I can't get an error there.

是否可以在不向返回类型添加错误字段的情况下解决此问题?

Is it possible to solve this problem without adding error field to the return type?

推荐答案

是否可以在不向返回类型添加错误字段的情况下解决此问题?

Is it possible to solve this problem without adding error field to the return type?

不幸的是,没有.

解析器可以返回数据,也可以返回 null 并抛出错误.它不能两者兼而有之.澄清一下,可能会得到部分响应和一些错误.一个简单的例子:

A resolver can either return data, or return null and throw an error. It cannot do both. To clarify, it is possible to get a partial response and some errors. A simple example:

const typeDefs = `
  type Query {
    foo: Foo
  }

  type Foo {
    a: String
    b: String
  }
`
const resolvers = {
  Query: {
    foo: () => {},
  }
  Foo: {
    a: () => 'A',
    b: () => new Error('Oops!'),
  }
}

在此示例中,查询 foo 上的两个字段将导致以下响应:

In this example, querying both fields on foo will result in the following response:

{
  "data": {
    "foo": {
      "a": "A",
      "b": null
    }
  },
  "errors": [
    {
      "message": "Oops",
      "locations": [
        {
          "line": 6,
          "column": 5
        }
      ],
      "path": [
        "foo",
        "b"
      ]
    }
  ]
}

通过这种方式,可以同时发回数据和错误.但是你不能在同一个领域这样做,就像你的问题一样.有几种方法可以解决这个问题.正如您所指出的,您可以将错误作为响应的一部分返回,这通常是这样做的.然后,您可以使用 formatResponse,遍历结果数据,提取任何错误并将它们与任何其他 GraphQL 错误结合起来.不是最优的,但它可能会让你得到你正在寻找的行为.

In this way, it's possible to send back both data and errors. But you cannot do so for the same field, like in your question. There's a couple of ways around this. As you point out, you could return the errors as part of the response, which is usually how this is done. You could then use formatResponse, walk the resulting data, extract any errors and combine them with them with any other GraphQL errors. Not optimal, but it may get you the behavior you're looking for.

另一种替代方法是修改突变,使其采用单个成员 ID.然后,您可以为要添加的每个 ID 请求单独的更改:

Another alternative is to modify the mutation so it takes a single memberId. You can then request a separate mutation for each id you're adding:

add1: addMemberToTeam(memberId: $memberId1 teamId: $teamId): {
  id
}
add2: addMemberToTeam(memberId: $memberId2 teamId: $teamId): {
  id
}
add3: addMemberToTeam(memberId: $memberId3 teamId: $teamId): {
  id
}

这在处理客户端时可能会比较棘手,当然效率也较低,但同样可能会给您带来预期的行为.

This can be trickier to handle client-side, and is of course less efficient, but again might get you the expected behavior.

这篇关于如何在graphql解析器中同时返回错误和数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆