GraphQL-将枚举值直接传递给突变作为参数吗? [英] GraphQL - Passing an enum value directly to a mutation as an argument?

查看:93
本文介绍了GraphQL-将枚举值直接传递给突变作为参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下GraphQL类型定义:

Given the following GraphQL type definition:

const typeDefs = `
  enum Action {
    update
    delete
  }    

  type Mutation {
    doSomething(action: Action)
  }
`;

此查询有效:

const query = `
  mutation($action: Action) {
    doSomething(action: $action)
  }
`
const variables = { action: "update" }

但是,这不是:

const query = `
  mutation {
    doSomething(action: "update")
  }
`

GraphQL不支持直接将枚举值作为参数传递吗?

Does GraphQL not support passing an enum value directly as an argument?

推荐答案

GraphQL确实支持将枚举直接作为参数传递。但是,您需要省略该值周围的引号才能使其起作用:

GraphQL does support passing the enum directly as an argument; however, you'll need to omit the quotation marks around the value to get it to work:

const query = `
  mutation {
    doSomething(action: update)
  }
`

根据规范


枚举值表示为未加引号的名称(例如MOBILE_WEB)。

Enum values are represented as unquoted names (ex. MOBILE_WEB).

所以与字符串不同值,枚举值不应放在引号内。我们将它们用于变量的唯一原因是遵循正确的JSON格式。

So unlike a string value, an enum value should not be put inside quotation marks. The only reason we do so when using them for variables is to follow proper JSON formatting.

这篇关于GraphQL-将枚举值直接传递给突变作为参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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