/
cats.resolver.ts
40 lines (34 loc) 路 1.09 KB
/
cats.resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { ParseIntPipe, UseGuards } from '@nestjs/common';
import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';
import { Cat } from '../graphql.schema';
import { CatsGuard } from './cats.guard';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';
const pubSub = new PubSub();
@Resolver('Cat')
export class CatsResolver {
constructor(private readonly catsService: CatsService) {}
@Query('cats')
@UseGuards(CatsGuard)
async getCats() {
return this.catsService.findAll();
}
@Query('cat')
async findOneById(
@Args('id', ParseIntPipe)
id: number,
): Promise<Cat> {
return this.catsService.findOneById(id);
}
@Mutation('createCat')
async create(@Args('createCatInput') args: CreateCatDto): Promise<Cat> {
const createdCat = await this.catsService.create(args);
pubSub.publish('catCreated', { catCreated: createdCat });
return createdCat;
}
@Subscription('catCreated')
catCreated() {
return pubSub.asyncIterator('catCreated');
}
}