我正在学习django-graphene
用于 graphql 目的。
对于突变,我所知道的是它会返回自己的错误消息。假设我有一个令牌字段,并检查令牌字段是否错误,我只知道如何为return None
前端提供查询结果,null
而不是针对状态和错误的自定义 json 响应
我有这些代码
class ProductType(DjangoObjectType):
class Meta:
model = Product
filter_fields = {'description': ['icontains']}
interfaces = (graphene.relay.Node,)
class ProductInput(graphene.InputObjectType):
token = graphene.String()
title = graphene.String()
barcode = graphene.String(required=True)
class CreateProduct(graphene.Mutation):
class Arguments:
product_input = ProductInput()
product = graphene.Field(ProductType)
def mutate(self, info, product_input=None):
if not product_input.get('token'):
return None # instead of return None, I would like to return error code, status with message
product = Product.objects.create(barcode=product_input.barcode, title=product_input.title)
return CreateProduct(product=product)
class ProductMutation(graphene.ObjectType):
create_product = CreateProduct.Field()
提前致谢