Skip to content

Commit 8622951

Browse files
committed
Update examples
1 parent 231331a commit 8622951

File tree

7 files changed

+68
-3
lines changed

7 files changed

+68
-3
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { createParamDecorator } from "type-graphql";
1+
import { createParameterDecorator } from "type-graphql";
22
import { type Context } from "../context.type";
33

44
export function CurrentUser() {
5-
return createParamDecorator<Context>(({ context }) => context.currentUser);
5+
return createParameterDecorator<Context>(({ context }) => context.currentUser);
66
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Int, createParameterDecorator } from "type-graphql";
2+
3+
const MAX_ID_VALUE = 3; // Number.MAX_SAFE_INTEGER
4+
5+
export function RandomId(argName = "id") {
6+
return createParameterDecorator(
7+
({ args }) => args[argName] ?? Math.round(Math.random() * MAX_ID_VALUE),
8+
{
9+
arg: {
10+
name: argName,
11+
typeFunc: () => Int,
12+
options: {
13+
nullable: true,
14+
description: "Accepts provided id or generates a random one.",
15+
validateFn: (value: number): void => {
16+
if (value < 0 || value > MAX_ID_VALUE) {
17+
throw new Error(`Invalid value for ${argName}`);
18+
}
19+
},
20+
},
21+
},
22+
},
23+
);
24+
}

examples/middlewares-custom-decorators/examples.graphql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,21 @@ query InterceptorsQuery {
2020
averageRating
2121
}
2222
}
23+
24+
query RandomIdQuery {
25+
recipe {
26+
id
27+
title
28+
averageRating
29+
description
30+
}
31+
}
32+
33+
query SelectedIdQuery {
34+
recipe(id: 2) {
35+
id
36+
title
37+
averageRating
38+
description
39+
}
40+
}

examples/middlewares-custom-decorators/recipe/recipe.data.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { Recipe } from "./recipe.type";
22

3+
let lastRecipeId = 0;
4+
35
function createRecipe(recipeData: Partial<Recipe>): Recipe {
4-
return Object.assign(new Recipe(), recipeData);
6+
return Object.assign(new Recipe(), {
7+
// eslint-disable-next-line no-plusplus
8+
id: lastRecipeId++,
9+
...recipeData,
10+
});
511
}
612

713
export const recipes = [

examples/middlewares-custom-decorators/recipe/recipe.resolver.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { RecipesArgs } from "./recipe.args";
44
import { recipes as recipesData } from "./recipe.data";
55
import { Recipe } from "./recipe.type";
66
import { CurrentUser, ValidateArgs } from "../decorators";
7+
import { RandomId } from "../decorators/random-id";
78
import { ResolveTimeMiddleware } from "../middlewares";
89
import { User } from "../user.type";
910

@@ -13,6 +14,12 @@ import { User } from "../user.type";
1314
export class RecipeResolver {
1415
private readonly items: Recipe[] = recipesData;
1516

17+
@Query(_returns => Recipe, { nullable: true })
18+
async recipe(@RandomId("id") id: number) {
19+
console.log(`Queried for recipe with id: ${id}`);
20+
return this.items.find(item => item.id === id);
21+
}
22+
1623
@Query(_returns => [Recipe])
1724
@ValidateArgs(RecipesArgs)
1825
async recipes(

examples/middlewares-custom-decorators/recipe/recipe.type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { LogAccessMiddleware, NumberInterceptor } from "../middlewares";
33

44
@ObjectType()
55
export class Recipe {
6+
@Field(_type => Int)
7+
id!: number;
8+
69
@Field()
710
title!: string;
811

examples/middlewares-custom-decorators/schema.graphql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
# -----------------------------------------------
55

66
type Query {
7+
recipe(
8+
"""
9+
Accepts provided id or generates a random one.
10+
"""
11+
id: Int
12+
): Recipe
713
recipes(skip: Int! = 0, take: Int! = 10): [Recipe!]!
814
}
915

1016
type Recipe {
1117
averageRating: Float
1218
description: String
19+
id: Int!
1320
ratings: [Int!]!
1421
title: String!
1522
}

0 commit comments

Comments
 (0)