-
-
Notifications
You must be signed in to change notification settings - Fork 675
/
Copy pathrecipe.resolver.ts
29 lines (25 loc) · 1.03 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
import { Arg, Query, Resolver } from "type-graphql";
import { CacheControl } from "./cache-control";
import { getTime } from "./helpers/getTime";
import { createRecipeSamples } from "./recipe.data";
import { Recipe } from "./recipe.type";
@Resolver(_of => Recipe)
export class RecipeResolver {
private readonly items: Recipe[] = createRecipeSamples();
@Query(_returns => Recipe, { nullable: true })
async recipe(@Arg("title") title: string): Promise<Recipe | undefined> {
console.log(`Called 'recipe' with title '${title}' on ${getTime()}`);
return this.items.find(recipe => recipe.title === title);
}
@Query(_returns => Recipe, { nullable: true })
// Cache query for 60s
@CacheControl({ maxAge: 60 })
async cachedRecipe(@Arg("title") title: string): Promise<Recipe | undefined> {
console.log(`Called 'cachedRecipe' with title '${title}' on ${getTime()}`);
return this.items.find(recipe => recipe.title === title);
}
@Query(_returns => [Recipe])
async recipes(): Promise<Recipe[]> {
return this.items;
}
}