在阅读Apollo Server文档时,推荐使用突变响应接口(interface)进行突变:
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.
https://www.apollographql.com/docs/apollo-server/essentials/schema.html
我理解接口(interface)对于具有事件接口(interface)的用例的好处,然后您有不同类型的事件,例如音乐会、 session 等。我的理解是接口(interface)可以让您例如,使用单个查询搜索所有事件,返回多种类型的事件。
我很困惑为什么应该将接口(interface)用于突变响应以及与标准响应类型相比有什么好处?
最佳答案
与联合体一样,接口(interface)是一种抽象类型,它允许一个字段返回多种类型之一。来自规范:
Fields which yield an interface are useful when one of many Object types are expected, but some fields should be guaranteed.
但是,接口(interface)还强制实现类型具有一组特定的字段和参数。具体规则可见here在规范中,但归结为:
- 如果接口(interface)有字段,则实现类型也必须有该字段
- 实现类型还必须对这些字段中的任何一个具有相同的参数(比如字段,它可以添加额外的参数,但必须至少实现与接口(interface)相同的参数)
- 这些必填字段和参数的类型必须与接口(interface)相匹配
- 如果必填字段或接口(interface)的类型为 Non-Null,则它在实现类型上也必须为 Non-Null(尽管反之则不然)
通过创建一个接口(interface)并让多个类型实现它,您可以有效地创建一个安全网,帮助您确保相关类型之间的结构一致。假设我们按照 Apollo 文档中的建议实现了一些没有接口(interface)的响应类型:
type UpdateUserMutationResponse {
code: String!
success: Boolean!
message: String!
user: User
}
type UpdatePostMutationResponse {
code: String!
success: Boolean!
message: String
post: Post
}
乍一看,这些类型是按照我们的预期定义的——我们有code
、success
和message
字段,以及任何其他字段与该响应相关。但是,我们有一个类型,不小心使 UpdatePostMutationResponse
上的 message
字段可为空。虽然这可能是无害的,但如果我们确实碰巧在我们的解析器中忽略了消息,它可能会被忽视直到稍后(希望在 QA 期间,但可能在生产中!)。
但是,如果我们让这些类型实现一个 MutationResponse
接口(interface),我们就可以确保即使存在任何不一致,我们的架构也不会构建。
这样,即使我们从不使用 MutationResponse
作为字段的返回类型,我们仍然可以从使用接口(interface)中受益。
关于javascript - 为什么我应该为 GraphQL 突变响应类型使用接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54845724/