Skip to content

Conversation

@Diluka
Copy link
Contributor

@Diluka Diluka commented Jul 21, 2025

closes #389

Diluka and others added 3 commits July 21, 2025 18:07
… 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>
Copilot AI review requested due to automatic review settings July 21, 2025 11:10
Copy link
Contributor

Copilot AI left a 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>
@nx-cloud
Copy link

nx-cloud bot commented Jul 22, 2025

View your CI Pipeline Execution ↗ for commit e4a05d1

Command Status Duration Result
nx run-many --target=e2e --all ✅ Succeeded 2m 32s View ↗
nx run-many --target=test --all ✅ Succeeded 43s View ↗
nx run-many --target=lint --all ✅ Succeeded 16s View ↗
nx run-many --target=build --all ✅ Succeeded 1s View ↗
nx run workspace:version ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2025-07-29 14:28:23 UTC

Diluka and others added 2 commits July 22, 2025 16:49
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>
@TriPSs TriPSs merged commit 9a5c835 into TriPSs:master Jul 29, 2025
13 checks passed
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.

referenceBy: { key: 'id' } doesn't use dataloader to optimize n+1 query

2 participants