Skip to content

Commit

Permalink
fix(mutations): Make sure we skip refetch when the optimizer is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
bellini666 committed Jan 7, 2025
1 parent 237c01e commit 06f62c7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion strawberry_django/mutations/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def arguments(self, value: list[StrawberryArgument]):
return args_prop.fset(self, value) # type: ignore

def refetch(self, resolved: _T, *, info: Info | None) -> _T:
if not DjangoOptimizerExtension.enabled or info is None:
if not DjangoOptimizerExtension.enabled.get() or info is None:
return resolved

if isinstance(resolved, list) and resolved:
Expand Down
10 changes: 7 additions & 3 deletions tests/test_input_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.exceptions import ValidationError
from strawberry.relay import from_base64, to_base64

from strawberry_django.optimizer import DjangoOptimizerExtension
from tests.utils import GraphQLTestClient, assert_num_queries

from .projects.faker import (
Expand Down Expand Up @@ -1028,7 +1029,7 @@ def test_input_nested_update_mutation(db, gql_client: GraphQLTestClient):
@pytest.mark.django_db(transaction=True)
def test_input_update_m2m_set_not_null_mutation(db, gql_client: GraphQLTestClient):
query = """
mutation UpdateProject ($input: ProjectInputPartial!) {
mutation UpdateProject ($input: ProjectInputPartial!, $optimizerEnabled: Boolean!) {
updateProject (input: $input) {
__typename
... on OperationInfo {
Expand All @@ -1042,7 +1043,7 @@ def test_input_update_m2m_set_not_null_mutation(db, gql_client: GraphQLTestClien
id
name
dueDate
isDelayed
isDelayed @include(if: $optimizerEnabled)
milestones {
id
name
Expand All @@ -1059,14 +1060,17 @@ def test_input_update_m2m_set_not_null_mutation(db, gql_client: GraphQLTestClien
milestone_1_id = to_base64("MilestoneType", milestone_1.pk)
MilestoneFactory.create(project=project)

with assert_num_queries(14):
# For mutations, having the optimizer enabled is expected to generate one extra
# query for the refetch of the object
with assert_num_queries(14 if DjangoOptimizerExtension.enabled.get() else 13):
res = gql_client.query(
query,
{
"input": {
"id": to_base64("ProjectType", project.pk),
"milestones": [{"id": milestone_1_id}],
},
"optimizerEnabled": DjangoOptimizerExtension.enabled.get(),
},
)

Expand Down

0 comments on commit 06f62c7

Please sign in to comment.