Index

Graphene

  1. Queries
  2. migrations
  3. UserModel
  4. User Schema
  5. cors
  6. static
  7. headers
  8. payumoney
  9. imageupload
  10. Total count

Queries


sakhura_backend/jewellery/models.py

from django.db import models

class JewelleryCategories(models.Model):
    """Basic categories of Jewellery"""
    category_name = models.CharField(max_length=255)
    category_url = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        """Return the category name as a string."""
        return self.category_name

class JewelleryType(models.Model):
    """Type of Jewellery"""
    jewellery_type = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        """Return the jewellery type as a string."""
        return self.jewellery_type


sakhura_backend/sakhura_backend/urls.py
from django.contrib import admin
from django.urls import path
from graphene_django.views import GraphQLView
from django.views.decorators.csrf import csrf_exempt


urlpatterns = [
    path('admin/', admin.site.urls),
    path('graphql', csrf_exempt(GraphQLView.as_view(graphiql=True))),
]


sakhura_backend/jewellery/fixtures/categories.json
[
    { "model""jewellery.JewelleryCategories""pk"1"fields": { "category_name""Earrings""category_url""/earrings"} },
    { "model""jewellery.JewelleryCategories""pk"2"fields": { "category_name""Kada""category_url""/kada"} }
]


python3 manage.py loaddata categories

// It will show error as created_at and updated_at is not present


sakhura_backend/jewellery/schema.py
from graphene import relay, ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
import graphene
from graphene_permissions.mixins import AuthNode, AuthMutation
from graphene_permissions.permissions import AllowStaff

from jewellery.models import JewelleryCategory, JewelleryType

class JewelleryCategoryNode(DjangoObjectType):
    class Meta:
        model = JewelleryCategory
        filter_fields = ['category_name''category_url']
        interfaces = (relay.Node, )
    

class JewelleryTypeNode(DjangoObjectType):
    class Meta:
        model = JewelleryType
        filter_fields = ['jewellery_type']
        interfaces = (relay.Node, )

class Query(ObjectType):
    jewellery_category = relay.Node.Field(JewelleryCategoryNode)
    all_jewellery_categories = DjangoFilterConnectionField(JewelleryCategoryNode)

    jewellery_type = relay.Node.Field(JewelleryTypeNode)
    all_jewellery_types = DjangoFilterConnectionField(JewelleryTypeNode)


class CreateCategory(AuthMutation, relay.ClientIDMutation):
    permission_classes = (AllowStaff, )
    jewellery_category = graphene.Field(JewelleryCategoryNode)
    class Input:
        category_name = graphene.String(required=True)
        category_url = graphene.String(required=True)
    
    @classmethod
    def mutate_and_get_payload(cls, root, info, **input):
        if cls.has_permission(root, info, input):
            category_name = input.get('category_name')
            category_url = input.get('category_url')
            category = JewelleryCategory(category_name=category_name, category_url=category_url)
            category.save()

            return CreateCategory(
                jewellery_category = category

            )
        else:
            raise Exception('Permission Denied')
class CategoryMutation(ObjectType):
    create_jewellery_category = CreateCategory.Field()



Another query:
from graphene import relay, ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
import graphene
from graphql_relay.node.node import from_global_id
from graphene_permissions.mixins import AuthNode, AuthMutation
from graphene_permissions.permissions import AllowStaff, AllowAny
from graphql_relay.node.node import from_global_id

from jewellery.models import JewelleryCategory, JewelleryType, JewelleryProduct

class JewelleryCategoryNode(DjangoObjectType):
    class Meta:
        model = JewelleryCategory
        filter_fields = ['category_name''category_url']
        interfaces = (relay.Node, )
    

class JewelleryTypeNode(DjangoObjectType):
    class Meta:
        model = JewelleryType
        filter_fields = ['type_name']
        interfaces = (relay.Node, )

class JewelleryProductDetailsNode(DjangoObjectType):
    class Meta:
        model = JewelleryProduct
        filter_fields = {
            'product_name': ['exact''icontains'],
            'sku': ['exact'],
            'description' :['icontains'],
            'jewellery_category__category_name': ['exact''icontains'],
            'jewellery_type__type_name': ['exact''icontains'],
            'price': ['exact'],
            'stock': ['exact'],
            'price_before_discount': ['exact']
        }
        interfaces = (relay.Node, )
        
class JewelleryProductList(DjangoObjectType):
    class Meta:
        model = JewelleryProduct

class Query(ObjectType):
    jewellery_category = relay.Node.Field(JewelleryCategoryNode)
    all_jewellery_categories = DjangoFilterConnectionField(JewelleryCategoryNode)

    jewellery_type = relay.Node.Field(JewelleryTypeNode)
    all_jewellery_types = DjangoFilterConnectionField(JewelleryTypeNode)

    product = relay.Node.Field(JewelleryProductDetailsNode)
    all_product = DjangoFilterConnectionField(JewelleryProductDetailsNode)
    product_list = graphene.List(JewelleryProductDetailsNode, ids = graphene.List(graphene.ID) )

    def resolve_product_list(self, info, ids):
        gid = []
        for id in ids:
            gid.append(from_global_id(id)[1])


        return JewelleryProduct.objects.filter(pk__in=gid)



sakhura_backend/sakhura_backend/schema.py
import graphene
import jewellery.schema

class Query(jewellery.schema.Query, graphene.ObjectType):
    pass

class Mutation(jewellery.schema.CategoryMutation):
    pass

schema = graphene.Schema(query=Query, mutation=Mutation)



Queries



query {
  allJewelleryCategories {
    edges {
      node {
        categoryName,
        categoryUrl
      }
    }
  }
}


query {
  allJewelleryCategories {
    edges {
      node {
        id,
        categoryName,
        categoryUrl
      }
    }
  }
}



query {
  
  jewelleryCategory(id: "SmV3ZWxsZXJ5Q2F0ZWdvcnlOb2RlOjE=") {
    id,
    categoryName,
    categoryUrl
  }
}




query {
  allJewelleryCategories (categoryName:"Earrings") {
    edges {
      node {
        categoryName,
        categoryUrl
      }
    }
  }
}

mutation {
    createJewelleryCategory(
        input:{categoryName: "Earrings", categoryUrl: "/earrings"}
    ) {
    jewelleryCategory{
        categoryName,
        categoryUrl

    }
  }
}

mutation {
    createJewelleryCategory(
        input:{categoryName: "Kada", categoryUrl: "/kada"}
    ) {
    jewelleryCategory{
        categoryName,
        categoryUrl

    }
  }
}