-
-
Notifications
You must be signed in to change notification settings - Fork 675
/
recipe.resolver.ts
36 lines (31 loc) · 1.02 KB
/
recipe.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
import { Arg, FieldResolver, Mutation, Query, Resolver, Root } from "type-graphql";
import { Inject, Service } from "typedi";
import { RecipeInput } from "./recipe.input";
import { RecipeService } from "./recipe.service";
import { Recipe } from "./recipe.type";
@Service()
@Resolver(_of => Recipe)
export class RecipeResolver {
constructor(
// Inject service
@Inject()
private readonly recipeService: RecipeService,
) {}
@Query(_returns => Recipe, { nullable: true })
async recipe(@Arg("recipeId") recipeId: string) {
return this.recipeService.getOne(recipeId);
}
@Query(_returns => [Recipe])
async recipes(): Promise<Recipe[]> {
return this.recipeService.getAll();
}
@Mutation(_returns => Recipe)
async addRecipe(@Arg("recipe") recipe: RecipeInput): Promise<Recipe> {
return this.recipeService.add(recipe);
}
@FieldResolver()
async numberInCollection(@Root() recipe: Recipe): Promise<number> {
const index = await this.recipeService.findIndex(recipe);
return index + 1;
}
}