Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Initial graphql federation: users application #14

Merged
merged 15 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"nestjs"
]
}
3 changes: 2 additions & 1 deletion apps/users-application/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from '../users/users.module';

@Module({
imports: [],
imports: [UsersModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
11 changes: 11 additions & 0 deletions apps/users-application/src/users/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType()
@Directive('@key(fields: "id")')
export class User {
@Field(() => ID)
id: number;

@Field()
name: string;
}
30 changes: 30 additions & 0 deletions apps/users-application/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
ApolloFederationDriver,
ApolloFederationDriverConfig,
} from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { UsersResolver } from './users.resolver';
import { UsersService } from './users.service';
import { ApolloServerPluginInlineTrace } from '@apollo/server/plugin/inlineTrace';

@Module({
providers: [UsersResolver, UsersService],
imports: [
GraphQLModule.forRoot<ApolloFederationDriverConfig>({
driver: ApolloFederationDriver,
autoSchemaFile: {
/**
* MEMO:
* Because of this problem, so mush need specify the version
* https://github.com/nestjs/graphql/issues/2646#issuecomment-1567381944
*/
federation: 2,
},
playground: process.env['NODE_ENV'] !== 'production',
sortSchema: true,
plugins: [ApolloServerPluginInlineTrace()],
}),
],
})
export class UsersModule {}
64 changes: 64 additions & 0 deletions apps/users-application/src/users/users.resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersResolver } from './users.resolver';
import { UsersService } from './users.service';
import { User } from './models/user.model';

describe('UsersResolver', () => {
let resolver: UsersResolver;
let service: UsersService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersResolver,
{
provide: UsersService,
useValue: {
findById: jest.fn(),
},
},
],
}).compile();

resolver = module.get<UsersResolver>(UsersResolver);
service = module.get<UsersService>(UsersService);
});

it('should be defined', () => {
expect(resolver).toBeDefined();
});

describe('getUser', () => {
it('should return a user by id', () => {
const user: User = { id: 1, name: 'John Doe' };
jest.spyOn(service, 'findById').mockReturnValue(user);

expect(resolver.getUser(1)).toEqual(user);
expect(service.findById).toHaveBeenCalledWith(1);
});

it('should return undefined if user not found', () => {
jest.spyOn(service, 'findById').mockReturnValue(undefined);

expect(resolver.getUser(1)).toBeUndefined();
expect(service.findById).toHaveBeenCalledWith(1);
});
});
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved

describe('resolveReference', () => {
it('should return a user by reference id', () => {
const user: User = { id: 1, name: 'John Doe' };
jest.spyOn(service, 'findById').mockReturnValue(user);

expect(resolver.resolveReference({ __typename: 'User', id: 1 })).toEqual(user);
expect(service.findById).toHaveBeenCalledWith(1);
});

it('should return undefined if user not found by reference id', () => {
jest.spyOn(service, 'findById').mockReturnValue(undefined);

expect(resolver.resolveReference({ __typename: 'User', id: 1 })).toBeUndefined();
expect(service.findById).toHaveBeenCalledWith(1);
});
});
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
});
22 changes: 22 additions & 0 deletions apps/users-application/src/users/users.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql';

import { User } from './models/user.model';
import { UsersService } from './users.service';

@Resolver(() => User)
export class UsersResolver {
constructor(private usersService: UsersService) {}

@Query(() => User, { nullable: true })
getUser(@Args({ name: 'id', type: () => ID }) id: number): User | undefined {
return this.usersService.findById(id);
}

@ResolveReference()
resolveReference(reference: {
__typename: string;
id: number;
}): User | undefined {
return this.usersService.findById(reference.id);
}
}
29 changes: 29 additions & 0 deletions apps/users-application/src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from './users.service';
import { User } from './models/user.model';

describe('UsersService', () => {
let service: UsersService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService],
}).compile();

service = module.get<UsersService>(UsersService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should return a user by id', () => {
const user: User = service.findById(1);
expect(user).toEqual({ id: 1, name: 'John Doe' });
});

it('should return undefined if user is not found', () => {
const user: User = service.findById(3);
expect(user).toBeUndefined();
});
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
});
14 changes: 14 additions & 0 deletions apps/users-application/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';
import { User } from './models/user.model';

@Injectable()
export class UsersService {
private users: User[] = [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Richard Roe' },
];

findById(id: number): User | undefined {
return this.users.find((user) => user.id === Number(id));
}
}
50 changes: 29 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@
},
"private": true,
"dependencies": {
"@nestjs/common": "^10.0.2",
"@nestjs/core": "^10.0.2",
"@nestjs/platform-express": "^10.0.2",
"axios": "^1.6.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0"
"@apollo/gateway": "^2.5.5",
"@apollo/server": "^4.11.2",
"@apollo/subgraph": "^2.5.5",
zhumeisongsong marked this conversation as resolved.
Show resolved Hide resolved
"@nestjs/apollo": "^12.2.1",
"@nestjs/common": "^10.4.7",
"@nestjs/core": "^10.4.7",
"@nestjs/graphql": "^12.2.1",
"@nestjs/platform-express": "^10.4.7",
"axios": "^1.7.7",
"graphql": "^16.9.0",
"graphql-tools": "^9.0.2",
"inlineTrace": "link:@apollo/server/plugin/inlineTrace",
"reflect-metadata": "^0.1.14",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@eslint/js": "^9.8.0",
"@nestjs/schematics": "^10.0.1",
"@nestjs/testing": "^10.0.2",
"@eslint/js": "^9.14.0",
"@nestjs/schematics": "^10.2.3",
"@nestjs/testing": "^10.4.7",
"@nx/eslint": "20.0.6",
"@nx/eslint-plugin": "20.0.6",
"@nx/jest": "20.0.6",
Expand All @@ -32,24 +40,24 @@
"@nx/web": "20.0.6",
"@nx/webpack": "20.0.6",
"@nx/workspace": "20.0.6",
"@swc-node/register": "~1.9.1",
"@swc/core": "~1.5.7",
"@swc/helpers": "~0.5.11",
"@types/jest": "^29.5.12",
"@swc-node/register": "~1.9.2",
"@swc/core": "~1.5.29",
"@swc/helpers": "~0.5.13",
"@types/jest": "^29.5.14",
"@types/node": "~22.9.0",
"@zhumeisong/git-cz-config": "^1.1.1",
"eslint": "^9.8.0",
"eslint-config-prettier": "^9.0.0",
"@zhumeisong/git-cz-config": "^1.2.0",
"eslint": "^9.14.0",
"eslint-config-prettier": "^9.1.0",
"git-cz": "^4.9.0",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
"nx": "20.0.6",
"prettier": "^3.0.0",
"ts-jest": "^29.1.0",
"prettier": "^2.8.8",
"ts-jest": "^29.2.5",
"ts-node": "10.9.1",
"tslib": "^2.3.0",
"typescript": "~5.5.2",
"typescript-eslint": "^8.0.0",
"tslib": "^2.8.1",
"typescript": "~5.5.4",
"typescript-eslint": "^8.13.0",
"webpack-cli": "^5.1.4"
},
"packageManager": "[email protected]",
Expand Down
Loading