Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unit test for graphene pr#1412 #1315

Conversation

tcleonard
Copy link
Collaborator

Thomas Leonard and others added 2 commits March 20, 2022 21:44
@tcleonard tcleonard force-pushed the users/tcleonard/fix-test-for-graphene-PR#1412 branch 3 times, most recently from 9b3a117 to 9b0af18 Compare March 20, 2022 22:06
@tcleonard tcleonard force-pushed the users/tcleonard/fix-test-for-graphene-PR#1412 branch from 9b0af18 to ca55529 Compare March 20, 2022 22:15
@firaskafri
Copy link
Collaborator

graphql-python/graphene#1412 is merged

@firaskafri firaskafri self-requested a review September 22, 2022 17:20
@firaskafri firaskafri merged commit 5d81ba0 into graphql-python:main Sep 23, 2022
firaskafri added a commit that referenced this pull request Sep 23, 2022
resolver = super().wrap_resolve(parent_resolver)

def custom_resolver(root, info, **args):
fk_obj = resolver(root, info, **args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You run the resolver, get a result (django model) back and then run _type.get_node to fetch it again making a 2nd unnecessary query?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an issue I've also been getting, queries are forced every time an object is resolved, making an exponential number of queries when nesting without any possibility of optimizing

Copy link
Collaborator

@mahdyhamad mahdyhamad Mar 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fairly new to this code base and I am trying to resolve the issue raised by @ekampf

In my attempt to fix this:

  • I started by getting the id of the field to be resolved (which is the foreign-key object) from the root in an attempt to stop calling one of the functions that resulting in a hit to the database.

  • Then I check the resolver

    • No Custom resolver
      • if the resolver equals to the default resolver (meaning that there is no custom resolver for the field in the root node), call get_node and return.
      • At this point and this scenario, the issue is solved, because there is one resolver that was called resulting in one hit to the database and thats fine!
    • Custom Resolver Exists
      • At this point, there is a custom resolver, which means that the node should go through multiple resolvers: the resolver method + get_node (I say should because the tests and the code written to make sure that the processes of resolving the node always goes through the get_node)
      • ** In this case, I did not include when the resolver is awaitable
  • But the wall I faced after that is the fact that if is required to call get_node even though the node is resolved as a result of calling the resolver function. Which stopped me for a second, why there are two functions responsible for resolving the

@tcleonard any thoughts how this can be fixed?

I think this is not the right way to assure that get_node is always called. The solution should be more concrete.
I was thinking of a way to pass the instance returned from get_node to the resolver function if node exists. Because there is no reason to get the instance 2 times. But I am not sure how this is can be done.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out the issues, and for attempting to fix! In the meantime until a solution can be worked out that doesn't cause N+1 queries and doesn't prevent query-optimization (assuming the original PR author or someone else do care to support the original intention of this PR), I've opened a PR to revert the problematic code here #1401, since this caused a significant regression relative to graphene-django v3.0.0b7 is a blocker for many people looking to upgrade to graphene-django v3.

@grantmcconnaughey
Copy link

@firaskafri Would it be possible to revert this PR? It seems to have caused unfixable N+1 issues, even when using select_related, for foreign key fields. It'll be a blocker for using graphene-django 3 for a lot of people, and it's not obvious this is happening unless you use something like django-silk to uncover the huge increase in queries.

sjdemartini added a commit to sjdemartini/graphene-django that referenced this pull request Apr 29, 2023
This reverts the change to `convert_field_to_djangomodel` introduced in
graphql-python#1315 for the
reasons discussed here
https://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857.
As mentioned there, without reverting this code, "queries are forced
every time an object is resolved, making an exponential number of
queries when nesting without any possibility of optimizing".

That regression prevented `graphene-django-optimizer` from working with
`graphene-django` v3.0.0b9+ (where this change first was published), as
discussed in
graphql-python#1356 (comment),
tfoxy/graphene-django-optimizer#86, and
tfoxy/graphene-django-optimizer#83 (comment).

For now, this marks the two tests that depended on this problematic code
as "expected to fail", and perhaps they can be reintroduced if there's a
way to support this logic in a way that does not prevent
`select_related` and `prefetch_related` query-optimization and introduce
nested N+1s.

As mentioned here
graphql-python#1315 (comment),
this is blocking upgrade to graphene-django v3 for many users, and
fixing this would allow many to begin upgrading and contributing to keep
graphene-django going.
sjdemartini added a commit to sjdemartini/graphene-django that referenced this pull request May 2, 2023
…related

This test passes after reverting the `CustomField` resolver change
introduced in
graphql-python#1315, but fails
with that resolver code present. For instance, adding back the resolver
code gives a test failure showing:

```
Failed: Expected to perform 2 queries but 11 were done
```

This should ensure there aren't regressions that prevent
query-optimization in the future.
sjdemartini added a commit to sjdemartini/graphene-django that referenced this pull request May 2, 2023
…related

This test passes after reverting the `CustomField` resolver change
introduced in
graphql-python#1315, but fails
with that resolver code present. For instance, adding back the resolver
code gives a test failure showing:

```
Failed: Expected to perform 2 queries but 11 were done
```

This should ensure there aren't regressions that prevent
query-optimization in the future.
sjdemartini added a commit to sjdemartini/graphene-django that referenced this pull request May 2, 2023
…related

This test passes after reverting the `CustomField` resolver change
introduced in
graphql-python#1315, but fails
with that resolver code present. For instance, adding back the resolver
code gives a test failure showing:

```
Failed: Expected to perform 2 queries but 11 were done
```

This should ensure there aren't regressions that prevent
query-optimization in the future.
firaskafri pushed a commit that referenced this pull request May 3, 2023
This reverts the change to `convert_field_to_djangomodel` introduced in
#1315 for the
reasons discussed here
https://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857.
As mentioned there, without reverting this code, "queries are forced
every time an object is resolved, making an exponential number of
queries when nesting without any possibility of optimizing".

That regression prevented `graphene-django-optimizer` from working with
`graphene-django` v3.0.0b9+ (where this change first was published), as
discussed in
#1356 (comment),
tfoxy/graphene-django-optimizer#86, and
tfoxy/graphene-django-optimizer#83 (comment).

For now, this marks the two tests that depended on this problematic code
as "expected to fail", and perhaps they can be reintroduced if there's a
way to support this logic in a way that does not prevent
`select_related` and `prefetch_related` query-optimization and introduce
nested N+1s.

As mentioned here
#1315 (comment),
this is blocking upgrade to graphene-django v3 for many users, and
fixing this would allow many to begin upgrading and contributing to keep
graphene-django going.
firaskafri pushed a commit that referenced this pull request May 3, 2023
…related

This test passes after reverting the `CustomField` resolver change
introduced in
#1315, but fails
with that resolver code present. For instance, adding back the resolver
code gives a test failure showing:

```
Failed: Expected to perform 2 queries but 11 were done
```

This should ensure there aren't regressions that prevent
query-optimization in the future.
@firaskafri firaskafri mentioned this pull request May 3, 2023
@firaskafri
Copy link
Collaborator

firaskafri commented May 3, 2023

@grantmcconnaughey this was reverted and released in 3.0.2.

Just a heads up for folks who use get_queryset as an auth layer. If you need that, you can stick with versions between >=3.0.0b9 and <3.0.2 for now. Feel free to suggest a different way that won't lead to those pesky N+1 SQL query issues.

@grantmcconnaughey
Copy link

@firaskafri Thank you! 🎉

spartacus0816 pushed a commit to spartacus0816/django_dev that referenced this pull request May 22, 2023
This reverts the change to `convert_field_to_djangomodel` introduced in
graphql-python/graphene-django#1315 for the
reasons discussed here
https://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857.
As mentioned there, without reverting this code, "queries are forced
every time an object is resolved, making an exponential number of
queries when nesting without any possibility of optimizing".

That regression prevented `graphene-django-optimizer` from working with
`graphene-django` v3.0.0b9+ (where this change first was published), as
discussed in
graphql-python/graphene-django#1356 (comment),
tfoxy/graphene-django-optimizer#86, and
tfoxy/graphene-django-optimizer#83 (comment).

For now, this marks the two tests that depended on this problematic code
as "expected to fail", and perhaps they can be reintroduced if there's a
way to support this logic in a way that does not prevent
`select_related` and `prefetch_related` query-optimization and introduce
nested N+1s.

As mentioned here
graphql-python/graphene-django#1315 (comment),
this is blocking upgrade to graphene-django v3 for many users, and
fixing this would allow many to begin upgrading and contributing to keep
graphene-django going.
spartacus0816 pushed a commit to spartacus0816/django_dev that referenced this pull request May 22, 2023
…related

This test passes after reverting the `CustomField` resolver change
introduced in
graphql-python/graphene-django#1315, but fails
with that resolver code present. For instance, adding back the resolver
code gives a test failure showing:

```
Failed: Expected to perform 2 queries but 11 were done
```

This should ensure there aren't regressions that prevent
query-optimization in the future.
azuredevil0818 pushed a commit to azuredevil0818/graphene-django that referenced this pull request Jul 4, 2023
This reverts the change to `convert_field_to_djangomodel` introduced in
graphql-python/graphene-django#1315 for the
reasons discussed here
https://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857.
As mentioned there, without reverting this code, "queries are forced
every time an object is resolved, making an exponential number of
queries when nesting without any possibility of optimizing".

That regression prevented `graphene-django-optimizer` from working with
`graphene-django` v3.0.0b9+ (where this change first was published), as
discussed in
graphql-python/graphene-django#1356 (comment),
tfoxy/graphene-django-optimizer#86, and
tfoxy/graphene-django-optimizer#83 (comment).

For now, this marks the two tests that depended on this problematic code
as "expected to fail", and perhaps they can be reintroduced if there's a
way to support this logic in a way that does not prevent
`select_related` and `prefetch_related` query-optimization and introduce
nested N+1s.

As mentioned here
graphql-python/graphene-django#1315 (comment),
this is blocking upgrade to graphene-django v3 for many users, and
fixing this would allow many to begin upgrading and contributing to keep
graphene-django going.
azuredevil0818 pushed a commit to azuredevil0818/graphene-django that referenced this pull request Jul 4, 2023
…related

This test passes after reverting the `CustomField` resolver change
introduced in
graphql-python/graphene-django#1315, but fails
with that resolver code present. For instance, adding back the resolver
code gives a test failure showing:

```
Failed: Expected to perform 2 queries but 11 were done
```

This should ensure there aren't regressions that prevent
query-optimization in the future.
superlevure pushed a commit to loft-orbital/graphene-django that referenced this pull request Jul 19, 2023
This reverts the change to `convert_field_to_djangomodel` introduced in
graphql-python#1315 for the
reasons discussed here
https://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857.
As mentioned there, without reverting this code, "queries are forced
every time an object is resolved, making an exponential number of
queries when nesting without any possibility of optimizing".

That regression prevented `graphene-django-optimizer` from working with
`graphene-django` v3.0.0b9+ (where this change first was published), as
discussed in
graphql-python#1356 (comment),
tfoxy/graphene-django-optimizer#86, and
tfoxy/graphene-django-optimizer#83 (comment).

For now, this marks the two tests that depended on this problematic code
as "expected to fail", and perhaps they can be reintroduced if there's a
way to support this logic in a way that does not prevent
`select_related` and `prefetch_related` query-optimization and introduce
nested N+1s.

As mentioned here
graphql-python#1315 (comment),
this is blocking upgrade to graphene-django v3 for many users, and
fixing this would allow many to begin upgrading and contributing to keep
graphene-django going.
superlevure pushed a commit to loft-orbital/graphene-django that referenced this pull request Jul 19, 2023
…related

This test passes after reverting the `CustomField` resolver change
introduced in
graphql-python#1315, but fails
with that resolver code present. For instance, adding back the resolver
code gives a test failure showing:

```
Failed: Expected to perform 2 queries but 11 were done
```

This should ensure there aren't regressions that prevent
query-optimization in the future.
@superlevure superlevure deleted the users/tcleonard/fix-test-for-graphene-PR#1412 branch August 10, 2023 21:18
DevStar1016 added a commit to DevStar1016/graphine-django that referenced this pull request Sep 11, 2023
This reverts the change to `convert_field_to_djangomodel` introduced in
graphql-python/graphene-django#1315 for the
reasons discussed here
https://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857.
As mentioned there, without reverting this code, "queries are forced
every time an object is resolved, making an exponential number of
queries when nesting without any possibility of optimizing".

That regression prevented `graphene-django-optimizer` from working with
`graphene-django` v3.0.0b9+ (where this change first was published), as
discussed in
graphql-python/graphene-django#1356 (comment),
tfoxy/graphene-django-optimizer#86, and
tfoxy/graphene-django-optimizer#83 (comment).

For now, this marks the two tests that depended on this problematic code
as "expected to fail", and perhaps they can be reintroduced if there's a
way to support this logic in a way that does not prevent
`select_related` and `prefetch_related` query-optimization and introduce
nested N+1s.

As mentioned here
graphql-python/graphene-django#1315 (comment),
this is blocking upgrade to graphene-django v3 for many users, and
fixing this would allow many to begin upgrading and contributing to keep
graphene-django going.
DevStar1016 added a commit to DevStar1016/graphine-django that referenced this pull request Sep 11, 2023
…related

This test passes after reverting the `CustomField` resolver change
introduced in
graphql-python/graphene-django#1315, but fails
with that resolver code present. For instance, adding back the resolver
code gives a test failure showing:

```
Failed: Expected to perform 2 queries but 11 were done
```

This should ensure there aren't regressions that prevent
query-optimization in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants