Skip to content

CODED-Factory/DEMO-Django-Graphene

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Graphene

Slides

Recording

Setup

  1. Install Django Graphene

    pip install graphene-django

    settings.py

    INSTALLED_APPS = [
        ...
        'django.contrib.staticfiles', # Required for GraphiQL
        'graphene_django'
    ]
  2. Add a GraphQL url

    from django.views.decorators.csrf import csrf_exempt
    from graphene_django.views import GraphQLView
    
    urlpatterns = [
        ...
        path("graphql/", csrf_exempt(GraphQLView.as_view(graphiql=True))),
    ]
  3. Add an empty Query

    schema.py

    import graphene
    
    class Query(graphene.ObjectType):
        pass
    
    schema = graphene.Schema(query=Query)

    settings.py

    GRAPHENE = {
      'SCHEMA': 'graphene_demo.schema.schema'
    }

Basics

  1. Add some fields to the query

    class Query(graphene.ObjectType):
      hello = graphene.String()
      goodbye = graphene.String()
  2. Add resolvers for the fields

    class Query(graphene.ObjectType):
      hello = graphene.String()
      goodbye = graphene.String()
    
      def resolve_hello(self, info):
          return "Hello world!"
    
      def resolve_goodbye(self, info):
          return "Goodbye cruel world!"
  3. Make a field and resolver with parameters

    class Query(graphene.ObjectType):
      hello = graphene.String(name=graphene.String(default_value="world"))
      goodbye = graphene.String()
    
      def resolve_hello(self, info, name):
          return f"Hello {name}!"
    
      def resolve_goodbye(self, info):
          return "Goodbye cruel world!"

Django Specific

  1. Make an application schema and connect it to the main schema

    bootcamps/schema.py

    from datetime import datetime
    import graphene
    
     class Query(object):
       thing = graphene.Date()
    
       def resolve_thing(parent, info):
         return datetime.now()

    schema.py

    import bootcamps.schema
    
    class Query(bootcamps.schema.Query, graphene.ObjectType):
      ...
  2. Create our first type

    bootcamps/schema.py

    from graphene_django import DjangoObjectType
    
    from .models import Instructor

    class InstructorType(DjangoObjectType): class Meta: model = Instructor

    
    
  3. Add an instructors query

    class Query(object):
      ...
      instructors = graphene.List(InstructorType)
    
      ...
    
      def resolve_instructors(self, info):
          return Instructor.objects.all()
  4. Add a single instructor query

    class Query(object):
     ...
     instructor = graphene.Field(InstructorType, id=graphene.Int())
    
     ...
    
     def resolve_instructor(self, info, id):
        if id is not None:
            return Instructor.objects.get(pk=id)
        return None
  5. Simplify using DjangoListField

    from graphene_django import DjangoObjectType, DjangoListField
    
    ...
    
    class Query(object):
    
      instructors = DjangoListField(InstructorType)
    
          def resolve_thing(self, info):
              ...
    
          # resolver can be deleted
          # def resolve_instructors(self, infor):
    
          def resolve_instructor(self, info, id):
              ...
  6. Automagic Relationships! 💖

    from .models import Instructor, Cohort
    
    class CohortType(DjangoObjectType):
      class Meta:
          model = Cohort

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages