简体   繁体   English

GraphQL 突变的响应应该是什么?

[英]What should the response from a GraphQL mutation be?

I'm writing a GraphQL server and am wondering what the response should be from a mutation?我正在编写一个 GraphQL 服务器并且想知道突变应该是什么响应? For queries, it's obvious: the response consists of what was requested.对于查询,很明显:响应由请求的内容组成。 For a mutation, however, what should the response be or look like?然而,对于突变,响应应该是什么或看起来像什么? Should the response consists of just a String message letting the consumer know whether the mutation succeeded without errors?响应是否应该只包含一个 String 消息,让消费者知道突变是否成功且没有错误? Or should the response be the object they mutated?或者响应应该是他们变异的对象?

GraphQL doesn't force this one way or the other, but popular convention is to return the object . GraphQL 不会以一种或另一种方式强制这样做,但流行的约定是返回 object

With REST, the understanding is generally that the object which was mutated should be returned as the new state the object is in. This convention has carried over to GraphQL as well, so I would say in most cases you want to return the object.对于 REST,人们的理解通常是被改变的对象应该作为对象所处的新状态返回。这个约定也延续到了 GraphQL,所以我会说在大多数情况下你想要返回对象。

The GraphQL documentation appears to reinforce this as well: GraphQL 文档似乎也加强了这一点:

type Mutation {
  createMessage(input: MessageInput): Message
  updateMessage(id: ID!, input: MessageInput): Message
}

Where in this case createMessage and updateMessage take a MessageInput and return the updated or new Message object.在这种情况下createMessageupdateMessage采用MessageInput并返回更新的或新的Message对象。

This also has key practical benefits!这也有关键的实际好处! For popular GraphQL frameworks like Apollo, returning the result means that Apollo will automatically integrate the updated result into its cache for faster retrieval and rendering later.对于像 Apollo 这样流行的 GraphQL 框架,返回结果意味着 Apollo 会自动将更新的结果集成到其缓存中,以便以后更快地检索和渲染。

Are there situations where you don't want to do this?是否存在您不想这样做的情况? Definitely.确实。 If you are deleting an object, you might want to return only a more simple return type such as the ID or a string that identifies the action was completed.如果您要删除一个对象,您可能只想返回更简单的返回类型,例如 ID 或标识操作已完成的字符串。

There are also some operations that don't have a natural object to return, as they don't intuitively tie into a CRUD operation ( promoteUser for example).还有一些操作没有要返回的自然对象,因为它们不直观地绑定到 CRUD 操作(例如, promoteUser )。 In REST, it often requires wrapping the operation into an abstract object (such as POST /promoteUserRequest ) to be technically idiomatic, but in GraphQL you can be more flexible (it'd be valid to return the user or also valid to return just a string confirmation).在 REST 中,它通常需要将操作包装到一个抽象对象(例如POST /promoteUserRequest )中才能在技术上惯用,但在 GraphQL 中,您可以更加灵活(返回用户是有效的,或者仅返回一个字符串确认)。

There is also other belief camps, which advocate this sort of design:还有其他信仰阵营,提倡这种设计:

type RootMutation {
  createTodo(input: CreateTodoInput!): CreateTodoPayload
  toggleTodoCompleted(input: ToggleTodoCompletedInput!): ToggleTodoCompletedPayload
  updateTodoText(input: UpdateTodoTextInput!): UpdateTodoTextPayload
  completeAllTodos(input: CompleteAllTodosInput!): CompleteAllTodosPayload
}

In this example from the Apollo blog , it doesn't return a generic Todo type but instead returns a custom type that's specific to each mutation.Apollo 博客的这个示例中,它不返回通用Todo类型,而是返回特定于每个突变的自定义类型。 Some people believe this 1-to-1-to-1 mapping of mutation-input-response is a better approach as it lets you edit individual mutations without worrying about effecting the return / input values of other resolvers.有些人认为这种突变-输入-响应的 1 对 1 对 1 映射是一种更好的方法,因为它可以让您编辑单个突变,而不必担心影响其他解析器的返回/输入值。 This has also been used for Queries sometimes.这有时也用于查询。

Lastly, keep in mind another GraphQL benefit here: regardless of what you decide to return, the client only has to retrieve it from the result if it makes sense for it to.最后,请记住 GraphQL 的另一个好处:无论您决定返回什么,客户端只需要从结果中检索它,如果它有意义。 The client is free to perform the mutation and request all, some, or none of the response type.客户端可以自由地执行更改并请求所有、部分或不请求响应类型。 For comparison, under REST, you are generally forced to receive the whole response.为了比较,在 REST 下,您通常被迫接收整个响应。

Overall, I'd say it's a lot more common to return the object, but there is no hard-and-fast rule so you can pick what is ideal for your API.总的来说,我认为返回对象更为常见,但没有硬性规定,因此您可以选择最适合您的 API 的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM