-
-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathresolver.ts
More file actions
42 lines (38 loc) · 1.22 KB
/
Copy pathresolver.ts
File metadata and controls
42 lines (38 loc) · 1.22 KB
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
41
42
import { Arg, Mutation, Query, Resolver } from "type-graphql";
import { Employee, EmployeeInput } from "./employee";
import { calculateAge, getId } from "./helpers";
import { IPerson } from "./person";
import { Student, StudentInput } from "./student";
@Resolver()
export class MultiResolver {
private readonly personsRegistry: IPerson[] = [];
@Query(_returns => [IPerson])
persons(): IPerson[] {
// This one returns interfaces,
// GraphQL has to be able to resolve type of the item
return this.personsRegistry;
}
@Mutation()
addStudent(@Arg("input") input: StudentInput): Student {
// Be sure to create real instances of classes
const student = Object.assign(new Student(), {
id: getId(),
name: input.name,
universityName: input.universityName,
age: calculateAge(input.dateOfBirth),
});
this.personsRegistry.push(student);
return student;
}
@Mutation()
addEmployee(@Arg("input") input: EmployeeInput): Employee {
const employee = Object.assign(new Employee(), {
id: getId(),
name: input.name,
companyName: input.companyName,
age: calculateAge(input.dateOfBirth),
});
this.personsRegistry.push(employee);
return employee;
}
}