2020-02-08 23:28:50 +03:00
# ![Graphene Logo](http://graphene-python.org/favicon.png) Graphene-Django
2016-09-18 02:29:00 +03:00
2016-09-18 03:09:56 +03:00
A [Django ](https://www.djangoproject.com/ ) integration for [Graphene ](http://graphene-python.org/ ).
2016-09-18 02:29:00 +03:00
2020-11-08 08:44:37 +03:00
[![build][build-image]][build-url]
2020-02-08 23:28:50 +03:00
[![pypi][pypi-image]][pypi-url]
[![Anaconda-Server Badge][conda-image]][conda-url]
[![coveralls][coveralls-image]][coveralls-url]
2020-11-08 08:44:37 +03:00
[build-image]: https://github.com/graphql-python/graphene-django/workflows/Tests/badge.svg
[build-url]: https://github.com/graphql-python/graphene-django/actions
2020-02-08 23:28:50 +03:00
[pypi-image]: https://img.shields.io/pypi/v/graphene-django.svg?style=flat
[pypi-url]: https://pypi.org/project/graphene-django/
2020-04-13 13:54:17 +03:00
[coveralls-image]: https://coveralls.io/repos/github/graphql-python/graphene-django/badge.svg?branch=master
2020-02-08 23:28:50 +03:00
[coveralls-url]: https://coveralls.io/github/graphql-python/graphene-django?branch=master
[conda-image]: https://img.shields.io/conda/vn/conda-forge/graphene-django.svg
[conda-url]: https://anaconda.org/conda-forge/graphene-django
2020-04-07 13:05:17 +03:00
[💬 Join the community on Slack ](https://join.slack.com/t/graphenetools/shared_invite/enQtOTE2MDQ1NTg4MDM1LTA4Nzk0MGU0NGEwNzUxZGNjNDQ4ZjAwNDJjMjY0OGE1ZDgxZTg4YjM2ZTc4MjE2ZTAzZjE2ZThhZTQzZTkyMmM )
2019-04-26 18:48:37 +03:00
## Documentation
[Visit the documentation to get started! ](https://docs.graphene-python.org/projects/django/en/latest/ )
## Quickstart
2016-09-18 02:29:00 +03:00
2017-11-29 05:02:53 +03:00
For installing graphene, just run this command in your shell
2016-09-18 02:29:00 +03:00
```bash
2017-10-26 10:21:11 +03:00
pip install "graphene-django>=2.0"
2016-09-18 02:29:00 +03:00
```
2016-09-20 08:04:23 +03:00
### Settings
```python
INSTALLED_APPS = (
# ...
2018-08-30 22:59:09 +03:00
'django.contrib.staticfiles', # Required for GraphiQL
2016-09-20 08:04:23 +03:00
'graphene_django',
)
GRAPHENE = {
'SCHEMA': 'app.schema.schema' # Where your Graphene schema lives
}
```
### Urls
We need to set up a `GraphQL` endpoint in our Django app, so we can serve the queries.
```python
2019-07-27 17:14:34 +03:00
from django.urls import path
2016-09-20 08:04:23 +03:00
from graphene_django.views import GraphQLView
urlpatterns = [
# ...
2019-07-27 17:14:34 +03:00
path('graphql', GraphQLView.as_view(graphiql=True)),
2016-09-20 08:04:23 +03:00
]
```
2016-09-18 02:29:00 +03:00
## Examples
2016-09-18 03:09:56 +03:00
Here is a simple Django model:
2016-09-18 02:29:00 +03:00
```python
from django.db import models
class UserModel(models.Model):
name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
2016-09-18 03:09:56 +03:00
```
To create a GraphQL schema for it you simply have to write the following:
```python
from graphene_django import DjangoObjectType
2016-11-03 19:20:40 +03:00
import graphene
2016-09-18 02:29:00 +03:00
class User(DjangoObjectType):
class Meta:
model = UserModel
2016-09-18 03:09:56 +03:00
class Query(graphene.ObjectType):
users = graphene.List(User)
2016-09-18 02:29:00 +03:00
2017-11-23 00:47:18 +03:00
def resolve_users(self, info):
2016-09-18 03:09:56 +03:00
return UserModel.objects.all()
2016-09-19 19:49:25 +03:00
schema = graphene.Schema(query=Query)
2016-09-18 03:09:56 +03:00
```
2020-04-13 13:53:06 +03:00
Then you can query the schema:
2016-09-18 03:09:56 +03:00
```python
query = '''
query {
users {
name,
lastName
}
}
'''
result = schema.execute(query)
2016-09-18 02:29:00 +03:00
```
2016-09-18 03:09:56 +03:00
To learn more check out the following [examples ](examples/ ):
2016-09-18 02:29:00 +03:00
* **Schema with Filtering**: [Cookbook example ](examples/cookbook )
* **Relay Schema**: [Starwars Relay example ](examples/starwars )
2020-11-07 03:04:45 +03:00
## GraphQL testing clients
- [Firecamp ](https://firecamp.io/graphql )
- [GraphiQL ](https://github.com/graphql/graphiql )
2016-09-18 02:29:00 +03:00
## Contributing
2019-07-27 17:14:34 +03:00
See [CONTRIBUTING.md ](CONTRIBUTING.md )
2019-10-01 16:59:52 +03:00
## Release Notes
* See [Releases page on github ](https://github.com/graphql-python/graphene-django/releases )