1

Below is the RDBMS diagram of the Django/Graphene example I am working with: enter image description here

Below is the code for my model.py for the app:

from django.db import models

# Create your models here.
class ProductCategory(models.Model):
  category = models.CharField(max_length=50)
  parentCategory = models.ForeignKey('self',null=True, on_delete=models.CASCADE)

class Product(models.Model):
  productNumber= models.CharField(max_length=50)
  description = models.CharField(max_length=50)
  productCategory= models.ForeignKey('product.ProductCategory', on_delete=models.PROTECT)

Below is Schema.py for the app:

import graphene
from graphene_django import DjangoObjectType

from .models import Product, ProductCategory

class ProductType(DjangoObjectType):
  class Meta:
    model = Product

class ProductCategoryType(DjangoObjectType):
  class Meta:
    model = ProductCategory

class Query(graphene.ObjectType):
  products = graphene.List(ProductType)
  productCategories = graphene.List(ProductCategoryType)

  def resolve_products(self, info):
      return Product.objects.all()
  def resolve_productCategories(self,info):
      return ProductCategory.objects.all()


class CreateProductCategory(DjangoObjectType):
  productCategory = graphene.Field(ProductCategoryType)

  class Arguments:
    category = graphene.String(required=True)
    parentCategory = graphene.Int()
  
  def mutate(self, info, category, parentCategory):
    productCategory = ProductCategory(category = category, parentCategory = parentCategory)
    productCategory.save()
    return CreateProductCategory(productCategory=productCategory)

    return CreateProductCategory(category=category,parentCategory=parentCategory)

class Mutation(graphene.ObjectType):
  createProductCategory= CreateProductCategory.Field()

Without the mutation codes, query request works fine as illustrated below enter image description here

But it would output an error when mutation codes are added and I can't figure out what I did wrong as I am a noob. Please help!!

AssertionError: You need to pass a valid Django Model in CreateProductCategory.Meta, received "None".

Dongyob
  • 87
  • 2
  • 8
  • 3
    Does this answer your question? [AssertionError: You need to pass a valid Django Model in UserProfile.Meta, received "None"](https://stackoverflow.com/questions/57975726/assertionerror-you-need-to-pass-a-valid-django-model-in-userprofile-meta-recei) – frozen Aug 08 '20 at 17:39

1 Answers1

3

This is the problem: class CreateProductCategory(DjangoObjectType)

You need to inherit graphene.Mutation, not DjangoObjectType, for mutations.

Correct: class CreateProductCategory(graphene.Mutation) `

Sagar Adhikari
  • 1,312
  • 1
  • 10
  • 17