-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathschema.ts
More file actions
109 lines (108 loc) · 3.48 KB
/
Copy pathschema.ts
File metadata and controls
109 lines (108 loc) · 3.48 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Executable schema generated by Grats (https://grats.capt.dev)
* Do not manually edit. Regenerate by running `npx grats`.
*/
import UserClass from "./models/User.js";
import queryAllUsersResolver from "./models/User.js";
import queryMeResolver from "./models/User.js";
import { GraphQLSchema, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLString, GraphQLInterfaceType } from "graphql";
import { person as queryPersonResolver } from "./Query.js";
export function getSchema(): GraphQLSchema {
const GroupType: GraphQLObjectType = new GraphQLObjectType({
name: "Group",
fields() {
return {
description: {
name: "description",
type: new GraphQLNonNull(GraphQLString)
},
members: {
name: "members",
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(UserType)))
},
name: {
name: "name",
type: new GraphQLNonNull(GraphQLString)
}
};
}
});
const IPersonType: GraphQLInterfaceType = new GraphQLInterfaceType({
name: "IPerson",
fields() {
return {
name: {
name: "name",
type: new GraphQLNonNull(GraphQLString)
}
};
},
resolveType
});
const UserType: GraphQLObjectType = new GraphQLObjectType({
name: "User",
fields() {
return {
groups: {
name: "groups",
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GroupType)))
},
name: {
name: "name",
type: new GraphQLNonNull(GraphQLString)
}
};
},
interfaces() {
return [IPersonType];
}
});
const QueryType: GraphQLObjectType = new GraphQLObjectType({
name: "Query",
fields() {
return {
allUsers: {
name: "allUsers",
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(UserType))),
resolve() {
return queryAllUsersResolver.allUsers();
}
},
me: {
name: "me",
type: new GraphQLNonNull(UserType),
resolve() {
return queryMeResolver.me();
}
},
person: {
name: "person",
type: new GraphQLNonNull(IPersonType),
resolve() {
return queryPersonResolver();
}
}
};
}
});
return new GraphQLSchema({
query: QueryType,
types: [IPersonType, GroupType, QueryType, UserType]
});
}
const typeNameMap = new Map();
typeNameMap.set(UserClass, "User");
function resolveType(obj: any): string {
if (typeof obj.__typename === "string") {
return obj.__typename;
}
let prototype = Object.getPrototypeOf(obj);
while (prototype) {
const name = typeNameMap.get(prototype.constructor);
if (name != null) {
return name;
}
prototype = Object.getPrototypeOf(prototype);
}
throw new Error("Cannot find type name.");
}