insolite / graphene-peewee-async

Graphene peewee-async integration
37 stars 9 forks source link

What about N+1 problem? #1

Closed art1415926535 closed 6 years ago

art1415926535 commented 6 years ago

I tried to use sqlalchemy_bulk_lazy_loader to work with SQLAlchemy. It works well. How am I can resolve this problem with graphene-peewee-async?

insolite commented 6 years ago

It is partially solved using joins where possible. If you have parent entity with foreign key to the child entity then it's fine, the query will be built using joins. For example, Book -> Author -> Country will end up in SELECT with 2 joins. You can even describe any count of children on the same level, even the same entity (using aliases) - they will be attached to the query using peewee's switch/join. However, things aren't so good for the reversed query when there are child arrays: Country -> Authors -> Books. In pure peewee there is an aggregate_rows feature that builds complex SELECT query without grouping and then resolving child arrays locally. But unfortunately that is not yet implemented for peewee-async (https://github.com/05bit/peewee-async/issues/10). I've made some tries on my own but with no success.

insolite commented 6 years ago

To be more concrete: This query: https://github.com/insolite/graphene-peewee-async/blob/533c1b9c5224d1abb4dbf99b5ead76629e9439a7/tests/test_api/test_query.py#L50-L67 will result in SQL:

SELECT "t1"."id", "t1"."name", "t1"."year", "t1"."author_id", "t2"."id", "t2"."name", "t2"."rating", Count(*) Over() AS __total__
FROM "book" AS t1
LEFT OUTER JOIN "author" AS t2 ON ("t1"."author_id" = "t2"."id")
WHERE ("t2"."rating" = 42)
ORDER BY "t1"."year"

However we can't do authors { edges { node { books } } }