-
-
Notifications
You must be signed in to change notification settings - Fork 677
/
resolver.ts
40 lines (35 loc) · 1.03 KB
/
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 { Resolver, Query, Mutation, Arg } from "type-graphql";
import { LogMessage } from "./log-message.decorator";
import { Recipe } from "./recipe.type";
import { sampleRecipes } from "./helpers/recipe";
@Resolver()
export class ExampleResolver {
private recipesData: Recipe[] = sampleRecipes.slice();
@Query(returns => [Recipe])
async recipes(): Promise<Recipe[]> {
return this.recipesData;
}
@Mutation()
addRecipe(
@Arg("title") title: string,
@Arg("description", { nullable: true }) description?: string,
): Recipe {
const newRecipe = Object.assign(new Recipe(), {
title,
description,
ratings: [],
});
this.recipesData.push(newRecipe);
return newRecipe;
}
@LogMessage("Recipe deletion requested")
@Mutation()
deleteRecipe(@Arg("title") title: string): boolean {
const foundRecipeIndex = this.recipesData.findIndex(it => it.title === title);
if (!foundRecipeIndex) {
return false;
}
this.recipesData.splice(foundRecipeIndex, 1);
return true;
}
}