Apollo Federation
Relevant source files
The following files were used as context for generating this wiki page:
Apollo Federation allows the nestjs-query library to participate in a distributed graph architecture by composing multiple subgraphs into a single gateway. This module enables subgraphs to resolve entity references, handle cross-service relations, and provide a unified schema using the Apollo Federation Specification (v1 or v2).
Sources: examples/federation-v2-e2e/README.md, packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:25-30
Subgraph Configuration
To use Apollo Federation, you must configure the NestJS GraphQLModule with the ApolloFederationDriver. This configuration registers the service as a subgraph and enables federation-specific features like the _entities query and the _service SDL export.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:47-51
Resolver Configuration
Entities become federated when you configure the referenceBy property in the NestjsQueryGraphQLModule feature registration. This property identifies the key used by the gateway to resolve the entity across different subgraphs.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:58-60, packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts:60-65
| Option | Type | Description |
|---|---|---|
referenceBy | object | Enables ReferenceResolver with specified keys (e.g., { key: 'id' }). |
driver | ApolloFederationDriver | Required driver for federation support in NestJS GraphQL. |
version | number | Federation version (1 or 2). |
Sources: packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:49-65
Entity Resolution Logic
The gateway resolves entities by sending an _entities query to subgraphs. This query contains "representations" consisting of the __typename and the primary keys of the requested entities.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:89-98
Reference Resolution Flow
The following diagram illustrates how the Apollo Gateway interacts with a nestjs-query subgraph to resolve an entity:
The system uses the @Parent() decorator to properly extract representation data from GraphQL arguments. This prevents the "representation parameter undefined" bug.
Sources: examples/federation-v2-e2e/README.md:104-124, packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:79-88
Key Resolution Features
- Batching: The
ReferenceLoaderusesDataLoaderto batch multiple reference requests into a single database query, preventing N+1 performance issues. - Type Safety: The system handles ID type mismatches by converting IDs (such as numeric strings from Federation) to the correct entity type for Map lookups.
- Mixed Queries: A single
_entitiesquery can resolve multiple different entity types simultaneously within the same subgraph.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:139-145, packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:175-185, examples/federation-v2-e2e/README.md:126-128
Cross-Service Relations
Federation enables relations between entities residing in different services. For example, a TodoItem in one service can reference a User in another service using their shared ID.
nestjs-query supports various ID formats for these resolutions, including standard integers and UUID strings.
Sources: examples/federation-v2-e2e/e2e-test/e2e/federation.spec.ts:48-80, examples/federation-v2-e2e/README.md:38-42
Schema and Validation
Subgraphs expose their schema through the _service query. This allows the gateway to inspect the Federated SDL (Schema Definition Language), including directives like @key.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:275-285
Error Handling
The reference resolution process validates incoming requests and returns specific GraphQL errors in the following cases:
- Missing Key: If a representation lacks the required key defined in
referenceBy. - Entity Not Found: If the requested entity does not exist in the database, resulting in a "Unable to find [Entity]" message.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:245-260
Performance Optimization
The integration prevents N+1 queries during reference resolution by utilizing batching. This is critical when resolving relations for large lists of entities.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts:130-140, packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts:230-245
SQL Execution Comparison
| Scenario | Standard Resolution | Federated Batching |
|---|---|---|
| Single Reference | 1 Query | 1 Query |
| 50 Todo Items (List Refs) | 51 Queries (N+1) | < 10 Queries |
Sources: packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts:135-145, packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts:240-250
Apollo Federation integration in nestjs-query ensures that distributed GraphQL architectures remain performant and easy to configure by leveraging automated reference resolution and efficient data loading patterns.