Registry / database / graphene-django-query-optimizer

graphene-django-query-optimizer

JSON →
library0.10.15pypypiunverified

Automatically optimizes SQL queries in Graphene-Django schemas by using Django's prefetch_related and select_related based on the GraphQL query. Version 0.10.15, actively maintained, monthly releases.

pip install graphene-django-query-optimizer
INSTALL
IMPORT
SIG · GRAPHENE-DJANGO-QU
G
graphene-django-query-optimizer
databasepythonv0.10.15
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GQLOptimizerMiddleware
from query_optimizer import GQLOptimizerMiddleware
from graphene_django_optimizer import GQLOptimizerMiddleware

Define a type using OptimizedDjangoObjectType and add the middleware to Graphene settings. The resolver returns a queryset; optimization is automatic.

import graphene from graphene_django import DjangoObjectType from graphene_django_optimizer import OptimizedDjangoObjectType, GQLOptimizerMiddleware from .models import MyModel class MyModelType(OptimizedDjangoObjectType): class Meta: model = MyModel class Query(graphene.ObjectType): my_models = graphene.List(MyModelType) def resolve_my_models(self, info): return MyModel.objects.all() schema = graphene.Schema(query=Query) # Add middleware to Django settings: 'GRAPHENE': {'MIDDLEWARE': [GQLOptimizerMiddleware]} # Then GraphQL queries will automatically optimize SQL.
Debug
Known issues
gotchaThe middleware must be added to the GRAPHENE['MIDDLEWARE'] setting, not simply imported. Missing this step results in no optimization.
fix
Ensure 'GRAPHENE' in settings.py includes: 'MIDDLEWARE': ['graphene_django_optimizer.GQLOptimizerMiddleware']
affects: all
gotchaOnly Django model fields (via DjangoObjectType) are optimized. Custom resolvers or non-model fields bypass optimization.
fix
Use OptimizedDjangoObjectType for model types; for other fields, you may need manual optimization or use gql_optimizer.resolver_hints.
affects: all
gotchaPrefetch optimization may not work correctly if the queryset uses .distinct() or .annotate() with subqueries. The optimizer might apply prefetches that conflict.
fix
Test with complex queries; consider using .defer() or .only() explicitly if issues arise.
affects: <=0.10.15
breakingVersion 0.10.0 dropped support for Python 3.8-3.10. If using older Python, you must pin to <0.10.0.
fix
Upgrade Python to >=3.11 or pin version: pip install 'graphene-django-query-optimizer<0.10.0'
affects: >=0.10.0
Upgrade
Version history
0.10.15latest on PyPI · released Sep 29, 2025
Audit
Dependencies
graphene-djangorequiredRequired integration with Graphene-Django schemas.
DjangorequiredRequired for Django models and query optimization.
Agent activity
6 hits · last 30 days
node
6
Resources
graphene-django-query-optimizer — pip install graphene-django-query-optimizer · libregistry