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.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc49f095a57ca8d658b38aedc618376466e42f19641679563ece629b78d37cb0 |
|
MD5 | 2d225b2c4bed8a2f7a8e0d8bb34869e4 |
|
BLAKE2b-256 | 3b0363da12efcaed63f9a5947d91cb09dfe287ad69b7175f71cc986e27594e0f |
Close
Hashes for graphene_django_optimizer-0.3.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76fd429e69b555d2aecadd85b0a7f08527ba84962160e2d7f21ab3c28f6a8301 |
|
MD5 | 21b2173c54e9f646a617f6edbf90b8e3 |
|
BLAKE2b-256 | 50a3718518d45f80c3b3f3a3f3a50201e8c3cb93eceea91ac4e334e902e83684 |