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.2.tar.gz (212.2 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.2-py3-none-any.whl (280.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: undine-0.3.2.tar.gz
  • Upload date:
  • Size: 212.2 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.2.tar.gz
Algorithm Hash digest
SHA256 bc10b6237b61f4fcd46cc2ea3365c3cf8cff26b6b7efd950c819961a6507bf96
MD5 41188dc829cb851dee3202b20800da65
BLAKE2b-256 179cc50c197b677278963078804221e5fa348cf33bf804361eac506cad457cec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: undine-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 280.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fa416217b10c9c091afa4f69fc95aa8f32d7532a8358517bde71db201ff17178
MD5 f6e1ce1c4d036b0bb28828e8db658540
BLAKE2b-256 81985edd42614066bd3aedefec7eb84bbc8595c42eae1b6c799d5a3c601b6f53

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