简体   繁体   中英

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

I want to do 2 creations in my GraphQL query. (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? I'm very new to GraphQL and i was wondering if it's possible.

Is there any other way possible to do such thing? Or i must do 2 mutation call? The first with affiliateCreate and in it's fallback the second one?

Thank you

What you want to do is not supported by GraphQL. In the Graphcool APIs we approach this kind of situation with what we call nested mutations . I've also heard it being referred to as complex mutations .

A nested create mutation is characterized by a nested input object argument . If you add an input object author to the affiliateCreate mutation, you could use it like that:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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