strawberry-django-plus
strawberry-django-plus copied to clipboard
more SQLs then expected
Hi,
Here is my schema and query. When I run the query, I get some extra SQL queries in Debug Toolbar, that seem unnecessary. Not sure what is invoking them and how to avoid them.
| + | SELECT ••• FROM "accounts_user" WHERE "accounts_user"."id" = '1' LIMIT 21 | | 2.24 | Sel Expl
| + | SELECT COUNT(*) AS "__count" FROM "marketplace_product" WHERE "marketplace_product"."author_id" = '1' | | 1.08 | Sel Expl
| + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."author_id" = '1' LIMIT 8 | | 0.41 | Sel Expl
| + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '659' LIMIT 21 7 similar queries. | | 0.36 | Sel Expl
| + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '660' LIMIT 21 7 similar queries. | | 0.34 | Sel Expl
| + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '661' LIMIT 21 7 similar queries. | | 0.34 | Sel Expl
| + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '662' LIMIT 21 7 similar queries. | | 0.37 | Sel Expl
| + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '663' LIMIT 21 7 similar queries. | | 0.35 | Sel Expl
| + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '664' LIMIT 21 7 similar queries. | | 0.62 | Sel Expl
| + | SELECT ••• FROM "marketplace_product" WHERE "marketplace_product"."id" = '665' LIMIT 21 7 similar queries. | | 0.46 | Sel Expl
3rd query gets me what I need (product's name), what are the queries below it? Here are 4 last lines from Debug Toolbar stack tract:
/usr/local/lib/python3.11/site-packages/strawberry_django_plus/utils/resolvers.py in wrapper(103)
return resolver(*args, **kwargs)
/usr/local/lib/python3.11/site-packages/strawberry_django_plus/field.py in resolve_connection(335)
return super().resolve_connection(
/usr/local/lib/python3.11/site-packages/strawberry_django_plus/relay.py in resolve_connection(1079)
return return_type.from_nodes(nodes, **kwargs)
/usr/local/lib/python3.11/site-packages/strawberry_django_plus/relay.py in from_nodes(770)
edges = [edge_class.from_node(v, cursor=start + i) for i, v in enumerate(iterator)]
#schema.py
@gql.django.type(User, filters=UserFilter, order=UserOrder)
class UserType(gql.relay.Node):
email: gql.auto
...
@gql.django.connection()
def products(self) -> List["ProductType"]:
return self.product_set.all()
@gql.django.type(Product, filters=ProductFilter, pagination=True)
class ProductType(gql.relay.Node):
name: gql.auto
...
#query
query MyQuery {
user {
products {
edges {
node {
name
}
}
}
}
}
Hey @tasiotas ,
I think that it is because the name products
is not matching the django model name, so the optimizer doesn't know where to find.
You can try 2 changes in there:
-
Define a
related_name="products
on the foreignkey so that it is accessible byself.products.all()
instead ofself.product_set.all()
. Since it matches the name of the field it should work -
If you don't want to set a
related_name
, try to define the field as:
@gql.django.connection(name="products")
def product_set(self) -> List["ProductType"]:
return self.product_set.all()
Let me know if any of those solutions works for you. Otherwise I might need some more info to understand what might be going on there
unfortunately both solutions didn't work. Will try to get you a repro.
Thanks!
unfortunately both solutions didn't work. Will try to get you a repro.
Thanks!
A repro will help a lot to understand how to properly optimize this :)
got the repro. Im new to Poetry, didn't know how to update this lib to latest, so its at 2.4.0 https://github.com/tasiotas/strawberry_debug
without optimizer I have 3 queries, with optimizer I get 6. Just create some Project and Milestone objects and run this query:
query MyQuery {
project(name: "one") {
milestones {
edges {
node {
name
}
}
}
}
}
Hey @tasiotas ,
I just released a new version, which has a major refactoring of the relay integration by using the official one instead.
Could you check if your issue still happens in that version? I think some of the changes I've made should have fixed this.
unfortunately, its still the same behavior in v3