Skip to content

Add documentation for django-graphbox library to GraphQL Python Tools… #1446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/content/code/language-support/python/server/django-graphbox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
name: Django Graphbox
description: Package for easy building a GraphQL API with basic CRUD operations for Django models.
url: https://90horasporsemana.com/graphbox/
github: yefeza/django-graphbox
---

A Quickstart for Django Graphbox:

1. Install the package:

```bash
pip install django-graphbox
```

2. Create a new Django project:

```bash
django-admin startproject myproject
```

3. Create a new Django app:

```bash
cd myproject
python manage.py startapp myapp
```

4. Define your Django models in `myapp/models.py`:

```python
from django.db import models

class MyModel(models.Model):
name = models.CharField(max_length=100)
```

5. Create and run migrations:

```bash
python manage.py makemigrations
python manage.py migrate
```

6. Configure and Build your GraphQL Schema in `myapp/schema.py`:

```python
from django_graphbox.builder import SchemaBuilder
from myapp.models import MyModel

builder = SchemaBuilder()
builder.add_model(MyModel)

query_class = builder.build_schema_query()
mutation_class = builder.build_schema_mutation()
```

7. Create a main Schema in `myproject/schema.py` (In this main schema you can add your own queries and mutations):

```python
import graphene
from myapp.schema import query_class, mutation_class

class Query(query_class, graphene.ObjectType):
pass

class Mutation(mutation_class, graphene.ObjectType):
pass

schema = graphene.Schema(query=Query, mutation=Mutation)
```

8. Add the GraphQL view to your `myproject/urls.py`:

```python
from django.urls import path
from graphene_file_upload.django import FileUploadGraphQLView
from django.views.decorators.csrf import csrf_exempt
from myproject.schema import schema

urlpatterns = [
path('graphql/', csrf_exempt(FileUploadGraphQLView.as_view(graphiql=True, schema=schema))),
]
```

9. Run the server:

```bash
python manage.py runserver
```

10. Open the GraphiQL interface at `http://localhost:8000/graphql/` and start querying your API!

You can find advanced examples with authentication, filters, validations and more on github or pypi.