Version 0.8.1
Federation support
Basic support of federation already in place. Just add to your method with @ResolveReference()
one more decorator @GraphqlLoader()
Example
This examples is the reference to official example https://github.com/nestjs/nest/tree/master/sample/31-graphql-federation-code-first. Clone https://github.com/nestjs/nest/tree/master/sample/31-graphql-federation-code-first (download specific directory with https://download-directory.github.io/ or with chrome extention https://chrome.google.com/webstore/detail/gitzip-for-github/ffabmkklhbepgcgfonabamgnfafbdlkn)
- Annotate method resolveReference of
users-application/src/users/users.resolver.ts
// users-application/src/users/users.resolver.ts
@ResolveReference()
@GraphqlLoader()
async resolveReference(
@Loader() loader: LoaderData<User, number>,
) {
const ids = loader.ids;
const users = this.usersService.findByIds(ids);
return loader.helpers.mapManyToOneRelation(users, loader.ids, 'id')
}
- Add method findByIds to
users-application/src/users/users.service.ts
// users-application/src/users/users.service.ts
@Injectable()
export class UsersService {
private users: User[] = [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Richard Roe' },
];
findByIds(idsList: number[]): User[] {
return this.users.filter((user) => idsList.some(id => Number(id) === user.id));
}
}
-
Install dependencies of 3 projects : npm ci in gateway, posts-application, users-application.
-
Run all projects in order :
cd users-application && npm run start
cd posts-application && npm run start
cd gateway && npm run start
-
Go to localhost:3001/graphql and send graphql request to gateway
{
posts {
id
title
authorId
user {
id
name
}
}
}