question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to define an Enum Input in mutation?

See original GitHub issue

In models.py, I defined a python Enum class:

class EmployeeKind(PyEnum):
    contractor = 1
    fulltime = 2
    parttime = 3

and in schema.py, I defined a mutation class:

class UpdateEmployee(graphene.Mutation):
    class Arguments:
        id = graphene.ID(required=True)
        name = graphene.String()
        department_id = graphene.ID()
        role_id = graphene.ID()
        kind = graphene.Enum.from_enum(EmployeeKind)
...

But when running the script, it shows an error:

Traceback (most recent call last):
  File "....../gqlsql/./app.py", line 5, in <module>
    from schema import schema
  File "....../gqlsql/schema.py", line 181, in <module>
    class Mutation(graphene.ObjectType):
  File "....../gqlsql/schema.py", line 183, in Mutation
    update_employee = UpdateEmployee.Field()
  File "....../gqlsql/.venv/lib/python3.9/site-packages/graphene/types/mutation.py", line 139, in Field
    return Field(
  File "....../gqlsql/.venv/lib/python3.9/site-packages/graphene/types/field.py", line 103, in __init__
    self.args = to_arguments(args or OrderedDict(), extra_args)
  File "....../gqlsql/.venv/lib/python3.9/site-packages/graphene/types/argument.py", line 104, in to_arguments
    raise ValueError('Unknown argument "{}".'.format(default_name))
ValueError: Unknown argument "kind".

What does this error mean? Why?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
chrisberkscommented, Jul 3, 2021

graphene.Enum.from_enum(EmployeeKind) returns a type object which you then need to call.

This:

class EmployeeKindEnum(PyEnum):
    contractor = 1
    fulltime = 2
    parttime = 3

EmployeeKind = graphene.Enum.from_enum(EmployeeKindEnum)

is essentially a dynamic form of:

class EmployeeKind(graphene.Enum):
    contractor = 1
    fulltime = 2
    parttime = 3

except the first is based on an existing Python Enum.

Example:

# models.py
import enum

class EmployeeKind(enum.Enum):
    contractor = 1
    fulltime = 2
    parttime = 3
# schema.py
import graphene

from models import EmployeeKind as EmployeeKindEnum

EmployeeKind = graphene.Enum.from_enum(EmployeeKindEnum)

class UpdateEmployee(graphene.Mutation):
    class Arguments:
        id = graphene.ID(required=True)
        name = graphene.String()
        department_id = graphene.ID()
        role_id = graphene.ID()
        kind = EmployeeKind(required=True)
2reactions
chrisberkscommented, Jul 6, 2021

Thanks @shaozi!

You’re right about the Enums. Graphene v3 has better Enum support, resolvers will receive the Enum member directly rather than the value—as you suggest.

Graphene v3 is still in beta though, Graphene-SQLAlchemy is using v2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL - Passing an enum value directly to a mutation as ...
use the enum variable name as defined in the mutation and send it to Graphql, like I have used gender As $gender: Gender!...
Read more >
How to use GraphQL enum and its best practices
The enum class allows us to map enum values to internal values represented by integers (or different strings etc.). By defining enumerated ...
Read more >
The enum type | Side Quest: Intermediate Schema Design
An enum is a GraphQL schema type that represents a predefined list of possible values. For example, in Airlock, listings can be a...
Read more >
How to use GraphQL enum and its best practices
In GraphQL, Enum type can be used as input and output type. Enums are a great way to add additional validation constraints. We...
Read more >
What you need to know about GraphQL enums
How to use enums ... GraphQL enums can be defined using the enum keyword. ... According to the official specification, you should name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found