Optimize database access inside graphene queries.
Project description
graphene-django-optimizer
Optimize queries executed by graphene-django automatically, using select_related
, prefetch_related
and only
methods of Django QuerySet.
Install
pip install graphene-django-optimizer
Usage
Having the following schema based on the tutorial of graphene-django (notice the use of gql_optimizer
)
# cookbook/ingredients/schema.py
import graphene
from graphene_django.types import DjangoObjectType
import graphene_django_optimizer as gql_optimizer
from cookbook.ingredients.models import Category, Ingredient
class CategoryType(DjangoObjectType):
class Meta:
model = Category
class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient
class Query(object):
all_categories = graphene.List(CategoryType)
all_ingredients = graphene.List(IngredientType)
def resolve_all_categories(root, info):
return gql_optimizer.query(Category.objects.all(), info)
def resolve_all_ingredients(root, info):
return gql_optimizer.query(Ingredient.objects.all(), info)
We will show some graphql queries and the queryset that will be executed.
Fetching all the ingredients with the related category:
{
all_ingredients {
id
name
category {
id
name
}
}
}
# optimized queryset:
ingredients = (
Ingredient.objects
.select_related('category')
.only('id', 'name', 'category__id', 'category__name')
)
Fetching all the categories with the related ingredients:
{
all_categories {
id
name
ingredients {
id
name
}
}
}
# optimized queryset:
categories = (
Category.objects
.only('id', 'name')
.prefetch_related(Prefetch(
'ingredients',
queryset=Ingredient.objects.only('id', 'name'),
))
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for graphene-django-optimizer-0.3.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 332f48d318e18dc58f89deaf79d51a0d25e5499d04d839a255bfac24dcac42e5 |
|
MD5 | 9e9087498a97666f6668d1f31d297b4b |
|
BLAKE2b-256 | 2b1d5c252caa9ea9df212aedb6630cca67ce9042662efa83930a06b0fab2605e |
Close
Hashes for graphene_django_optimizer-0.3.5-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f104e3cfba65f3aef591bf3a9859915abfa074eccb4508e5af19a90a3091f9ee |
|
MD5 | 77b22c6f847d42e6685768efbdc72103 |
|
BLAKE2b-256 | 511bc76086e6d8fc2f0719fb2d7c80430502d8cf34d195a858fd560d055e653d |