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.
To install Graphene-Django, run the following command:
pip install graphene-django
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' }
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 ]
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())
Contributions to Graphene-Django are always welcome! To get started, check the repository's issue tracker and contribution guidelines.
Graphene-Django is released under the MIT License.
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4