-
-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathresolver.ts
More file actions
35 lines (30 loc) · 1022 Bytes
/
Copy pathresolver.ts
File metadata and controls
35 lines (30 loc) · 1022 Bytes
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
import { Arg, Mutation, Query, Resolver } from "type-graphql";
import { AmendUserInput, CreateUserInput } from "./inputs";
import { User } from "./types";
@Resolver()
export class UserResolver {
private autoIncrementId = 0;
private readonly usersData: User[] = [];
@Query(_returns => [User])
async users(): Promise<User[]> {
return this.usersData;
}
@Mutation(_returns => User)
async createUser(@Arg("input") userData: CreateUserInput): Promise<User> {
// Generate the ID and store the password
this.autoIncrementId += 1;
const user: User = { ...userData, id: this.autoIncrementId };
this.usersData.push(user);
return user;
}
@Mutation(_returns => User)
async amendUser(@Arg("input") { id, ...userData }: AmendUserInput): Promise<User> {
// Receive the ID but can't change the password
const user = this.usersData.find(it => it.id === id);
if (!user) {
throw new Error(`Invalid ID: ${id}`);
}
Object.assign(user, userData);
return user;
}
}