strawberry-django-plus icon indicating copy to clipboard operation
strawberry-django-plus copied to clipboard

enum validation

Open vocabulista opened this issue 1 year ago • 4 comments

First of all: thank you very much for this library! I have a question regarding how to properly validate an enum.

This is my model:

class Lemma(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid_lib.uuid4, editable=False)
    word = models.CharField(max_length=100)
    language = models.CharField(max_length=3, null=True, blank=True)
    multiword_expression = models.BooleanField(default=False)
    related_lemmas = models.ManyToManyField('self', blank=True)
    related_meanings = models.ManyToManyField(Meaning, blank=True, related_name='lemma_related_meanings')

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

my enum:

@strawberry.enum
class Language(Enum):
    pal = "pal"
    eng = "eng"
    deu = "deu"
    ita = "ita"
    spa = "spa"
    fra = "fra"

and my type:

@gql.django.filters.filter(models.Lemma, lookups=True)
class LemmaFilter:
    id: relay.GlobalID
    word: gql.auto
    language: Language


@gql.django.type(models.Lemma, filters=LemmaFilter)
class Lemma(relay.Node):

    token_lemmas:  relay.Connection[gql.LazyType['Token', 'types.token']]
    comment_lemma: relay.Connection[gql.LazyType['Comment', 'types.comment']]

    id: relay.GlobalID
    word: gql.auto
    language: Language
    related_lemmas: List['Lemma']
    related_meanings: List[gql.LazyType['Meaning', 'types.meaning']]


@gql.django.input(models.Lemma)
class LemmaInput:
    word: gql.auto
    language: Language
    related_lemmas: gql.auto
    related_meanings: gql.auto


@gql.django.partial(models.Lemma)
class LemmaPartial:
    id: relay.GlobalID
    word: gql.auto
    language: Language
    related_lemmas: gql.auto
    related_meanings: gql.auto

This is the query and the error that I see:

image

I do not want to use django_choices_field. How can I avoid then this validation error? Thanks!

vocabulista avatar Feb 16 '23 12:02 vocabulista