лучшее решение выглядит как это , если вы не хотите использовать Relay.
вы можете изменить свой schema.py так:
import graphene
from graphene_django.types import DjangoObjectType
from shoes_store.ingredients.models import Category, Product
from django.db.models import Q
class CategoryType(DjangoObjectType):
class Meta:
model = Category
class ProductType(DjangoObjectType):
class Meta:
model = Product
class Query:
#===========================
# product
products = graphene.List(ProductType,name=graphene.String(),category_id=graphene.ID())
product = graphene.Field(ProductType,product_id=graphene.ID())
def resolve_products(self, info, name=None,category_id=None, **kwargs):
# Querying a list
if name:
filter = (
Q(name__icontains=name) |
Q(descreption__icontains=name)
)
return Product.objects.filter(filter)
#
if category_id:
filter = (
Q(category_id__exact=category_id)
)
return Product.objects.filter(filter)
#
return Product.objects.all()
def resolve_product(self, info, product_id):
# Querying a single user
return Product.objects.get(pk=product_id)
#===========================
# product_category
categories = graphene.List(CategoryType)
category = graphene.Field(CategoryType,category_id=graphene.ID())
def resolve_categories(self, info, **kwargs):
# Querying a list
return Category.objects.all()
def resolve_category(self, info, category_id):
# Querying a single user
return Category.objects.get(pk=category_id)
теперь вы можете использовать разные запросы в вашем адресе "http://127.0.0.1: 8000 / graphql ". Например, вы можете использовать:
query{
products{
id
name
price
}
}
или даже:
query{
products(categoryId:1){
id
name
price
}
}
или
query{
products(name:"Men's boots"){
id
name
price
}
}