石墨烯:枚举参数似乎不起作用

Graphene: Enum argument doesn't seem to work

我目前在突变枚举上遇到困难 Argument

下面是我的 Mutation 代码:

class CreatePerson(graphene.Mutation):
    foo = graphene.String()

    def mutate(self, info, **kwargs):
        return CreatePerson(foo='foo')


    class Arguments:
        enum_arg = graphene.Argument(graphene.Enum.from_enum(EnumArg))

枚举class:

from enum import Enum

class EnumArg(Enum):
    Baz = 0
    Bar = 1
    Spam = 2
    Egg = 3

命令使用 POSTMAN:

{
    "query": "mutation": {createPerson(enumArg=1) { foo }}
}

但我最终得到了这个错误信息:

"message": "Argument \"enumArg\" has invalid value 1.
            Expected type \"EnumArg\", found 1.",

我也尝试在 createPerson 突变上给出 enumArg=\"Bar\",但错误仍然存​​在。

定义枚举时,我们可以为枚举中的每个枚举值分配一个任意值。但是,此值仅供 GraphQL 服务本身在内部使用。例如,如果字段参数的类型是枚举,则该值将作为参数值传递给字段的解析器。但是,在编写 GraphQL 文档时,枚举值必须始终用它的名称而不是它的值来引用。

mutation {
  createPerson(enumArg: Bar) {
    foo
  }
}

后端定义的枚举是:

enum Gender {
  MALE
  FEMALE
}

我在前端使用 Vue,因此可以像这样将数据从 Vue 传递到突变。 我在组件的本地状态中将性别定义为字符串:

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

来自 Vue 的方法是:

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

上面使用的突变 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
    }
}
`

使用突变中定义的枚举变量名并将其发送到 Graphql,就像我使用的性别一样 $gender: Gender! 在 gql 突变中。您不必担心将数据作为枚举发送,只需将其作为字符串发送,否则您将不得不面对 JSON 错误,Graphql 将处理您作为字符串发送的值(如 'MALE'或 'FEMALE') 就像我在上面所做的那样,不要忘记在 gql 突变中提及性别是性别类型(枚举)。

请看我在这个linkLink的回答,以供参考