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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b6853885ac26d8d9976d4afd74696e634c8013f8e69daa6f6f9e1b9f6b7e562 |
|
MD5 | f1d19d53b05c87ae843f9d830edd01b8 |
|
BLAKE2b-256 | 532dd1d314fed5afb01ff87dc8a058b1da693787d8385b3f21c2749a53b1a236 |
Close
Hashes for graphene_django_optimizer-0.3.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9440045340d82ea1743d6e6d5de20433da32d69457c9dc1429dccfabe749ade1 |
|
MD5 | 6aaea56f880a009ceca7560865354e24 |
|
BLAKE2b-256 | ed8eb72ed0a9fa1c344f0be89da004d69e1a2f193a30113ea04e3614a5cda926 |