如何摆脱变异结果的冗余包装对象?
How get rid of redundant wrapper object of a mutation result?
当我通过这样的突变向我的后端发出请求时:
mutation{
resetPasswordByToken(token:"my-token"){
id
}
}
我收到的回复格式如下:
{
"data": {
"resetPasswordByToken": {
"id": 3
}
}
}
那个与突变同名的包装器对象对我来说似乎有些尴尬(至少是多余的)。有没有办法去掉那个包装器,让返回的结果更干净一些?
这就是我现在定义突变的方式:
export const ResetPasswordByTokenMutation = {
type: UserType,
description: 'Sets a new password and sends an informing email with the password generated',
args: {
token: { type: new GraphQLNonNull(GraphQLString) },
captcha: { type: GraphQLString },
},
resolve: async (root, args, request) => {
const ip = getRequestIp(request);
const user = await Auth.resetPasswordByToken(ip, args);
return user.toJSON();
}
};
一句话:没有.
resetPasswordByToken
不是 "wrapper object",而只是您在架构中定义的解析为对象(在本例中为 UserType
)的字段。虽然一次只请求 mutation
类型的一个字段很常见,但可以请求任意数量的字段:
mutation {
resetPasswordByToken(token:"my-token"){
id
}
someOtherMutation {
# some fields here
}
andYetAnotherMutation {
# some other fields here
}
}
如果我们像您建议的那样扁平化响应的结构,我们将无法区分一个突变返回的数据与另一个突变返回的数据。我们同样需要将所有这些嵌套在 data
中,以使我们的实际数据与任何返回的错误(出现在单独的 errors
条目中)分开。
当我通过这样的突变向我的后端发出请求时:
mutation{
resetPasswordByToken(token:"my-token"){
id
}
}
我收到的回复格式如下:
{
"data": {
"resetPasswordByToken": {
"id": 3
}
}
}
那个与突变同名的包装器对象对我来说似乎有些尴尬(至少是多余的)。有没有办法去掉那个包装器,让返回的结果更干净一些?
这就是我现在定义突变的方式:
export const ResetPasswordByTokenMutation = {
type: UserType,
description: 'Sets a new password and sends an informing email with the password generated',
args: {
token: { type: new GraphQLNonNull(GraphQLString) },
captcha: { type: GraphQLString },
},
resolve: async (root, args, request) => {
const ip = getRequestIp(request);
const user = await Auth.resetPasswordByToken(ip, args);
return user.toJSON();
}
};
一句话:没有.
resetPasswordByToken
不是 "wrapper object",而只是您在架构中定义的解析为对象(在本例中为 UserType
)的字段。虽然一次只请求 mutation
类型的一个字段很常见,但可以请求任意数量的字段:
mutation {
resetPasswordByToken(token:"my-token"){
id
}
someOtherMutation {
# some fields here
}
andYetAnotherMutation {
# some other fields here
}
}
如果我们像您建议的那样扁平化响应的结构,我们将无法区分一个突变返回的数据与另一个突变返回的数据。我们同样需要将所有这些嵌套在 data
中,以使我们的实际数据与任何返回的错误(出现在单独的 errors
条目中)分开。