GraphQL - 是否有可能设置一个变量结果为突变
问题描述:
我想在我的GraphQL查询中做2个创建。 (我知道我的查询结构是不正确的,但它说明我的问题)GraphQL - 是否有可能设置一个变量结果为突变
mutation {
affiliateCreate(company: "test mutation") {
$id: id,
affiliateUserCreate(affiliate_id: $id, name: "test name") {
id,
name
},
company
}
}
我想我的第一个ID结果是在可变谁我传递给第二个创建调用?我对GraphQL非常陌生,我想知道是否有可能。
有没有其他办法可以做这样的事情?或者我必须做2突变呼叫?第一个是affiliateCreate
,第二个呢?
谢谢
答
你想做的事情不被GraphQL支持。在Graphcool API中,我们用我们所谓的嵌套变异来处理这种情况。我也听说它被称为复杂的突变。
嵌套创建突变的特征是嵌套的输入对象参数。如果添加输入对象author
到affiliateCreate
突变,你可以使用它像:
mutation createAffiliateAndUser {
affiliateCreate(
company: "test company"
author: {
name: "test user"
}
) {
id
}
}
这将创建一个联盟,一个用户,然后链接两个在一起。相若方式,如果添加输入对象affiliates
到userCreate
突变,它可能是这样的:
mutation createUserAndAffiliates {
userCreate(
name: "test user"
affiliates: [{
company: "first company"
}, {
company: "second company"
}]
) {
id
}
}
了解更多关于嵌套突变in this article。
我想,在你的解决方法的回调中,你传递用户ID来创建关联公司? –