我正在网上做教程,并尝试让mutation
在graphql
上运行,但我一直在收到错误,我不知道真正的错误来自何处以及如何开始调试做错了。
看着这个youtube突变 https://www.youtube.com/watch?v=aB6c7UUMrPo&t=1962s 和石墨烯文件http://docs.graphene-python.org/en/latest/types/mutations/
我注意到由于石墨烯版本不同,这就是为什么我阅读文档而不是完全按照youtube
我设置了一些东西,但是当它执行变异查询时我无法正常工作,我得到了错误。
我有这样的模特。
class Product(models.Model):
sku = models.CharField(max_length=13, help_text="Enter Product Stock Keeping Unit", null=True, blank=True)
barcode = models.CharField(max_length=13, help_text="Enter Product Barcode (ISBN, UPC ...)", null=True, blank=True)
title = models.CharField(max_length=200, help_text="Enter Product Title", null=True, blank=True)
description = models.TextField(help_text="Enter Product Description", null=True, blank=True)
unitCost = models.FloatField(help_text="Enter Product Unit Cost", null=True, blank=True)
unit = models.CharField(max_length=10, help_text="Enter Product Unit ", null=True, blank=True)
quantity = models.FloatField(help_text="Enter Product Quantity", null=True, blank=True)
minQuantity = models.FloatField(help_text="Enter Product Min Quantity", null=True, blank=True)
family = models.ForeignKey('Family', null=True, blank=True)
location = models.ForeignKey('Location', null=True, blank=True)
def __str__(self):
return self.title
我的产品schema
class ProductType(DjangoObjectType):
class Meta:
model = Product
filter_fields = {'description': ['icontains']}
interfaces = (graphene.relay.Node,)
class CreateProduct(graphene.Mutation):
class Argument:
barcode = graphene.String()
# form_errors = graphene.String()
product = graphene.Field(lambda: ProductType)
def mutate(self, info, barcode):
product = Product(barcode=barcode)
return CreateProduct(product=product)
class ProductMutation(graphene.AbstractType):
create_product = CreateProduct.Field()
class ProductQuery(object):
product = relay.Node.Field(ProductType)
all_products = DjangoFilterConnectionField(ProductType)
def resolve_all_products(self, info, **kwargs):
return Product.objects.all()
全局架构看起来像这样
class Mutation(ProductMutation,
graphene.ObjectType):
pass
class Query(FamilyQuery,
LocationQuery,
ProductQuery,
TransactionQuery,
graphene.ObjectType):
# This class extends all abstract apps level Queries and graphene.ObjectType
pass
allGraphQLSchema = graphene.Schema(query=Query, mutation=Mutation)
尝试查询...这是我的查询
mutation ProductMutation {
createProduct(barcode:"abc"){
product {
id, unit, description
}
}
}
错误返回
{
"errors": [
{
"message": "Unknown argument \"barcode\" on field \"createProduct\" of type \"Mutation\".",
"locations": [
{
"column": 17,
"line": 2
}
]
}
]
}
有人可以帮我看看我应该尝试做些什么吗?
提前感谢您提供任何帮助
答案 0 :(得分:1)
想出我自己的问题。
有三件事,Argument
应该是Arguments
,在mutate
函数下,我应该使用常规的django创建模型,所以从product = Product(barcode=barcode)
到{{{ 1}}最后但并非最不重要product = Product.objects.create(barcode=barcode)
应为class ProductMutation(graphene.AbstractType):
所以代码应该是
class ProductMutation(graphene.ObjectType):