Skip to content

Conversation

@alissn
Copy link
Contributor

@alissn alissn commented Oct 24, 2025

Summary

Hi,

In this pull request, I’ve added support for eager loading nested relations within Union types when using the SelectFields class.

Example

Consider a scenario where you have a Comment model that is morphable to both Post and Product models.
Each of these models has a file relation, and each file belongs to a folder.

Previously, when selecting nested relations for different Union types, the eager loading for deeper relations (like file.folder) was ignored, leading to N+1 query issues.
Reference: SelectFields.php#L254-L258

Example Query

{
  comments {
    id
    title
    body
    commentable {
      ... on Post {
        id
        title
        file {
          id
          name
          path
          folder {
            id
            name
          }
        }
      }
      ... on Product {
        id
        name
        price
        file {
          id
          name
          path
          folder {
            id
            name
          }
        }
      }
    }
  }
}

In the above query, the file and folder relations for both Post and Product models were not properly eager-loaded, resulting in multiple unnecessary queries.

Fix

This pull request resolves the issue by leveraging Constrained Eager Loading of MorphTo Relationships, ensuring that nested relations within Union types are loaded efficiently without N+1 problems.


Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)g
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Misc. change (internal, infrastructure, maintenance, etc.)

Checklist

  • Existing tests have been updated and/or new tests have been added
  • Add a CHANGELOG.md entry
  • Update the README.md if needed
  • Code style has been fixed via composer fix-style

@alissn alissn changed the title **Fix N+1 Query Issue with Nested Eager Loading on Union Types** Fix N+1 Query Issue with Nested Eager Loading on Union Types Oct 24, 2025
@alissn
Copy link
Contributor Author

alissn commented Oct 24, 2025

Hi @mfn 👋,

I need some help regarding interface types.

In the new logic, I’m using the getTypes method on GraphQL\Type\Definition\UnionType to retrieve types. However, I couldn’t find a similar method (or an equivalent way) for interfaces. Could you give me some guidance or a roadmap on how to handle this properly?

After changing the logic for handling morphTo relations, the following tests started failing:

  • testGeneratedInterfaceFieldSqlQuery
  • testGeneratedInterfaceFieldInlineFragmentsAndAlias
  • testGeneratedInterfaceFieldWithRelationSqlQuery
  • testGeneratedInterfaceFieldWithRelationAndCustomQueryOnInterfaceSqlQuery

Thanks in advance for your help! 🙏

@alissn alissn marked this pull request as draft October 25, 2025 07:37
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.

1 participant