Relations and DataLoader
Relevant source files
The following files were used as context for generating this wiki page:
- packages/query-graphql/src/resolvers/relations/relations.resolver.ts
- packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts
- packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts
- packages/query-graphql/tests/resolvers/relations/relations.resolver.spec.ts
- packages/query-mongoose/tests/services/mongoose-query.service.spec.ts
- examples/all-paging/e2e/sub-task.resolver.spec.ts
Relations and DataLoader provide the mechanism for fetching associated data and optimizing those fetches within nestjs-query. This system allows DTOs to define connections to other entities while preventing performance issues like the N+1 query problem through batching.
The system integrates directly with GraphQL resolvers to resolve single relations, connections, and references. It uses a batching strategy to consolidate multiple database requests into a single optimized query.
Architecture and Data Flow
The relation system relies on mixins to add relational capabilities to base resolvers. These mixins handle reading, updating, and removing relations between DTOs. When a GraphQL query requests related data, the resolver uses a DataLoader to collect IDs and execute a single batch query.
Request Resolution Flow
The flow shows how GraphQL requests for relations are intercepted by mixins and routed through a DataLoader to the persistence layer.
Sources: packages/query-graphql/src/resolvers/relations/relations.resolver.ts, packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts:100-115
Core Components
The relation system is composed of several decorators and mixins that define how entities interact.
| Component | Description |
|---|---|
Relatable | A decorator that applies relation mixins to a resolver class. |
Relation | Defines a one-to-one or many-to-one relationship on a DTO. |
CursorConnection | Defines a one-to-many relationship using cursor-based pagination. |
Reference | Defines a relationship to an external entity, often used in Federation. |
ReadRelationsMixin | Injects logic for querying and fetching related entities. |
UpdateRelationsMixin | Injects logic for setting or adding relations between entities. |
Sources: packages/query-graphql/tests/resolvers/relations/relations.resolver.spec.ts:25-45, packages/query-graphql/src/resolvers/relations/relations.resolver.ts
DataLoader and N+1 Prevention
The DataLoader prevents N+1 queries by batching requests for related entities. Instead of executing a separate SQL query for every parent record to find its children, the DataLoader collects all parent IDs and executes one query using an IN operator.
Batching Sequence
This diagram illustrates how individual load calls are aggregated into a single service call and database query.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts:85-110, packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:115-130
Federation Reference Resolution
In a federated architecture, the ReferencesRelationMixin handles the resolution of entities across different microservices. It uses the @ResolveReference() decorator to allow the Apollo Gateway to resolve entity fragments. The system uses a referenceBy configuration to identify which keys should be used for lookups.
- Key Mapping: You specify the DTO class and the keys used for the lookup (e.g.,
{ id: 'relationId' }). - Batch Resolution: The
ReferenceLoaderbatches multiple entity representations into a singlefindByIdscall.
Sources: packages/query-graphql/tests/resolvers/relations/relations.resolver.spec.ts:50-65, packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:55-80
Persistence Layer Integration
The QueryService within the persistence packages (Mongoose, TypeORM) implements the methods required for the DataLoader to function.
- findRelation: Finds a single related entity for one or more parent entities.
- queryRelations: Queries a collection of related entities (many-to-many or one-to-many) for one or more parent entities.
- aggregateRelations: Performs aggregation (count, sum, avg) on related entities.
Service Method Capabilities
| Method | Batch Support | Filtering | Paging |
|---|---|---|---|
findRelation | Yes (via Map) | Yes | No |
queryRelations | Yes (via Map) | Yes | Yes |
countRelations | Yes (via Map) | Yes | No |
Sources: packages/query-mongoose/tests/services/mongoose-query.service.spec.ts:565-650, packages/query-mongoose/tests/services/mongoose-query.service.spec.ts:775-800
Summary
Relations and DataLoader ensure that complex object graphs are resolved efficiently. By utilizing mixins to inject relational logic and DataLoaders to batch database interactions, nestjs-query maintains high performance even when querying deeply nested entities. This system also extends to distributed environments through its support for GraphQL Federation reference resolution.