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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for tarxemo_django_graphene_utils-0.2.2.tar.gz
Algorithm Hash digest
SHA256 e633fabd82d1151ceed24f78d94ae54cdad050d2cffe9b1b3bd3b0936bdd11d0
MD5 18e0a889bc48eca8680626bf8fd0c9b7
BLAKE2b-256 6cde402e015f70d9e7d6cbd81ae901417c014610a3f370bde4aa6d2f51759f5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tarxemo_django_graphene_utils-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6a6873910301186c69fcbbb6baf26ea8fbcad26c5c22f8ec38a10ae201acaa69
MD5 2798f2ffc9fa77493e15d0fd5880b0f3
BLAKE2b-256 d1b868102a2418755b57a2c2d9da764f5c831ae3e09b38918efb651dff2b8b54

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