Skip to main content

Shared Django Graphene utilities and DTOs for efficient API development

Project description

TarXemo Django Graphene Utils

Professional, modular, and high-performance GraphQL utilities for Django.

This package provides a standardized toolkit to accelerate GraphQL API development. It handles the "boring" parts—pagination, filtering, response standardization, and N+1 prevention—so you can focus on building features.


✨ Features

  • 🚀 Performance-First: Internal field caching and auto_optimize helper for N+1 query prevention.
  • 🛠 Modular & Flexible: Clean separation between responses, pagination, and filtering.
  • 🧠 Smart & Forgiving: Automatically handles both camelCase and snake_case inputs.
  • ✅ Structured Validation: Integrated support for Django ValidationError with standardized field-level error reporting.
  • ⚙️ Fully Configurable: Global settings support via Django settings.py.

📦 Installation

pip install tarxemo-django-graphene-utils

📖 Full Usage Guide

1. Standardized Responses

Every API should speak the same language. Use these builders to return consistent metadata to your frontend.

from tarxemo_django_graphene_utils import build_success_response, build_error, build_validation_error

def resolve_my_mutation(root, info, input):
    try:
        # Business logic here...
        return build_success_response("Item created successfully")
    except ValidationError as e:
        # Standardized field-level errors: {"field": "email", "message": "Invalid format"}
        return build_validation_error(e)
    except Exception as e:
        return build_error(str(e), code=9003)

2. High-Performance Data Retrieval

The core utility get_paginated_and_non_paginated_data handles everything from filtering to pagination in one call.

from tarxemo_django_graphene_utils import get_paginated_and_non_paginated_data, BaseFilterInput
import graphene

class MyFilter(BaseFilterInput):
    # Inherits: page_number, items_per_page, search_term, is_paged, is_active, order_by
    category = graphene.String()

def resolve_items(root, info, **kwargs):
    return get_paginated_and_non_paginated_data(
        model_or_queryset=MyModel, # Can also be MyModel.objects.select_related(...)
        filtering_object=kwargs,
        graphene_type=MyModelGrapheneType
    )

3. N+1 Prevention with auto_optimize

Prevent database bottlenecks by automatically applying select_related and prefetch_related based on requested fields.

from tarxemo_django_graphene_utils import auto_optimize

def resolve_list(root, info, **kwargs):
    # Determine requested fields from info or custom list
    requested = ['author', 'author__profile', 'tags']
    
    qs = MyModel.objects.all()
    optimized_qs = auto_optimize(qs, requested)
    
    return build_paged_list(optimized_qs, MyType, **kwargs)

4. Robust Object Mapping

Convert Django Models or Dicts to Graphene types with high-performance caching.

from tarxemo_django_graphene_utils import to_graphene

# Caches field discovery internally—extremely fast for large lists!
graph_obj = to_graphene(model_instance, MyGrapheneType)

5. Common GraphQL Types

Save time by using pre-defined common types:

  • BaseType: Includes id (UUID), created_at, updated_at, is_active.
  • LocationInput: latitude, longitude, radius.
  • DateRangeInput: start_date, end_date.
  • SortInput: field, reverse.

⚙️ Configuration

Customize the library behavior in your Django settings.py:

TARXEMO_UTILS_CONFIG = {
    'ITEMS_PER_PAGE': 50,
    'DEFAULT_SUCCESS_MESSAGE': "Done!",
    'DEFAULT_ERROR_MESSAGE': "Oops, something broke.",
    'RESPONSE_CODES': {
        2001: {"id": 2001, "status": True, "code": 700, "message": "Custom Event"},
    }
}

🧪 Development

  1. Install for development:

    pip install -e ".[dev]"
    
  2. Run Tests:

    pytest
    

📄 License

MIT © TarXemo

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

tarxemo_django_graphene_utils-0.2.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

tarxemo_django_graphene_utils-0.2.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file tarxemo_django_graphene_utils-0.2.0.tar.gz.

File metadata

File hashes

Hashes for tarxemo_django_graphene_utils-0.2.0.tar.gz
Algorithm Hash digest
SHA256 17238c6a668b56670f6fa17160abe7d92b3fa39a428be51eaf91b663722dc453
MD5 d8c453c84bb58d4c2fc6ec075cf5167e
BLAKE2b-256 5ab503f592295acd97e69a23f84a34641b6fed179e1158386883a2807727c4a8

See more details on using hashes here.

File details

Details for the file tarxemo_django_graphene_utils-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tarxemo_django_graphene_utils-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06f293e6b5d0a61f886f5bccbeb2720099da8b3974f33717887038f95c87eef1
MD5 7cd2e2dfb42427d8a66058698aedf425
BLAKE2b-256 86fc789fb6ec3953dc88bb9b599f680e0abe9d4292e8e3815a45f14d043f50ef

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