- Plug into existing federated graphs, through references.
- Create a federated relations/connections on types defined in other services.
- https://docs.nestjs.com/graphql/federation
- https://www.apollographql.com/docs/apollo-server/federation/introduction/
Base Type
The simplest way to integrate with a federated graph is through references. A reference is an object that looks like__typename lets the gateway know which type is being referenced with additional fields that can be used to uniquely identify the type.
Both of the examples below add a
resolveReference function see
https://www.apollographql.com/docs/apollo-server/federation/entities/#resolvingnestjs-query you must first create DTO that defines the base type.
Base Type
The base type in its own service must be decorated with federated directives specifying its key. todo-item/todo-item.dto.tsAuto Generated Resolver
When using theNestjsQueryGraphQLModule module add the referenceBy option that nestjs-query will use to automatically expose add a @ResolveReference decorator and method that the gateway can use.
todo-item/todo-item.module.ts
The
referenceBy.key should be the field you want to look up the DTO by.Manual Resolver
If you want to manually define your resolver pass in thereferenceBy option to the CRUDResolver.
todo-item.resolver.ts
The
referenceBy.key should be the field you want to look up the DTO by.App Module
This app module must also use theGraphQLFederationModule in order for the base type to be resolved by the gateway.
app.module.ts
Reference Base Type
In a separate service from the one defining the base type above, we can use Apollo Federation to extend that base type. To do this withnestjs-query you must create a type that extends the base type contained in some other graphql service.
For example
sub-task/todo-item-reference.dto.ts
Notice how the
@Directive decorator is used to add the @extends annotation along with the @keys.To read more about @extends annotation see https://www.apollographql.com/docs/apollo-server/federation/entities/#extending@Reference Decorator
To reference a type defined in another service you can use the@Reference decorator.
When using the @Reference decorator nestjs-query will NOT look up the relation through the QueryService, instead return a reference type like the one described above.
sub-task/sub-task.dto.ts
SubTask.todoItemId to the id field in the reference type.
Assuming you have the following SubTask
Resolver
Now that we have added the decorator thenestjs-query resolver will automatically add the reference to the graphql type when using NestjsQueryGraphQLModule or CRUDResolver
- NestjsQueryGraphQLModule
- CRUDResolver
- Example
- Example
sub-task/sub-task.module.ts
Federated Relations
Another common use case is to addrelations to a federated type from another service.
Lets continue with the SubTask example used above. We have add a todoItem reference to the SubTask but now lets add subTasks to the TodoItem.
RelationQueryService
The first step is to create aRelationQueryService. The RelationQueryService is a special type of QueryService that allows looking up relations without defining them in your entity.
todo-item.service.ts
SubTask service that will be used to look up subTask relations. The query method is used to filter relations when findRelation or queryRelations is called.
Add the Connection
Next we add thesubTasks connection to the TodoItemReferenceDTO.
The name of the relation should match the name of the relation defined by your
RelationQueryService.The same pattern applies when you have a single relation and use the
@Relation decorator.Federation Resolver
Next we set up our resolver that exposes the relations in the schema. As with other resolvers you can use theNestjsQueryGraphQLModule or define your own FederationResolver.
- NestjsQueryGraphQLModule
- FederationResolver
- Example
- Example
- Example
- Example
- Example
- Example
When using the
NestjsQueryGraphQLModule set the type of the resolver to federated, and specify the Service.