简体   繁体   English

GraphQL - 将枚举值作为参数直接传递给突变?

[英]GraphQL - Passing an enum value directly to a mutation as an argument?

Given the following GraphQL type definition:给定以下 GraphQL 类型定义:

const typeDefs = `
  enum Action {
    update
    delete
  }    

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

This query works:此查询有效:

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

But this one does not:但这个没有:

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

Does GraphQL not support passing an enum value directly as an argument? GraphQL 不支持直接将枚举值作为参数传递吗?

GraphQL does support passing the enum directly as an argument; GraphQL 确实支持将枚举作为参数直接传递; however, you'll need to omit the quotation marks around the value to get it to work:但是,您需要省略值周围的引号才能使其工作:

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

According to the spec :根据规范

Enum values are represented as unquoted names (ex. MOBILE_WEB).枚举值表示为不带引号的名称(例如 MOBILE_WEB)。

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.我们在将它们用于变量时这样做的唯一原因是遵循正确的 JSON 格式。

enum defined in backend is:后端定义的枚举是:

enum Gender {
  MALE
  FEMALE
}

I am using Vue for frontend so passing data to the mutation from Vue can be done like this.我正在使用 Vue 作为前端,因此可以像这样将数据从 Vue 传递到突变。 I have defined gender as a string in my local state of the component as:我已将性别定义为组件本地状态中的字符串:

data(){
  return {
     gender: ''
  }
}

The method from Vue is: Vue 的方法是:

async handleEditProfile () {
      const response = await this.$apollo.mutate({
        query: EDIT_PROFILE,
        variables: {
          nameAsInPan: this.nameAsInPan,
          gender: this.gender,
          dateOfBirth: this.dateOfBirth
        }
      })
    }

mutation used above EDIT_PROFILE:上面使用的突变 EDIT_PROFILE:

gql`mutation editProfile($name: String!, $email: String!,$phone: String!, $gender: Gender!, $dateOfBirth: String!) {
    editProfile (profileInput:{name: $name, email: $email, phone: $phone, gender: $gender, dateOfBirth: $dateOfBirth}){
      id
      email
      phone
      firstName
      lastName
      nameAsInPan
      gender
      dateOfBirth
    }
}
`

use the enum variable name as defined in the mutation and send it to Graphql, like I have used gender As $gender: Gender!使用突变中定义的枚举变量名称并将其发送到 Graphql,就像我使用过性别一样$gender: Gender! in gql mutation.You don't have to worry about sending data as enum, just send it as String otherwise you will have to face JSON error, Graphql will take care of the value you send as a string (like 'MALE' or 'FEMALE') just don't forget to mention that gender is type of Gender(which is enum) in gql mutation as I did above.在 gql 突变中。您不必担心将数据作为枚举发送,只需将其作为字符串发送,否则您将不得不面对 JSON 错误,Graphql 将处理您作为字符串发送的值(如 'MALE' 或 ' FEMALE') 只是不要忘记提到性别是 gql 突变中的性别类型(即枚举),就像我上面所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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