[英]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:
确切的规则可以发现这里的规范,但它归结为:
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. 乍看之下,这些类型是按照我们的意图定义的 - 我们有
code
, success
和message
字段,以及与该响应相关的任何其他字段。 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.