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.3.tar.gz (214.3 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.3-py3-none-any.whl (283.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: undine-0.3.3.tar.gz
  • Upload date:
  • Size: 214.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Linux/6.14.0-1017-azure

File hashes

Hashes for undine-0.3.3.tar.gz
Algorithm Hash digest
SHA256 2688b716b3a8f6417b6debcf467ae7fd3a6683ae51cb96ce89dbf64a1912a2fa
MD5 18d6db2823988a6736ed1a98e1f315ba
BLAKE2b-256 347386fe4ea19849ee62788392497d5ed8d8afe898b6c16b4b151ce40d4f1688

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for undine-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 36c340a01df17cafec23a95a10305af1f3f89c1408243f5cd2e7007a811a6c20
MD5 7c99f560fcae96b4c8e69a11ed132f50
BLAKE2b-256 3c27090f7ff0b8314ad5f68b662e45f5453b4499958acb004a60c8fb13b9683a

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