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

查看:32
本文介绍了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天全站免登陆