[英]graphql mutation response type
We have a lot of mutations in our service that all of those return the same type of response like there: 我们的服务存在很多变异,所有这些变异都返回相同的响应类型,如下所示:
export default new GraphQLObjectType({
name: 'MessageResponse',
fields: () => ({
message: {
type: GraphQLString,
},
}),
});
My question is could I return this type for all of these mutations as a best practice or I have to create single type for each of those mutations? 我的问题是,作为最佳实践,我是否可以针对所有这些突变返回此类型,或者我必须为每个突变创建单个类型?
The only reason you would want to have different types in this scenario is if you needed it implement a different resolve
function for your message field. 在这种情况下,您想要具有不同类型的唯一原因是,如果您需要它为您的消息字段实现不同的resolve
功能。 If that's not the case, then it's perfectly fine to use the same type. 如果不是这种情况,那么使用相同的类型就可以了。
Also, bear in mind that it's also possible to just return a scalar at root level. 另外,请记住,也可以只返回根级别的标量。 For instance, your Mutation type can look like this: 例如,您的Mutation类型可能如下所示:
new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
someMutation: {
type: GraphQLString,
},
}),
})
This isn't always a good idea (for example, if you anticipate need to return additional information in the future). 这并不总是一个好主意(例如,如果您预计将来需要返回其他信息)。 However, it's worth pointing out that it is possible. 但是,值得指出的是有可能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.