Optimize database access inside graphene queries.
Project description
graphene-django-optimizer
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
}
}
}
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
}
}
}
categories = (
Category.objects
.only('id', 'name')
.prefetch_related(Prefetch(
'ingredients',
queryset=Ingredient.objects.only('id', 'name'),
))
)
Contribute
The system must have installed:
- python 3
- virtualenv
virtualenv -p python3 venv
. venv/bin/activate
pip install -r dev-requirements.txt
# run tests:
python setup.py test
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.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a7f25e2035f500d0777bd6d72220b2d5005291934b2ed3e02114d9cb33997b2 |
|
MD5 | c62cc8764eb9ce913dc921bb3927ebde |
|
BLAKE2b-256 | a53ed593e5ab163f35b1a98f7b0416cee799d5a38a23340a56651bc60f09b455 |
Close
Hashes for graphene_django_optimizer-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdf505236707ab0ecb644629c401c4307e1de5609ee555d329a9a22e17a931bf |
|
MD5 | 1835dd92e7142bd027920c85cd2e90db |
|
BLAKE2b-256 | ba01ab0a3d17cfa2fd05f8dceed9b3c640687a07d479737dd74674edef766ae5 |