strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
409 stars 118 forks source link

Interface optimization hints doesn't get applied to all implementations #420

Open SafaAlfulaij opened 10 months ago

SafaAlfulaij commented 10 months ago

Describe the Bug

Note: all the following is pseudo code.

Given the following models:

class BaseModel(models.Model):
    creation_user = models.ForeignKey("User", related_name="+", on_delete=models.CASCADE)

    class Meta:
        abstract = True

class Pizza(BaseModel):
    title = models.CharField(max_length=20)

class Order(BaseModel):
    notes = models.CharField(max_length=20)

And the fields:

@strawberry.django.interface(models.BaseModel)
class BaseRecord:
    created_by: "User" = field(select_related=['creation_user'])

@strawberry.django.field(models.Pizza)
class Pizza(BaseRecord, relay.Node):
    name: str

@strawberry.django.field(models.Order)
class Order(BaseRecord, relay.Node):
    notes: str

When querying pizza objects like so:

{
  pizzaObjs {
    createdBy { username }
  }
}

Optimization hints are applied (select_related).

But when querying order objects like so:

{
  orderObjs {
    createdBy { username }
  }
}

Optimization hints are not applied.

After debugging, it seems an issue around these lines: https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/strawberry_django/type.py#L350-L366

Where the first implementation (Pizza) would get the created_by field defined, and origin_django_type set. When the next implementation (Order) tries to get the created_by field defined, it does but as the field has defined origin_django_type, it tried to recreate the field (else clause) and ignore all the hints.

System Information

Upvote & Fund

Fund with Polar

bellini666 commented 3 months ago

Hi @SafaAlfulaij ,

The optimizer has gone through major changes recently. Is this still an issue in the latest release?