Skip to main content

GraphQL for Django, powered by graphene and Pydantic: define types/mutations/subscriptions from your models, with nested filtering (and/or/not), pagination, permissions and directives.

Project description

django-graphex

Codecov PyPI - Python Version Django Versions PyPI PyPI - License Downloads Ruff

GraphQL for Django, powered by graphene and Pydantic. Define your GraphQL API straight from your Django models — no DRF, no graphene-django, no django-filter.

  • Model-first types & mutationsDjangoModelType / DjangoModelMutation give you query, list and create/update/delete from a single Meta.model, validated and persisted with Pydantic v2 + the Django ORM (FK existence, uniqueness, unique_together, partial updates, choices → Enum).
  • Logical filtering — one nested filter: argument with and / or / not, per-field lookups, relation descent and plain-pk/UUID filtering (no django-filter).
  • Pagination — limit/offset, page and keyset cursor paginators with a uniform results / totalCount shape (and an automatic N+1 query optimizer).
  • Custom validation — DRF-style inline validate_<field>() / validate() or a Meta.pydantic_model.
  • Permissions, security & directives — permission classes, depth & cost limits, introspection control, and string/number/date/list directives.
  • Subscriptions — real-time GraphQL over Django Channels 4 (optional extra).

Coming from graphene-django or graphene-django-extras? See the Migration Guide for a step-by-step upgrade with before/after examples.

Requirements

  • Python: 3.12+ (3.13, 3.14 supported)
  • Django: 4.2+ (4.2, 5.0, 5.1, 5.2, 6.0 supported)
  • graphene: >=3.3,<4
  • pydantic: >=2,<3

Installation

# uv (recommended)
uv add django-graphex
# real-time subscriptions (adds Django Channels 4):
uv add "django-graphex[subscriptions]"
# pip
pip install django-graphex
pip install "django-graphex[subscriptions]"

The base install never imports channels; only the subscriptions extra does.

Quick start

import graphene
from django.contrib.auth.models import User
from django_graphex import (
    DjangoListObjectType, DjangoListObjectField, DjangoObjectField, DjangoModelMutation,
)
from django_graphex.paginations import LimitOffsetGraphqlPagination


class UserListType(DjangoListObjectType):
    class Meta:
        model = User
        pagination = LimitOffsetGraphqlPagination()
        filter_fields = {"username": ("icontains", "exact"), "is_active": ("exact",)}


class UserMutation(DjangoModelMutation):      # define once -> create/update/delete
    class Meta:
        model = User


class Query(graphene.ObjectType):
    users = DjangoListObjectField(UserListType)


class Mutation(graphene.ObjectType):
    user_create = UserMutation.CreateField()
    user_update = UserMutation.UpdateField()
    user_delete = UserMutation.DeleteField()


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

Query it with the nested filter: argument (and / or / not):

{
  users(filter: { is_active: { exact: true }, username: { icontains: "jo" } }) {
    results(limit: 10, ordering: "-date_joined") { id username }
    totalCount
  }
}

Configuration

All settings live under a single DJANGO_GRAPHEX dict (every key is optional):

# settings.py
DJANGO_GRAPHEX = {
    "DEFAULT_PAGINATION_CLASS": "django_graphex.paginations.LimitOffsetGraphqlPagination",
    "DEFAULT_PAGE_SIZE": 20,
    "MAX_PAGE_SIZE": 50,
    # Response caching. Default is False (disabled).
    # WARNING: cache keys are identity-salted per user (v1.2.1+), but shared
    # caches can still leak data if misconfigured. Review the caching guide
    # before enabling in production: docs/usage/caching.md
    "CACHE_ACTIVE": True,
}

To use directives, add the middleware and pass all_directives to the schema:

GRAPHENE = {"MIDDLEWARE": ["django_graphex.GraphQLDirectiveMiddleware"]}

from django_graphex import all_directives
schema = graphene.Schema(query=Query, mutation=Mutation, directives=all_directives)

Playground

A fully wired example project lives in examples/playground/. It exercises every major feature end-to-end — types, paginators, filtering, mutations, permissions, subscriptions, and the query optimizer — and installs the library from this repo checkout (editable, no PyPI release needed).

Documentation

📚 Full documentation — including the Quick Start, Model backend, Filtering, Pagination, Subscriptions, Settings and the Migration Guide.

License

MIT License — see the LICENSE file.

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_graphex-1.2.1.tar.gz (510.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_graphex-1.2.1-py3-none-any.whl (184.1 kB view details)

Uploaded Python 3

File details

Details for the file django_graphex-1.2.1.tar.gz.

File metadata

  • Download URL: django_graphex-1.2.1.tar.gz
  • Upload date:
  • Size: 510.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_graphex-1.2.1.tar.gz
Algorithm Hash digest
SHA256 6830a3cb5565c7f2ed8f6c426a5a96120cc51086b89e9defa59869df47ad57a4
MD5 5cda33fef2023b6903f0087552bdb306
BLAKE2b-256 350f9a0283968167fd4bfaaf0f8dd2c629aa38acb3bebe43d3bba843fbda4132

See more details on using hashes here.

File details

Details for the file django_graphex-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: django_graphex-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 184.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_graphex-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e48b9cfe03e0cec41f2b592e7bd2d4931185ccd888cbcd9199b0d755afbe247
MD5 a1e1b1d4efeed18976d9fb6e62884aef
BLAKE2b-256 6f4198c53e39bd421452668bc1d70b5599a9b789c1c95ab9dd5821b01cafa6ea

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