-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
265 lines (234 loc) · 9.23 KB
/
index.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLFloat,
GraphQLList,
GraphQLNonNull,
GraphQLInputObjectType,
GraphQLScalarType
} from 'graphql';
import { Kind } from 'graphql/language'
import * as helper from './src/helper';
import { mutationProcess } from './src/type/mutation';
import * as metadata from './src/metadata';
import "reflect-metadata";
var models: any = {};
var schema: any = {};
var mutations = { name: 'Mutation', fields: {} };
var haveMutation = false;
var mutationsArray = [];
var entryQuery = '';
var object = {};
class item {
type: string;
decorator: string;
target: any;
key: any;
description: string;
nullable: boolean;
required: string[];
returnType: string;
params: any;
constructor(decorator: string, target: any, key: any) {
this.decorator = decorator;
this.target = target;
this.key = key;
this.description = metadata.getDescription(this.target, this.key);
this.nullable = metadata.getNullable(this.target, key);
this.type = Reflect.getMetadata("design:type", this.target, this.key).name;
switch (this.type) {
case 'Function':
this.required = metadata.getRequired(this.target, this.key);
let temp = metadata.getReturn(this.target, this.key) || Reflect.getMetadata("design:returntype", this.target, this.key);
this.returnType = temp && temp.name ? temp.name : temp;
var paramType = Reflect.getMetadata("design:paramtypes", this.target, this.key)
if (paramType) this.params = helper.getArgs(target, key, paramType);
break;
case 'Array':
this.returnType = metadata.getReturn(this.target, this.key) || Reflect.getMetadata("design:returntype", this.target, this.key).name;
break;
default:
}
}
}
class property {
target: any;
type: string;
description: string;
name: string;
constructor(type: string, target: any) {
this.target = target;
this.type = type;
this.name = target.name;
this.description = metadata.getDescription(this.target);
}
}
function buildSchema(entryPoint: string) {
var obj = object[entryPoint];
if (!obj) throw new Error(`${entryPoint} is not valid`);
var property: property = <property>obj.property;
switch (property.type) {
case 'objectType': {
models[property.name] = new GraphQLObjectType({
name: property.name,
fields: {},
description: property.description
});
for (let key in obj.items) {
let item = <item>obj.items[key];
switch (item.type) {
case 'Function': {
let type = helper.getGraphQLType(item.returnType) || models[item.returnType];
if (!type) {
type = buildSchema(item.returnType);
}
if (item.decorator == 'list') type = new GraphQLList(type);
if (!item.nullable) type = new GraphQLNonNull(type);
var wrapFunction = function(_: any, data: any, context: any, fieldAST: any) {
data._context = context;
data._root = _;
data._fieldAST = fieldAST
var paramsTemp = item.params.map(function(param) {
return data[param.name] ? data[param.name] : undefined;
})
return item.target[item.key].apply(_, paramsTemp);
}
models[property.name]._typeConfig.fields[key] = {
type: type,
resolve: wrapFunction,
description: item.description,
args: helper.convertArgsToGraphQL(item.params, item.required, null)
}
break;
}
default:
let type = helper.getGraphQLType(item.type) || models[item.type];
if (!type && item.decorator == 'list') {
type = helper.getGraphQLType(item.returnType) || models[item.returnType];
}
if (!type) {
type = buildSchema(item.type);
}
if (item.decorator == 'list') type = new GraphQLList(type);
if (!item.nullable) type = new GraphQLNonNull(type);
models[property.name]._typeConfig.fields[key] = {
type: type,
description: item.description,
}
break;
}
}
break;
}
case 'inputType': {
models[property.name] = new GraphQLInputObjectType({
name: property.name,
description: property.description,
fields: {}
})
for (let key in obj.items) {
let item = <item>obj.items[key];
switch (item.decorator) {
case 'list':
let type = helper.getGraphQLType(item.returnType) || models[item.returnType];
if (!type) {
type = buildSchema(item.returnType);
}
type = new GraphQLList(type);
if (!item.nullable) type = new GraphQLNonNull(type);
models[property.name]._typeConfig.fields[key] = {
type: type,
description: item.description
}
break;
default:
let type2 = helper.getGraphQLType(item.type) || models[item.type];
if (!item.nullable) type2 = new GraphQLNonNull(type2);
if (!type2) {
type2 = buildSchema(item.type);
}
models[property.name]._typeConfig.fields[key] = {
type: type2,
description: item.description,
}
break;
}
}
break;
}
case 'scalarType': {
var items = obj.items;
let temp: any = { name: property.name }
for (let key in obj.items) {
temp[key] = obj.items[key].target[key];
}
models[property.name] = new GraphQLScalarType(temp)
break;
}
}
return models[property.name];
}
export function objectType(target: any) {
if (!object[target.name]) object[target.name] = { items: {}, property: {} };
object[target.name].property = new property('objectType', target);
}
export function inputType(target: any) {
if (!object[target.name]) object[target.name] = { items: {}, property: {} };
object[target.name].property = new property('inputType', target);
buildSchema(target.name);
}
export function field(target: any, key: string) {
if (!object[target.constructor.name]) object[target.constructor.name] = { items: {}, property: {} };
var temp = new item('field', target, key)
object[target.constructor.name].items[key] = temp;
}
export function list(target: any, key: string) {
if (!object[target.constructor.name]) object[target.constructor.name] = { items: {}, property: {} };
var temp = new item('list', target, key)
object[target.constructor.name].items[key] = temp;
}
export function scalarType(target: any) {
if (!object[target.name]) object[target.name] = { items: {}, property: {} };
object[target.name].property = new property('scalarType', target);
}
export module graphqlTs {
export function getSchema() {
return schema;
}
export function init<T>(query: T) {
var queryObject = <any>query;
if (queryObject.constructor.name) entryQuery = queryObject.constructor.name;
buildSchema(entryQuery);
mutationsArray.forEach(function(item) {
mutationProcess(item.target, item.key, models, mutations, models)
})
schema.query = models[entryQuery];
if (haveMutation) schema.mutation = new GraphQLObjectType(mutations);
schema = new GraphQLSchema(schema);
}
export function query(query: string): Promise<any> {
return graphql(getSchema(), query);
}
}
export function required(name: [string]) {
return metadata.required(name);
}
export function description(text: string) {
return metadata.description(text);
}
export function nullable(nullable: boolean) {
return metadata.nullable(nullable);
}
export function returnType<T>(objectType: T) {
var temp = <any>objectType;
return metadata.returnType(temp.name);
}
export function mutation(target: any, key: string) {
haveMutation = true;
mutationsArray.push({
target: target,
key: key
})
}