Как я могу не вызывать ошибку при мутациях с помощью graphene-django-cud?

Я использую graphene-django-cud для мутаций. Но я не могу поднять какой-либо GraphQLError, ValueError или Exception в мутациях. Например, в before_mutate() или в любом методе validate_. Процесс просто останавливается без сообщения об ошибке. Затем возвращается null для экземпляра и сообщения.

@classmethod
def before_mutate(cls, root, info, input, id):
    print("before_mutate")
    from graphql import GraphQLError
    raise GraphQLError(f"The observation with id {id} doesn't exists")

@classmethod
def validate_name(cls, root, info, value, input, id, obj):
    print("validate_name")
    raise ValueError(f"The observation with id {id} doesn't existssss")

Кто-нибудь встречался с этим раньше? Заранее спасибо!

Теперь я понял, что происходит.

Это не из graphene-django-cud, это из графена. Мне нужно добавить try/except, чтобы перехватить ошибку, а затем вернуть GraphQLError.

from graphene import Field
from graphene_django_cud.mutations import DjangoCreateMutation
from graphql import GraphQLError
from .models import Demo
from .types import DemoType
import logging
import traceback

class DemoCreateMutation(DjangoCreateMutation):
    demo = Field(DemoType)

    class Meta:
        model = Demo

    @classmethod
    def mutate(cls, root, info, input):
        try:
            return super().mutate(root, info, input)
        except Exception as e:
            logging.error(str(e))
            return GraphQLError(traceback.format_exc())

    @classmethod
    def before_mutate(cls, root, info, input, id):
        raise GraphQLError(f"The Demo {id} doesn't exists")

    @classmethod
    def validate_name(cls, root, info, value, input, id, obj):
        raise ValueError(f"The Demo {id} doesn't existssss")
Вернуться на верх