简体   繁体   English

GraphQL - 是否可以设置一个带有突变结果的变量

[英]GraphQL - Is it possible to set a variable with a result for a mutation

I want to do 2 creations in my GraphQL query.我想在我的 GraphQL 查询中创建 2 个。 (I know my query structure is not correct, but it's to illustrate my question) (我知道我的查询结构不正确,但这是为了说明我的问题)

mutation {
    affiliateCreate(company: "test mutation") {
    $id: id,
    affiliateUserCreate(affiliate_id: $id, name: "test name") {
      id, 
      name
    },
    company
  }
}

I want my first id result to be in variable who i pass to the second creation call?我希望我的第一个 id 结果在我传递给第二个创建调用的变量中? I'm very new to GraphQL and i was wondering if it's possible.我对 GraphQL 很陌生,我想知道是否有可能。

Is there any other way possible to do such thing?有没有其他方法可以做这样的事情? Or i must do 2 mutation call?或者我必须做 2 个突变调用? The first with affiliateCreate and in it's fallback the second one?第一个与affiliateCreate并在它的后备第二个?

Thank you谢谢

What you want to do is not supported by GraphQL. GraphQL 不支持您要执行的操作。 In the Graphcool APIs we approach this kind of situation with what we call nested mutations .在 Graphcool API 中,我们通过所谓的嵌套突变来处理这种情况。 I've also heard it being referred to as complex mutations .我也听说它被称为复杂突变

A nested create mutation is characterized by a nested input object argument .嵌套的 create 突变的特点是嵌套的输入对象参数 If you add an input object author to the affiliateCreate mutation, you could use it like that:如果您将输入对象author添加到affiliateCreate突变,您可以像这样使用它:

mutation createAffiliateAndUser {
  affiliateCreate(
    company: "test company"
    author: {
      name: "test user"
    }
  ) {
    id
  }
}

This would create an affiliate, a user and then link the two together.这将创建一个会员、一个用户,然后将两者链接在一起。 Similarily, if you add an input object affiliates to the userCreate mutation, it could look like this:类似地,如果您向userCreate突变添加一个输入对象affiliates ,它可能如下所示:

mutation createUserAndAffiliates {
  userCreate(
    name: "test user"
    affiliates: [{
      company: "first company"
    }, {
      company: "second company"
    }]
  ) {
    id
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM