I'm very new to Graphene and testing it to see if i could use it for a Django project with complex queries. To test it, i'm trying to create an Ecommerce with the following models
class Sku(models.Model):
name = models.CharField(max_length=100)
class Product(models.Model):
name = models.CharField(max_length=100)
class ProductSku(models.Model):
sku = models.ForeignKey(Sku, related_name='product_sku', on_delete=models.CASCADE)
product = models.ForeignKey(Product, related_name='product_sku', on_delete=models.CASCADE)
price = models.IntegerField()
As you can see here Product and Sku have a many to many relationship using the model ProductSku
Using the documentation from Graphene i created the following Schema
class SkuNode(DjangoObjectType):
class Meta:
model = Sku
class ProductNode(DjangoObjectType):
class Meta:
model = Product
class ProductSkuNode(DjangoObjectType):
class Meta:
model = ProductSku
class Query(graphene.ObjectType):
all_products = graphene.List(ProductNode, name=graphene.String())
product = graphene.Field(ProductNode, id=graphene.Int())
def resolve_all_products(self, info, **args):
name = args.get('name')
if name is not None:
return Product.objects.filter(name__icontains=name)
return Product.objects.all()
def resolve_product(self, info, **args):
id = args.get('id')
if id is not None:
return Product.objects.filter(pk=id).first()
Right now my frontend app could get the price of a given product for a given sku by doing a query that asks for
query{
allProducts{
id,
name,
productSku{
price,
sku{
id,
name
}
}
}
}
But what i want to do is a query that asks for the price inside the SkuNode
query{
allProducts{
id,
name,
sku{
id,
name,
price
}
}
}
Is that posible?