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.4.tar.gz (108.9 kB view details)

Uploaded Source

File details

Details for the file django-autographql-0.0.4.tar.gz.

File metadata

  • Download URL: django-autographql-0.0.4.tar.gz
  • Upload date:
  • Size: 108.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

File hashes

Hashes for django-autographql-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1102eb5be1844e45430042f86c966ca15f18556e603b5a7490c783e7e68e9ffa
MD5 65aee7758c8727d0f0c4f258a15f095e
BLAKE2b-256 d302d98a8ce19d1f18eab376f6b5bdc5aaeb57c717068c1392faec5787b9489f

See more details on using hashes here.

Supported by

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