0

我正在网上做教程并尝试继续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
        }
      ]
    }
  ]
}

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

提前感谢您的帮助

4

1 回答 1

2

Figured my own problem.

There are three things, which are Argument should be Arguments and under the mutate function, I should use a regular django create model so from product = Product(barcode=barcode) into product = Product.objects.create(barcode=barcode) last but not least class ProductMutation(graphene.AbstractType): should be class ProductMutation(graphene.ObjectType):

so the code should be

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()
于 2017-12-02T21:47:31.217 回答