@graphql-yoga/nestjs
Version:
GraphQL Yoga driver for NestJS GraphQL.
64 lines (54 loc) • 1.3 kB
text/typescript
import { ParseIntPipe, UseGuards } from '@nestjs/common'
import {
Args,
Mutation,
Query,
ResolveField,
Resolver,
Subscription,
} from '@nestjs/graphql'
import { createPubSub } from '../../../utils/pubsub'
import { CatsGuard } from './cats.guard'
import { CatsService } from './cats.service'
import { Cat } from './interfaces/cat.interface'
const catCreated = createPubSub<{ catCreated: Cat }>()
('Cat')
export class CatsResolvers {
constructor(private readonly catsService: CatsService) {}
()
(CatsGuard)
getCats() {
return this.catsService.findAll()
}
('color')
getColor() {
return 'black'
}
()
weight() {
return 5
}
('cat')
findOneById(
('id', ParseIntPipe)
id: number,
): Cat | undefined {
return this.catsService.findOneById(id)
}
('createCat')
create(() args: Cat): Cat {
const createdCat = this.catsService.create(args)
catCreated.pub({ catCreated: createdCat })
return createdCat
}
('catCreated')
catCreated() {
return catCreated.sub()
}
('greetings')
async *greetings() {
for (const hi of ['Hi', 'Bonjour', 'Hola', 'Ciao', 'Zdravo']) {
yield { greetings: hi }
}
}
}