graphql django code example

Example 1: install graphene django

pip install graphene-django

Example 2: django pymysql

#here is latest version of pymysql in mysqldb.
 #add this settings in settings file
 import pymysql
 pymysql.version_info = (1, 4, 6, 'final', 0)
 pymysql.install_as_MySQLdb()

Example 3: pip django graphql

INSTALLED_APPS = (
    # ...
    'graphene_django',
)

GRAPHENE = {
    'SCHEMA': 'app.schema.schema' # Where your Graphene schema lives
}

Example 4: pip django graphql

#example
from graphene_django import DjangoObjectType
import graphene

class User(DjangoObjectType):
    class Meta:
        model = UserModel

class Query(graphene.ObjectType):
    users = graphene.List(User)

    @graphene.resolve_only_args
    def resolve_users(self):
        return UserModel.objects.all()

schema = graphene.Schema(query=Query)

Example 5: pip django graphql

pip install "graphene-django>=2.0"

Example 6: pip django graphql

from django.conf.urls import url
from graphene_django.views import GraphQLView

urlpatterns = [
    # ...
    url(r'^graphql$', GraphQLView.as_view(graphiql=True)),
]