Giter Club home page Giter Club logo

demo-django-graphene's Introduction

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

demo-django-graphene's People

Contributors

octowl avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.