graphene examples

How to Use GraphQL with Django

The ability to integrate Wagtail CMS (Django) with existing site models at the GraphQL level means that we could use our existing API calls within blog articles using slightly more than ForeignKey and a small converter.


  • an empty folder for the project;
  • Python 3.6;
  • Virtualenv;
  • installed Wagtail CMS.

Add an app for your blog:

python startapp blog

Update blog/ with the following content:

# Taken From

from __future__ import unicode_literals

from django.db import models

# Create your models here.

 from wagtail.wagtailcore.models import Page

from wagtail.wagtailcore.fields import RichTextField

from wagtail.wagtailadmin.edit_handlers import FieldPanel

from wagtail.wagtailsearch import index

class BlogIndexPage(Page):

 intro = RichTextField(blank=True)

content_panels = Page.content_panels + [

FieldPanel(‘intro’, classname=”full”)


class BlogPage(Page):

date = models.DateField(“Post date”)

intro = models.CharField(max_length=250)

body = RichTextField(blank=True)

search_fields = Page.search_fields + [




 content_panels = Page.content_panels + [



FieldPanel(‘body’, classname=”full”),


To implement a bundle of Wagtail CMS (Django) and GraphQL, we use Graphene.

Install Graphene:

pip install “graphene-django==1.2”

Configuring Graphene:

Add the GRAPHENE parameter to


   ‘SCHEMA’: ‘api.schema.schema’,


Add the API application:

Create in the root folder of the site mysite, a folder named api.


Create an file inside the new api folder and paste the following into it:

from django.apps import AppConfig

class ApiConfig(AppConfig): name = ‘api’


Create one more file inside api folder with the following content:

from __future__ import unicode_literals

import graphene

from graphene_django import DjangoObjectType

from blog.models import BlogPage

from django.db import models

class ArticleNode(DjangoObjectType):

    class Meta:

        model = BlogPage

        only_fields = [‘id’, ‘title’, ‘date’, ‘intro’, ‘body’]

class Query(graphene.ObjectType):

    articles = graphene.List(ArticleNode)


    def resolve_articles(self):


schema = graphene.Schema(query=Query)
Configuring URL-addresses

Add two new imports to your file.

from django.views.decorators.csrf import csrf_exempt

from graphene_django.views import GraphQLView

Add two new URLs to your file, just above the Wagtail addresses.

url(r’^api/graphql’, csrf_exempt(GraphQLView.as_view())),

url(r’^api/graphiql’, csrf_exempt(GraphQLView.as_view(graphiql=True, pretty=True))),

Add new apps to settings:






Commit your changes:

python makemigrations

python migrate

If everything was done correctly and there were no errors, start the local server:

python runserver


Igor Grigorenko

Add comment