5

Im currently having hard time on mutation enum Argument.

Below are my code for 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))

Enum class:

from enum import Enum

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

Command using POSTMAN:

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

But I end up this error message:

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

I also tried giving enumArg=\"Bar\" on the createPerson mutation and the error still persists.

Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117
  • The `command` is not valid python how are you callling `command`? – Devesh Kumar Singh May 22 '19 at 14:57
  • No it is pretty valid. Check here and im using postman https://stackoverflow.com/a/55146271/6143656 – Shift 'n Tab May 22 '19 at 15:00
  • @MarcoDaniel Its looking good – Shift 'n Tab May 22 '19 at 15:02
  • 1
    Not sure if this is a dupe. The issue here is that we can assign an arbitrary value for each enum value, but this value is only used ***internally*** by the GraphQL service itself. When referring to the enum value inside a GraphQL document, it must always be referenced by its name. – Daniel Rearden May 22 '19 at 15:11
  • When i printed the `kwargs` on the `def mutate()`, the argument `enum_arg` has a value of `1`. Now the consequence problem is im using `flask-sqlalchemy` and the model field is an enum. it cannot accept value `1` as it is giving `Not a valid enum value`. I expected that it should give a ``. – Shift 'n Tab May 22 '19 at 15:16
  • @DanielRearden however it solve the problem of the original error which it cannot even proceed on mutation function. – Shift 'n Tab May 22 '19 at 15:17
  • 1
    @Roel That's probably a good question, unfortunately not one I can answer since I don't have experience with `flask-sqlalchemy`. I would suggest opening a new question. – Daniel Rearden May 22 '19 at 15:22

2 Answers2

12

When defining an enum, we can assign an arbitrary value to each enum value in the enum. However, this value is only used internally by the GraphQL service itself. For example, if the type of a field's argument is the enum, this value will be passed to the field's resolver as the argument value. However, when composing a GraphQL document, the enum value must always be referred to by it's name, not it's value.

mutation {
  createPerson(enumArg: Bar) {
    foo
  }
}
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
1

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. I have defined gender as a string in my local state of the component as:

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

The method from Vue is:

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:

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! 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.

Please read my answer on this link Link for reference

Shivam Bisht
  • 404
  • 1
  • 5
  • 16