Skip to content

Commit

Permalink
feat: ✨ add user resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 8, 2024
1 parent 59baaef commit e1697fa
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
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);
});
});

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);
});
});
});
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()
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);
}
}

0 comments on commit e1697fa

Please sign in to comment.