【问题标题】:How can I can not raise error in mutations with graphene django?我怎样才能不引发石墨烯 django 突变的错误?
【发布时间】:2023-02-17 01:32:35
【问题描述】:

我正在使用 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")

有人见过这个吗?提前致谢!

【问题讨论】:

标签: django graphql graphene-django graphql-mutation


【解决方案1】:

现在我明白是怎么回事了。

它不是来自 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")

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2017-03-18
    • 2022-10-25
    • 2021-03-11
    • 2018-12-06
    • 2022-01-17
    • 2020-01-08
    • 2018-01-11
    • 2017-02-26
    相关资源
    最近更新 更多