Skip to content

Latest commit

 

History

History
82 lines (55 loc) · 1.74 KB

GraphQL.md

File metadata and controls

82 lines (55 loc) · 1.74 KB

Django GraphQL

Instructions for setting up GraphQL in a Django Project.

Graphene documentation

Table of Contents:

  1. Why Graphql?
  2. Graphene Installation
  3. Creating Schemas
  4. Using Graphiql

Why Graphql?

  • Get only the data that you want
  • Easier to manage endpoints

Graphene Installation

Install Graphene: pip install django_graphene

Creating Schemas

# cookbook/schema.py
import graphene
from graphene_django import DjangoObjectType

from cookbook.ingredients.models import Category, Ingredient

class CategoryType(DjangoObjectType):
    class Meta:
        model = Category
        fields = ("id", "name", "ingredients")

class IngredientType(DjangoObjectType):
    class Meta:
        model = Ingredient
        fields = ("id", "name", "notes", "category")

class Query(graphene.ObjectType):
    all_ingredients = graphene.List(IngredientType)
    category_by_name = graphene.Field(CategoryType, name=graphene.String(required=True))

    def resolve_all_ingredients(root, info):
        # We can easily optimize query count in the resolve method
        return Ingredient.objects.select_related("category").all()

    def resolve_category_by_name(root, info, name):
        try:
            return Category.objects.get(name=name)
        except Category.DoesNotExist:
            return None

schema = graphene.Schema(query=Query)

Light Color Current State Next State
Green Green Yellow
Yellow Yellow Red
Section Header

Section body text.

  • hello
  • test

Foot Notes

Happy trees are happy 1

Footer

Footnotes

  1. By Bob Ross