我怎么能不提出错误的突变与石墨烯django?

5uzkadbs  于 2023-02-17  发布在  Go
关注(0)|答案(1)|浏览(113)

我使用graphene-django-cud来处理变异,但是我不能在变异中引发任何GraphQLError,ValueError或者Exception,就像before_mutate()或者任何validate_ method一样,这个过程只是停止了,没有任何错误消息。然后为示例和消息返回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")

有人以前遇到过这个吗?提前感谢!

8fsztsew

8fsztsew1#

现在我知道怎么回事了。
它不是来自graphene-django-cud,而是来自graphene,我必须添加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")

相关问题