GraphQL-是否可以设置变量以产生突变 [英] GraphQL - Is it possible to set a variable with a result for a mutation

查看:35
本文介绍了GraphQL-是否可以设置变量以产生突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在GraphQL查询中进行2次创建.(我知道我的查询结构不正确,但这只是为了说明我的问题)

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
  }
}

我希望我的第一个id结果是变量,我将其传递给第二个创建调用吗?我是GraphQL的新手,我想知道是否有可能.

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.

还有其他方法可以做这种事情吗?还是我必须做2个突变电话?第一个带有 affiliateCreate ,而第二个则回退了吗?

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?

谢谢

推荐答案

GraphQL不支持您要执行的操作.在Graphcool API中,我们使用称为嵌套变异"的方法来处理这种情况.我也听说过它被称为复杂突变.

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.

嵌套的create变异的特征在于嵌套的输入对象参数.如果将输入对象 author 添加到 affiliateCreate 变异中,则可以这样使用它:

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
  }
}

这将创建一个会员,一个用户,然后将两者链接在一起.类似地,如果将输入对象 affiliates 添加到 userCreate 变异中,则它看起来可能像这样:

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
  }
}

这篇关于GraphQL-是否可以设置变量以产生突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆