Skip to main content

Graphene Logo Graphene-Django

build pypi Anaconda-Server Badge coveralls

Graphene-Django is an open-source library that provides seamless integration between Django, a high-level Python web framework, and Graphene, a library for building GraphQL APIs. The library allows developers to create GraphQL APIs in Django quickly and efficiently while maintaining a high level of performance.

Features

  • Seamless integration with Django models
  • Automatic generation of GraphQL schema
  • Integration with Django's authentication and permission system
  • Easy querying and filtering of data
  • Support for Django's pagination system
  • Compatible with Django's form and validation system
  • Extensive documentation and community support

Installation

To install Graphene-Django, run the following command:

pip install graphene-django

Configuration

After installation, add 'graphene_django' to your Django project's INSTALLED_APPS list and define the GraphQL schema in your project's settings:

INSTALLED_APPS = [
    # ...
    'graphene_django',
]

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

Usage

To use Graphene-Django, create a schema.py file in your Django app directory and define your GraphQL types and queries:

import graphene
from graphene_django import DjangoObjectType
from .models import MyModel

class MyModelType(DjangoObjectType):
    class Meta:
        model = MyModel

class Query(graphene.ObjectType):
    mymodels = graphene.List(MyModelType)

    def resolve_mymodels(self, info, **kwargs):
        return MyModel.objects.all()

schema = graphene.Schema(query=Query)

Then, expose the GraphQL API in your Django project's urls.py file:

from django.urls import path
from graphene_django.views import GraphQLView
from . import schema

urlpatterns = [
    # ...
    path('graphql/', GraphQLView.as_view(graphiql=True)), # Given that schema path is defined in GRAPHENE['SCHEMA'] in your settings.py
]

Testing

Graphene-Django provides support for testing GraphQL APIs using Django's test client. To create tests, create a tests.py file in your Django app directory and write your test cases:

from django.test import TestCase
from graphene_django.utils.testing import GraphQLTestCase
from . import schema

class MyModelAPITestCase(GraphQLTestCase):
    GRAPHENE_SCHEMA = schema.schema

    def test_query_all_mymodels(self):
        response = self.query(
            '''
            query {
                mymodels {
                    id
                    name
                }
            }
            '''
        )

        self.assertResponseNoErrors(response)
        self.assertEqual(len(response.data['mymodels']), MyModel.objects.count())

Contributing

Contributions to Graphene-Django are always welcome! To get started, check the repository's issue tracker and contribution guidelines.

License

Graphene-Django is released under the MIT License.

Resources

Tutorials and Examples

Related Projects

  • Graphene - A library for building GraphQL APIs in Python
  • Graphene-SQLAlchemy - Integration between Graphene and SQLAlchemy, an Object Relational Mapper (ORM) for Python
  • Graphene-File-Upload - A package providing an Upload scalar for handling file uploads in Graphene
  • Graphene-Subscriptions - A package for adding real-time subscriptions to Graphene-based GraphQL APIs

Support

If you encounter any issues or have questions regarding Graphene-Django, feel free to submit an issue on the official GitHub repository. You can also ask for help and share your experiences with the Graphene-Django community on 💬 Discord

Release Notes

Release files for graphene-django 3.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphene-django 3.2.3
File Size Uploaded
graphene-django-3.2.3.tar.gz 88.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for graphene-django 3.2.3
File Interpreter ABI Platform
graphene_django-3.2.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 203.4 kB

Release files / graphene-django-3.2.3.tar.gz

Download URL graphene-django-3.2.3.tar.gz
Size 88.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d831bfe8e9a6e77e477b7854faef4addb318f386119a69ee4c57b74560f3e07d
BLAKE2b-256 checksum
How to use checksums
6f7a8aef131349329dcd167578720f6364412f1728bfda14bba22c1e7b5d8365
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / graphene_django-3.2.3-py2.py3-none-any.whl

Download URL graphene_django-3.2.3-py2.py3-none-any.whl
Size 115.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0c673a4dad315b26b4d18eb379ad0c7027fd6a36d23a1848b7c7c09a14a9271e
BLAKE2b-256 checksum
How to use checksums
1835ab9c668222f6271a0b71efb147c46816229dbb9fa5d15ee6b025eb08d4b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

3.2.3 This release

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.6

2 release files

3.1.5

2 release files

3.1.4

2 release files

3.1.3

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.16.0

2 release files

2.15.0

2 release files

2.14.0

2 release files

2.13.0

2 release files

2.12.1

2 release files

2.12.0

2 release files

2.11.0

2 release files

2.10.1

2 release files

2.9.1

2 release files

2.9.0

2 release files

2.8.2

2 release files

2.8.1

2 release files

2.8.0

2 release files

2.7.1

2 release files

2.7.0

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.2

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.3

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0

1 release file

0.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page