突变错误,类型为 \"Mutation\" 的字段 \"createProduct\" 上的未知参数 \"barcode\" - django
mutation error, Unknown argument \"barcode\" on field \"createProduct\" of type \"Mutation\" - django
我正在做在线教程并试图让 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
}
]
}
]
}
有人可以告诉我应该尝试做什么吗?
提前感谢您的帮助
想出了我自己的问题。
有三件事,Argument
应该是 Arguments
并且在 mutate
函数下,我应该使用常规的 django 创建模型所以从 product = Product(barcode=barcode)
到product = Product.objects.create(barcode=barcode)
最后但同样重要的是 class ProductMutation(graphene.AbstractType):
应该是 class ProductMutation(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()
我正在做在线教程并试图让 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
}
]
}
]
}
有人可以告诉我应该尝试做什么吗?
提前感谢您的帮助
想出了我自己的问题。
有三件事,Argument
应该是 Arguments
并且在 mutate
函数下,我应该使用常规的 django 创建模型所以从 product = Product(barcode=barcode)
到product = Product.objects.create(barcode=barcode)
最后但同样重要的是 class ProductMutation(graphene.AbstractType):
应该是 class ProductMutation(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()