我想在我的 GraphQL 查询中创建 2 个。 (我知道我的查询结构不正确,但这是为了说明我的问题)
mutation {
affiliateCreate(company: "test mutation") {
$id: id,
affiliateUserCreate(affiliate_id: $id, name: "test name") {
id,
name
},
company
}
}
我希望我的第一个 id 结果在我传递给第二个创建调用的变量中?我对 GraphQL 很陌生,我想知道是否有可能。
有没有其他方法可以做这样的事情?或者我必须做 2 个突变调用?第一个用
affiliateCreate
在它的后备第二个?谢谢
最佳答案
GraphQL 不支持您要执行的操作。在 Graphcool API 中,我们通过所谓的嵌套突变来处理这种情况。我也听说它被称为复杂突变。
嵌套的 create 突变的特点是嵌套的输入对象参数。如果添加输入对象 author
到 affiliateCreate
突变,你可以这样使用它:
mutation createAffiliateAndUser {
affiliateCreate(
company: "test company"
author: {
name: "test user"
}
) {
id
}
}
这将创建一个成员(member)、一个用户,然后将两者链接在一起。同样,如果你添加一个输入对象 affiliates
到 userCreate
突变,它可能看起来像这样:mutation createUserAndAffiliates {
userCreate(
name: "test user"
affiliates: [{
company: "first company"
}, {
company: "second company"
}]
) {
id
}
}
关于GraphQL - 是否可以设置一个带有突变结果的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44377518/