Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.
Go to file
Steven DeMartini 520ddeabf6
Fix graphiql explorer styles by including official CSS, and update local example app for testing (#1418)
* Add venv and .venv to gitignore since common venv paths

* Update cookbook-plain app requirements and local-dev notes

This also adds the DEFAULT_AUTO_FIELD to the app's Django settings to
resolve this warning when running `migrate`:

```
ingredients.Category: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
	HINT: Configure the DEFAULT_AUTO_FIELD setting or the IngredientsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
```

* Fix #1417 graphiql explorer styles by including official CSS

Like in the official graphiql-plugin-explorer example here
6198646919/packages/graphiql-plugin-explorer/examples/index.html (L26-L29)

Resolves https://github.com/graphql-python/graphene-django/issues/1417

* Update GraphiQL version

---------

Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
Co-authored-by: Kien Dang <mail@kien.ai>
2023-06-02 11:48:53 +03:00
.github Set pypi GH action to latest v1 (#1415) 2023-05-27 16:26:52 +03:00
bin Isolated Graphene Django in a new package 2016-09-17 16:31:17 -07:00
docs Add DjangoFormInputObjectType to forms/types (#1325) 2023-05-24 15:58:50 +03:00
examples Fix graphiql explorer styles by including official CSS, and update local example app for testing (#1418) 2023-06-02 11:48:53 +03:00
graphene_django Fix graphiql explorer styles by including official CSS, and update local example app for testing (#1418) 2023-06-02 11:48:53 +03:00
.coveragerc Improved python syntax and sorts (pep8). Improved Readme 2016-09-17 17:09:56 -07:00
.gitignore Fix graphiql explorer styles by including official CSS, and update local example app for testing (#1418) 2023-06-02 11:48:53 +03:00
.pre-commit-config.yaml Add support for Python 3.11 (#1365) 2023-05-04 15:19:24 +03:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2019-05-06 13:28:02 +01:00
CONTRIBUTING.md 👷 Add pre-commit (#1336) 2022-10-19 17:10:30 +03:00
LICENSE add license to repo 2018-05-25 01:28:51 -04:00
Makefile Add pre-commit to dev-setup 2023-05-03 12:08:22 +03:00
MANIFEST.in 👷 Add pre-commit (#1336) 2022-10-19 17:10:30 +03:00
README.md Update README.md (#1408) 2023-05-04 23:54:09 +03:00
setup.cfg 👷 Add pre-commit (#1336) 2022-10-19 17:10:30 +03:00
setup.py Fix linting issues (#1412) 2023-05-24 16:13:23 +03:00
tox.ini Add support for Python 3.11 (#1365) 2023-05-04 15:19:24 +03:00

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

  • 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