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.

Required:

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

Add an app for your blog:

python manage.py startapp blog

Update blog/models.py with the following content:

# Taken From http://docs.wagtail.io/en/v1.9/getting_started/tutorial.html

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 + [

index.SearchField(‘intro’),

index.SearchField(‘body’),

]

 content_panels = Page.content_panels + [

FieldPanel(‘date’),

FieldPanel(‘intro’),

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 base.py

GRAPHENE = {

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

}

Add the API application:

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

Add apps.py.

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

from django.apps import AppConfig

class ApiConfig(AppConfig): name = ‘api’

Add schema.py

Create one more file schema.py 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)

    @graphene.resolve_only_args

    def resolve_articles(self):

        return BlogPage.objects.live()

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

Add two new imports to your urls.py file.

from django.views.decorators.csrf import csrf_exempt

from graphene_django.views import GraphQLView

Add two new URLs to your urls.py 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:

INSTALLED_APPS = (

    ‘api’,

    ‘blog’,

    ‘graphene_django’,

)

Commit your changes:

python manage.py makemigrations

python manage.py migrate

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

python manage.py runserver

Igor Grigorenko

Add comment