Quickstart🔗
The database schema these examples will be using can be seen here.
Let's say we have defined a graphql schema like this:
Now, based on our database schema, we want to make a query like this:
As is, this query will result in:
- 1 query for all apartments
- 1 query or each sale
- 1 query for each ownership in each sale
- 1 query for each owner in each ownership in each sale
Let's say that we have:
- a modest 20 apartments
- each apartment has 3 sales
- each sale has 2 ownerships
In total, that's...
It's important to notice, that the amount of queries is proportional to the amount of records in our database, so the number of queries is only going to increase. This is called an N+1 problem.
We are also over-fetching all fields on each model, and thus not taking advantage of GraphQLs schema at all.
This is the issue this library hopes to solve.
Shoutout to graphene-django-optimizer, which inspired this library. The library seem to no longer work in modern versions of
graphene-django. Hopefully this library can replace it, while offering a cleaner API.
We can optimize this query by simply using DjangoObjectType from query_optimizer
instead of graphene_django
We could also use the optimize function to wrap a custom resolver queryset:
That's it!
With the following configuration, the same query will result in just 3 database queries, regardless of the number of database records.
- 1 query for all apartments
- 1 query for all sales in all apartments
- 1 query for all ownerships with their owners for each sale in each apartment
Also, the optimization will only fetch the fields given in the GraphQL query, as the query intended.
See technical details on how this works.