Skip to main content

A Django app to automatically generate graphql apis from django code.

Project description

autographql is a Django app to automatically generate a GraphQL api from Django models.

Detailed documentation is in the “docs” directory.

Features

  • Automatic optimization of querysets to prevent N+1 lookups

  • Record level permissions system provided by Bridgekeeper integration

  • Easily extended automatic filters based on django Lookups

Quick start

  1. Add “autographql”, “graphene_django, and “bridgekeeper” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'autographql',
        'graphene_django',
        'bridgekeeper',
    ]
  2. Add the following settings to your settings.py:

    GRAPHENE = {
        'SCHEMA': 'autographql.schema.schema',
        'MIDDLEWARE': [
            'autographql.auth.middleware.AuthorizationMiddleware',
        ]
    }
    
    AUTHENTICATION_BACKENDS = [
        'bridgekeeper.backends.RulePermissionBackend',
        'django.contrib.auth.backends.ModelBackend', # this is default
    ]
  3. Add the graphql URLconf in your project urls.py like this:

    path('graphql', csrf_exempt(GraphQLView.as_view(graphiql=True)), name='graphql'),
  4. Write your models extending GraphQLModel instead of models.Model:

    # cookbook/ingredients/models.py
    from django.db import models
    from autographql import GraphQLModel
    
    
    class Category(GraphQLModel):
        name = models.CharField(max_length=100)
    
        def __str__(self):
            return self.name
    
    
    class Ingredient(GraphQLModel):
        name = models.CharField(max_length=100)
        notes = models.TextField()
        category = models.ForeignKey(Category, related_name='ingredients')
    
        def __str__(self):
            return self.name
  5. Add some access control rules to your models:

    # cookbook/ingredients/permissions.py
    from bridgekeeper import perms
    from bridgekeeper.rules import always_allow
    
    from autographql.auth.utils import get_model_permission, VIEW, CREATE, UPDATE, DELETE
    from cookbook.models import Category, Ingredient
    
    perms[get_model_permission(Category, VIEW)] = always_allow
    perms[get_model_permission(Category, CREATE)] = always_allow
    perms[get_model_permission(Category, UPDATE)] = always_allow
    perms[get_model_permission(Category, DELETE)] = always_allow
    perms[get_model_permission(Ingredient, VIEW)] = always_allow
    perms[get_model_permission(Ingredient, CREATE)] = always_allow
    perms[get_model_permission(Ingredient, UPDATE)] = always_allow
    perms[get_model_permission(Ingredient, DELETE)] = always_allow
  6. Import the permissions file in your app’s ready function:

    # cookbook/ingredients/app.py
    from django.apps import AppConfig
    
    
    class IngredientsConfig(AppConfig):
        default_auto_field = 'django.db.models.BigAutoField'
        name = 'ingredients'
    
        def ready(self):
            # Apply permissions
            import ingredients.permissions
  7. Start the development server and visit http://127.0.0.1:8000/graphql/ to view your fully featured graphql api!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-autographql-0.0.3.tar.gz (127.4 kB view hashes)

Uploaded Source

Built Distribution

django_autographql-0.0.3-py3-none-any.whl (151.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page