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.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 340744350b324a0bee57316844b540b68b112f3c92644dc0cc3b58776cef77d6 |
|
MD5 | 073587861474e71fdd42cd9493776ae0 |
|
BLAKE2b-256 | bb8a36c8cb0ede3e927bfab070a25b397322dee1b9673d5e8eadd38f56f82166 |
Close
Hashes for graphene_django_optimizer-0.3.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74404db76d8df1d05103ae9138a37d585888609e2eaaf65ce12eacb074fcc5aa |
|
MD5 | 179b95885b667320914d597e4c8e54af |
|
BLAKE2b-256 | d4e2da3bb7aefd7e1c364cf38634cb59153ebd6724855e92f83aa5f2ac88ddd6 |