strawberry-graphql / strawberry-django

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

Polymorphic interfaces break prefetch when a nested field has filters applied #593

Open pfcodes opened 4 months ago

pfcodes commented 4 months ago

Describe the Bug

Polymorphic interfaces break the prefetch cache when a nested field has filters/ordering applied.

This works:

{
  allIndividualCreatives { # Not polymorphic
    edges {
      node {
        id
        creativeWorkPortfolioProjects(order: { profileOrder: ASC}) { # ✅  Filter works without breaking prefetching
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }
}

This also works:

{
  jobPosting(id: "Q3JlYXRpdmVKb2JQb3N0aW5nOjMyNQ==") {
    applications {
      edges {
        node {
          id
          creative { # Polymorphic
            creativeWorkPortfolioProjects { # ✅ Nested three levels deep, but prefetch doesn't break
              edges {
                node {
                  id
                }
              }
            }
          }
        }
      }
    }
  }
}

This doesn't:

{
  jobPosting(id: "Q3JlYXRpdmVKb2JQb3N0aW5nOjMyNQ==") {
    applications {
      edges {
        node {
          id
          creative { # Polymorphic
            creativeWorkPortfolioProjects(order: { profileOrder: ASC}) { # ❌ Nested 3 levels deep and prefetch breaks because of the filter
              edges {
                node {
                  id
                }
              }
            }
          }
        }
      }
    }
  }
}

System Information

Additional Context

Apparently the prefetched cache is being sent to the subclass, but the filters are being applied to the parent class. I know this because I see error messages about an n+1 query on the parent class only when applying the filters.

Upvote & Fund

Fund with Polar