-
Notifications
You must be signed in to change notification settings - Fork 58
fix(query-graphql): use DataLoader in ReferenceResolver to prevent N+1 queries (#389) #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… problem - Add ReferenceLoader that batches reference queries using service.query - Update reference resolver to use DataLoader when ExecutionContext is available - Maintain backward compatibility by falling back to getById when context is missing - Batch multiple reference queries into single database query to eliminate n+1 problem 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
…ence resolver - Add context mock using the same pattern as existing DataLoader tests - Create test case to verify DataLoader is used when ExecutionContext is provided - Add test for proper error handling when entity is not found via DataLoader - Ensure backward compatibility test still passes for non-context scenarios - All tests pass, confirming DataLoader n+1 solution works correctly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Remove optional ExecutionContext parameter, make it required - Always use DataLoader for reference resolution, no fallback to getById - Simplify tests to always provide context, matching real-world usage - Reduce code complexity while maintaining full DataLoader functionality - All tests pass, confirming the simplified approach works correctly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements DataLoader in the ReferenceResolver to prevent N+1 query problems when resolving GraphQL federated references. Instead of making individual database calls for each reference, the resolver now batches requests together for more efficient data loading.
- Replaces direct service.getById() calls with DataLoader-based batching
- Adds new ReferenceLoader class to handle batch loading of entities by ID
- Updates the resolveReference method to accept execution context and DataLoader configuration
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/query-graphql/src/resolvers/reference.resolver.ts | Modified to use DataLoader instead of direct service calls for reference resolution |
| packages/query-graphql/src/loader/reference.loader.ts | New loader class implementing batch loading logic for entity references |
| packages/query-graphql/src/loader/index.ts | Added export for the new reference loader |
| packages/query-graphql/tests/resolvers/reference.resolver.spec.ts | Updated tests to work with DataLoader pattern and added test for entity not found scenario |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
View your CI Pipeline Execution ↗ for commit e4a05d1
☁️ Nx Cloud last updated this comment at |
Fix type assertion issue in reference.loader.ts where Filter<DTO> type didn't allow 'id' property. Use double type assertion through unknown to resolve compatibility issue. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
closes #389