简体   繁体   English

为什么我应该使用GraphQL突变响应类型的接口?

[英]Why should I use an interface for a GraphQL mutation response type?

In reading the Apollo Server documentation, it recommends using a mutation response interface for mutations: 在阅读Apollo Server文档时,它建议使用突变响应接口进行突变:

In order to provide consistency across a schema, we suggest introducing a MutationResponse interface which can be implemented on every mutation response in a schema and enables transactional information to be returned in addition to the normal mutation response object. 为了在模式中提供一致性,我们建议引入MutationResponse接口, MutationResponse接口可以在模式中的每个变异响应上实现,并且除了正常的变异响应对象之外还能够返回事务信息。

https://www.apollographql.com/docs/apollo-server/essentials/schema.html https://www.apollographql.com/docs/apollo-server/essentials/schema.html

I understand the benefit of interfaces for use cases where you have an events interface and then you have different types of events such as a concert, conference, etc. My understanding is an interface would allow you to search all events with a single query for example, returning multiple types of events. 我了解接口的好处,你有一个事件界面,然后你有不同类型的事件,如音乐会,会议等。我的理解是一个接口将允许你用一个查询搜索所有事件,例如,返回多种类型的事件。

I am confused why an interface should be used for a mutation response and what the benefits would be over a standard response type? 我很困惑为什么界面应该用于突变响应以及对标准响应类型有什么好处?

Like a Union, an Interface is an abstract type that allows a field to return one of multiple types. 与Union类似,Interface是一种抽象类型,允许字段返回多种类型之一。 From the spec: 从规格:

Fields which yield an interface are useful when one of many Object types are expected, but some fields should be guaranteed. 当需要许多Object类型之一时,产生接口的字段很有用,但是应该保证一些字段。

However, interfaces also force implementing types to have a specific set of fields and arguments. 但是,接口还强制实现类型具有一组特定的字段和参数。 The exact rules can be found here in the spec, but it boils down to: 确切的规则可以发现这里的规范,但它归结为:

  • If the interface has a field, the implementing type must also have that field 如果接口具有字段,则实现类型还必须具有该字段
  • The implementing type must also have the same arguments for any of these fields (like fields, it can add additional ones, but must at least implement the same ones as the interface) 对于任何这些字段,实现类型也必须具有相同的参数(如字段,它可以添加其他字段,但必须至少实现与接口相同的字段)
  • The types of these required fields and arguments must match the interface 这些必填字段和参数的类型必须与接口匹配
  • If the type of a required field or interface is Non-Null, it must also be Non-Null on the implementing type (although the reverse is not true) 如果必需字段或接口的类型是非空,则它在实现类型上也必须是非空的(尽管反之亦然)

By creating an Interface and having multiple types implement it, you effectively create a safety net that will help you ensure a consistent structure across related types. 通过创建一个接口并使多种类型实现它,您可以有效地创建一个安全网,帮助您确保跨相关类型的一致结构。 Let's assume we implement some response types as suggested in the Apollo docs, without an interface: 假设我们实现了Apollo文档中建议的一些响应类型,没有接口:

type UpdateUserMutationResponse {
  code: String!
  success: Boolean!
  message: String!
  user: User
}

type UpdatePostMutationResponse {
  code: String!
  success: Boolean!
  message: String
  post: Post
}

At a glance, these types are defined as we intended -- we have fields for code , success and message , along with whatever other fields are relevant for that response. 乍看之下,这些类型是按照我们的意图定义的 - 我们有codesuccessmessage字段,以及与该响应相关的任何其他字段。 However, we have a type and accidentally made the message field on UpdatePostMutationResponse nullable. 但是,我们有一个类型,并且意外地使UpdatePostMutationResponse上的message字段可以UpdatePostMutationResponse空。 While this may be harmless, if we do happen to omit the message in our resolver, it may go unnoticed until way later (hopefully during QA, but maybe in production!). 虽然这可能是无害的,如果我们确实在我们的解析器中省略了消息,它可能会被忽视,直到稍后(希望在QA期间,但可能在生产中!)。

If we have these types implement a MutationResponse interface, though, we can ensure that our schema won't even build if there's any inconsistencies like that. 但是,如果我们有这些类型实现MutationResponse接口,我们可以确保如果存在任何不一致的情况,我们的架构甚至都不会构建。

In this way, even if we never utilize MutationResponse as a return type for a field, we can still benefit from utilizing an Interface. 这样,即使我们从不使用MutationResponse作为字段的返回类型,我们仍然可以从使用接口中受益。

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

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