Skip to main content

A GraphQL library for Django

Project description

Undine - GraphQL for Django

Coverage Status GitHub Workflow Status PyPI GitHub GitHub Last Commit GitHub Issues Downloads Python Version Django Version

pip install undine

Documentation: https://mrthearman.github.io/undine/

Source Code: https://github.com/MrThearMan/undine/

Contributing: https://mrthearman.github.io/undine/contributing/


Undine is a GraphQL library for Django. It's designed to be easy to use and extend while providing out-of-the-box solutions for many common issues GraphQL developers face.

Feature highlights:

  • Automatic generation of GraphQL types from Django models
  • Automatic query optimization
  • Logically composable filtering
  • Ordering based on enums
  • Single and bulk mutations, including relations
  • Hidden and input-only mutation inputs
  • Built-in permission and validation hooks
  • Support for Relay Global object IDs and Connection pagination
  • File uploads based on GraphQL multipart request specification
  • Support for asynchronous execution and DataLoaders
  • Subscriptions with WebSockets, Server-Sent Events, or Multipart HTTP
  • Server-side query caching
  • Optional persisted documents support
  • Lifecycle hooks for customizing the GraphQL request cycle
  • Hiding fields and types from schema (experimental)
  • Built-in testing tools

Check out the Tutorial to get started.

import asyncio
from collections.abc import AsyncIterator
from typing import Any

from undine import (
    Entrypoint,
    Field,
    Filter,
    FilterSet,
    GQLInfo,
    Input,
    MutationType,
    Order,
    OrderSet,
    QueryType,
    RootType,
    create_schema,
)
from undine.exceptions import GraphQLPermissionError, GraphQLValidationError
from undine.relay import Connection, Node
from undine.subscriptions import ModelCreateSubscription

from .models import Task


class TaskFilterSet(FilterSet[Task]):
    name = Filter(lookup="icontains")
    done = Filter()


class TaskOrderSet(OrderSet[Task]):
    id = Order()
    name = Order(null_placement="last")


@Node
@TaskFilterSet
@TaskOrderSet
class TaskType(QueryType[Task], schema_name="Task"):
    pk = Field()
    name = Field()

    @name.permissions
    def name_permissions(self, instance: Task, info: GQLInfo) -> None:
        if not info.context.user.is_authenticated:
            raise GraphQLPermissionError


class TaskCreateMutation(MutationType[Task]):
    name = Input()
    done = Input(default_value=False)

    @classmethod
    def __permissions__(cls, instance: Task, info: GQLInfo, input_data: dict[str, Any]) -> None:
        if not info.context.user.is_staff:
            msg = "Only staff members can create tasks"
            raise GraphQLPermissionError(msg)

    @classmethod
    def __validate__(cls, instance: Task, info: GQLInfo, input_data: dict[str, Any]) -> None:
        if len(input_data["name"]) < 3:
            msg = "Task name must be at least 3 characters"
            raise GraphQLValidationError(msg)


class Query(RootType):
    node = Entrypoint(Node)
    task = Entrypoint(TaskType, cache_time=10)
    tasks = Entrypoint(Connection(TaskType))


class Mutation(RootType):
    create_task = Entrypoint(TaskCreateMutation)
    bulk_create_tasks = Entrypoint(TaskCreateMutation, many=True)


class Subscription(RootType):
    task_created = Entrypoint(ModelCreateSubscription(TaskType))

    @Entrypoint
    async def countdown(self, info: GQLInfo) -> AsyncIterator[int]:
        for i in range(10, -1, -1):
            yield i
            await asyncio.sleep(1)


schema = create_schema(query=Query, mutation=Mutation, subscription=Subscription)

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

undine-0.3.5.tar.gz (224.6 kB view details)

Uploaded Source

Built Distribution

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

undine-0.3.5-py3-none-any.whl (299.3 kB view details)

Uploaded Python 3

File details

Details for the file undine-0.3.5.tar.gz.

File metadata

  • Download URL: undine-0.3.5.tar.gz
  • Upload date:
  • Size: 224.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Linux/6.17.0-1008-azure

File hashes

Hashes for undine-0.3.5.tar.gz
Algorithm Hash digest
SHA256 6507310cd1fa5434f1c10b11f05cb3a41e4b37db69c6d6ee4f73932210558cf8
MD5 68dbbf74e9ed7b88c542d6b4117fafb5
BLAKE2b-256 167f9b080ce8fb600026271cad4e428fa123dab37fdf9f8d2f33c17c35995fab

See more details on using hashes here.

File details

Details for the file undine-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: undine-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 299.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Linux/6.17.0-1008-azure

File hashes

Hashes for undine-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6da34b39709ff78dc3b2a2bea9f0ef7eb021a670641dec8dd70a8370e6d0d9b1
MD5 b6814ff4d2336242940f31b9ca0ee9c7
BLAKE2b-256 df69488e36bb72aa4e95391645c9759435106f078f3fa736cbdd5730619ba006

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