graphql 是否允许有条件执行突变中的步骤?

Mon*_*key 5 graphql

由于您可以在突变中顺序运行多个步骤,因此可以在 graphql 中使用条件 if 语句,以便仅在前一步的结果满足条件时才运行后面的步骤吗?

例如

 mutation upsertLogin($idToken: String!, $email: String!, $username: String!) {

    User(email: $email, username: $username) {
      id
    }

    // only do the next step if no id from from previous step

    createUser(email: $email, username: $username) {
      id
    }

  }
Run Code Online (Sandbox Code Playgroud)

hel*_*fer 3

不,GraphQL 不支持这种条件执行。唯一明确支持的事情是在一个突变中抛出错误,以防止其余突变运行。这是有效的,因为突变是按顺序运行的。

理论上,您可以通过写入然后读取上下文来实现您提到的条件,该上下文在所有解析器之间共享,但我不建议这样做,因为 GraphQL 的某些实现专门声明上下文是不可变的。