-
Notifications
You must be signed in to change notification settings - Fork 384
/
recipes.resolver.ts
79 lines (69 loc) 路 2.07 KB
/
recipes.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { NotFoundException, UseGuards } from '@nestjs/common';
import { PubSub } from 'apollo-server-express';
import {
Args,
Mutation,
Parent,
Query,
ResolveProperty,
Resolver,
Subscription,
} from '../../../lib';
import { AuthGuard } from '../common/guards/auth.guard';
import { NewRecipeInput } from './dto/new-recipe.input';
import { RecipesArgs } from './dto/recipes.args';
import { Ingredient } from './models/ingredient';
import { IRecipe, Recipe } from './models/recipe';
import { RecipesService } from './recipes.service';
import { SearchResultUnion } from './unions/search-result.union';
const pubSub = new PubSub();
@Resolver(of => Recipe)
export class RecipesResolver {
constructor(private readonly recipesService: RecipesService) {}
@UseGuards(AuthGuard)
@Query(returns => IRecipe)
async recipe(@Args('id') id: string): Promise<IRecipe> {
const recipe = await this.recipesService.findOneById(id);
if (!recipe) {
throw new NotFoundException(id);
}
return recipe;
}
@Query(returns => [SearchResultUnion])
async search(): Promise<Array<typeof SearchResultUnion>> {
return [
new Recipe({ title: 'recipe' }),
new Ingredient({
name: 'test',
}),
];
}
@Query(returns => [Recipe])
recipes(@Args() recipesArgs: RecipesArgs): Promise<Recipe[]> {
return this.recipesService.findAll(recipesArgs);
}
@Mutation(returns => Recipe)
async addRecipe(
@Args('newRecipeData') newRecipeData: NewRecipeInput,
): Promise<Recipe> {
const recipe = await this.recipesService.create(newRecipeData);
pubSub.publish('recipeAdded', { recipeAdded: recipe });
return recipe;
}
@ResolveProperty('ingredients', () => [Ingredient])
getIngredients(@Parent() root) {
return [new Ingredient({ name: 'cherry' })];
}
@ResolveProperty()
rating(): number {
return 10;
}
@Mutation(returns => Boolean)
async removeRecipe(@Args('id') id: string) {
return this.recipesService.remove(id);
}
@Subscription(returns => Recipe)
recipeAdded() {
return pubSub.asyncIterator('recipeAdded');
}
}