strawberry-graphql / strawberry-django

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

Querying two annotated fields at the same time return incorrect results #598

Closed pfcodes closed 4 months ago

pfcodes commented 4 months ago

Describe the Bug

When I query two different annotated fields at the same time, the results are inaccurate.

@strawberry_django.interface(PersonModel)
class Person(Node):
        parked_cars: int = strawberry_django.field(
            annotate=Count(
                "cars",
                filter=Q(cars__status="parked"),
            )
        )
    parked_trucks: int = strawberry_django.field(
        annotate=Count(
            "trucks",
            filter=Q(trucks__status="parked"),
        )
    )
{
  person(id: "personId") {
    parkedCars     # this will return the correct value
    # parkedTrucks  -- this is commented out
  }
}
{
  person(id: "personId") {
    parkedCars   # this will return an incorrect value
    parkedTrucks  # now that this is no longer commented out, parkedCars returns an incorrect value, the value of this field is also incorrect
  }
}

System Information

Additional Context

Upvote & Fund

Fund with Polar

pfcodes commented 4 months ago

This isn't a strawberry issue, the problem here was that distinct=True needed to be set. Might be a good thing to add to the docs.