[英]Handling ForeignKey in GraphQL (graphene)
I'm using Django-Graphene to build and API for my webapp.我正在使用 Django-Graphene 为我的 webapp 构建和 API。 I've built this simple structure where I have Product
that belong to a category
:我已经建立了这个简单的结构,其中我有属于某个category
的Product
:
from graphene_django import DjangoObjectType
import graphene
from .models import ProductModel, CategoryModel
class CategoryType(DjangoObjectType):
class Meta:
model = CategoryModel
class ProductType(DjangoObjectType):
category = graphene.Field(CategoryType)
class Meta:
model = ProductModel
class CategoryInput(graphene.InputObjectType):
title = graphene.String()
class ProductInput(graphene.InputObjectType):
title = graphene.String()
category = graphene.Field(CategoryInput)
class Query(graphene.ObjectType):
products = graphene.List(ProductType)
categories = graphene.List(CategoryType)
def resolve_products(self, info):
return ProductModel.objects.all()
def resolve_categories(self, info):
return CategoryModel.objects.all()
class CreateProduct(graphene.Mutation):
class Arguments:
product_data = ProductInput(required=True)
product = graphene.Field(ProductType)
@staticmethod
def mutate(self, info, product_data):
product = ProductModel.objects.create(**product_data)
product.save()
return CreateProduct(product=product)
class CreateCategory(graphene.Mutation):
class Arguments:
title = graphene.String()
category = graphene.Field(CategoryType)
def mutate(self, info, title):
category = CategoryModel(title=title)
category.save()
return CreateCategory(category=category)
class Mutations(graphene.ObjectType):
create_product = CreateProduct.Field()
create_category = CreateCategory.Field()
schema = graphene.Schema(query=Query, mutation=Mutations)
But when I try to mutate, I need to pass an instance of the CategoryModel, which I can't figure out how to do:但是当我尝试变异时,我需要传递 CategoryModel 的一个实例,我不知道该怎么做:
mutation X {
createProduct(productData: {title: "title", category: {}}) {
product {
id
}
}
}
How can I create products which require a title and a category?如何创建需要标题和类别的产品?
I once had similar problem yesterday, but seems I haven't gotten a better grasp of GRAPHENE.昨天我曾经遇到过类似的问题,但似乎我还没有更好地掌握 GRAPHENE。 But I wrote some funny codes which worked, tho there could be a more graphene way of solving it.但我写了一些有趣的代码,但可能有更多的石墨烯方法来解决它。
class ProductInput(graphene.InputObjectType):
name = graphene.String()
class ProductPayload(graphene.Mutation):
product = graphene.Field(ProductType)
class Meta:
input = ProductInput(required=True)
category_name = graphene.String(required=True)
def mutate(self, info, input=None, category_name=None):
cat = None
if category_name is not None:
cat = Category.objects.get(name=category_name)
product = Product.objects.create(name=input.name, category=cat)
return ProductPayload(product=product)
class ProductMutation(graphene.ObjectType):
create_product = ProductPayload.Field()
schema = graphene.Schema(mutation=ProductMutation
To make a mutation in your graphiql,要在您的 graphiql 中进行突变,
mutation {
createProduct(input: {name: "Your product name"}, category_name: "your category name") {
product {
name,
category {
name
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.