首页 文章

变异错误,类型为“Mutation”的字段\“createProduct \”上的未知参数\“barcode \” - django

提问于
浏览
0

我正在网上做教程并试图使 mutationgraphql 上工作,但我一直在收到错误,我不知道真正的错误来自哪里以及如何开始调试我做错了 .

看着这个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
        }
      ]
    }
  ]
}

有人可以帮我看看我应该尝试做些什么吗?

在此先感谢您的帮助

1 回答

  • 1

    想出了我自己的问题 .

    有三件事, 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()
    

相关问题