Python 变异错误,未知参数\“;条形码\“;关于字段\“;createProduct\";类型\";突变\";-德扬戈
我在网上做教程,试图让Python 变异错误,未知参数\“;条形码\“;关于字段\“;createProduct\";类型\";突变\";-德扬戈,python,django,graphql,graphene-python,Python,Django,Graphql,Graphene Python,我在网上做教程,试图让变异在图形ql上工作,但我不断地出错,我不知道真正的错误来自哪里,也不知道如何开始调试我做错的地方 在youtube上寻找突变 和石墨烯文档 我注意到,由于石墨烯版本不同,这就是为什么我要阅读文档,而不是完全按照youtube 我设置了一些东西,但无法让它工作,当我执行变异查询时,我得到了错误 我有一个这样的模型 class Product(models.Model): sku = models.CharField(max_length=13, help_text=
变异
在图形ql
上工作,但我不断地出错,我不知道真正的错误来自哪里,也不知道如何开始调试我做错的地方
在youtube上寻找突变
和石墨烯文档
我注意到,由于石墨烯版本不同,这就是为什么我要阅读文档,而不是完全按照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
我的产品有这个模式
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
}
]
}
]
}
谁能帮我一下我该怎么做
提前感谢您的帮助解决了我自己的问题 有三件事,它们是
Argument
应该是Arguments
并且在mutate
函数下,我应该使用常规的django创建模型,以便从product=product(barcode=barcode)
到product=product.objects.create(barcode=barcode)
最后但并非最不重要的class-ProductMutation(graphene.AbstractType):
应该是类product(graphene.ObjectType):
所以代码应该是
class ProductType(DjangoObjectType):
class Meta:
model = Product
filter_fields = {'description': ['icontains']}
interfaces = (graphene.relay.Node,)
class CreateProduct(graphene.Mutation):
class Arguments: # change here
barcode = graphene.String()
product = graphene.Field(lambda: ProductType)
def mutate(self, info, barcode):
# change here
# somehow the graphene documentation just state the code I had in my question which doesn't work for me. But this one does
product = Product.objects.create(barcode=barcode)
return CreateProduct(product=product)
class ProductMutation(graphene.ObjectType): # change here
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()
我解决了自己的问题 有三件事,它们是
Argument
应该是Arguments
并且在mutate
函数下,我应该使用常规的django创建模型,以便从product=product(barcode=barcode)
到product=product.objects.create(barcode=barcode)
最后但并非最不重要的class-ProductMutation(graphene.AbstractType):
应该是类product(graphene.ObjectType):
所以代码应该是
class ProductType(DjangoObjectType):
class Meta:
model = Product
filter_fields = {'description': ['icontains']}
interfaces = (graphene.relay.Node,)
class CreateProduct(graphene.Mutation):
class Arguments: # change here
barcode = graphene.String()
product = graphene.Field(lambda: ProductType)
def mutate(self, info, barcode):
# change here
# somehow the graphene documentation just state the code I had in my question which doesn't work for me. But this one does
product = Product.objects.create(barcode=barcode)
return CreateProduct(product=product)
class ProductMutation(graphene.ObjectType): # change here
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()