/
inputTypeComposer.js
334 lines (289 loc) · 9.93 KB
/
inputTypeComposer.js
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* @flow */
/* eslint-disable no-use-before-define */
import { GraphQLInputObjectType, GraphQLNonNull, getNamedType } from './graphql';
import { resolveMaybeThunk } from './utils/misc';
// import { deprecate } from './utils/debug';
import { isObject, isString } from './utils/is';
import { resolveInputConfigsAsThunk, keepConfigsAsThunk } from './utils/configAsThunk';
import TypeMapper from './typeMapper';
import { typeByPath } from './typeByPath';
import type { Thunk, ObjMap } from './utils/definitions';
import type EnumTypeComposer from './enumTypeComposer';
import type { TypeAsString } from './typeMapper';
import type {
// GraphQLInputFieldConfig,
GraphQLInputFieldConfigMap,
GraphQLInputType,
InputValueDefinitionNode,
} from './graphql';
export type ComposeInputFieldConfigMap = ObjMap<ComposeInputFieldConfig>;
export type ComposeInputFieldConfig =
| ComposeInputFieldConfigAsObject
| ComposeInputType
| (() => ComposeInputFieldConfigAsObject | ComposeInputType);
export type ComposeInputFieldConfigAsObject = {
type: Thunk<ComposeInputType> | GraphQLInputType,
defaultValue?: mixed,
description?: ?string,
astNode?: ?InputValueDefinitionNode,
[key: string]: any,
} & { $call?: void };
export type ComposeInputType =
| InputTypeComposer
| EnumTypeComposer
| GraphQLInputType
| TypeAsString
| Array<ComposeInputType>;
export type ComposeInputObjectTypeConfig = {
name: string,
fields: Thunk<ComposeInputFieldConfigMap>,
description?: ?string,
};
export default class InputTypeComposer {
gqType: GraphQLInputObjectType;
static create(
opts: TypeAsString | ComposeInputObjectTypeConfig | GraphQLInputObjectType
): InputTypeComposer {
let ITC;
if (isString(opts)) {
const typeName: string = opts;
const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
if (NAME_RX.test(typeName)) {
ITC = new InputTypeComposer(
new GraphQLInputObjectType({
name: typeName,
fields: () => ({}),
})
);
} else {
const type = TypeMapper.createType(typeName);
if (!(type instanceof GraphQLInputObjectType)) {
throw new Error(
'You should provide correct GraphQLInputObjectType type definition.' +
'Eg. `input MyInputType { name: String! }`'
);
}
ITC = new InputTypeComposer(type);
}
} else if (opts instanceof GraphQLInputObjectType) {
ITC = new InputTypeComposer(opts);
} else if (isObject(opts)) {
const type = new GraphQLInputObjectType({
name: opts.name,
description: opts.description,
fields: () => ({}),
});
ITC = new InputTypeComposer(type);
if (isObject(opts.fields)) {
ITC.addFields(opts.fields);
}
} else {
throw new Error(
'You should provide InputObjectConfig or string with type name to InputTypeComposer.create(opts)'
);
}
return ITC;
}
constructor(gqType: GraphQLInputObjectType) {
if (!(gqType instanceof GraphQLInputObjectType)) {
throw new Error('InputTypeComposer accept only GraphQLInputObjectType in constructor');
}
this.gqType = gqType;
}
/**
* Get fields from a GraphQL type
* WARNING: this method read an internal GraphQL instance variable.
* TODO: should return GraphQLInputFieldConfigMap
* BUT if setFields(fields: ComposeInputFieldConfigMap | GraphQLInputFieldConfigMap)
* then flow producess error with such common case ITC.setFields(ITC.getFields())
* with following message "Could not decide which case to select"
* More info about solution
* https://twitter.com/nodkz/status/925010361815851008
* https://github.com/facebook/flow/issues/2892
*/
getFields(): ObjMap<any> {
const fields: Thunk<GraphQLInputFieldConfigMap> = this.gqType._typeConfig.fields;
const fieldMap: mixed = keepConfigsAsThunk(resolveMaybeThunk(fields));
if (isObject(fieldMap)) {
return { ...fieldMap };
}
return {};
}
getFieldNames(): string[] {
return Object.keys(this.getFields());
}
hasField(fieldName: string): boolean {
const fields = this.getFields();
return !!fields[fieldName];
}
/**
* Completely replace all fields in GraphQL type
* WARNING: this method rewrite an internal GraphQL instance variable.
*/
setFields(fields: ComposeInputFieldConfigMap): InputTypeComposer {
const prepearedFields = TypeMapper.convertInputFieldConfigMap(fields, this.getTypeName());
this.gqType._typeConfig.fields = () =>
resolveInputConfigsAsThunk(prepearedFields, this.getTypeName());
delete this.gqType._fields; // if schema was builded, delete defineFieldMap
return this;
}
setField(fieldName: string, fieldConfig: ComposeInputFieldConfig): InputTypeComposer {
this.addFields({ [fieldName]: fieldConfig });
return this;
}
/**
* Add new fields or replace existed in a GraphQL type
*/
addFields(newFields: ComposeInputFieldConfigMap): InputTypeComposer {
this.setFields({ ...this.getFields(), ...newFields });
return this;
}
/**
* Get fieldConfig by name
* TODO should be GraphQLInputFieldConfig
* see getFields() method for details
*/
getField(fieldName: string): any {
const fields = this.getFields();
if (!fields[fieldName]) {
throw new Error(
`Cannot get field '${fieldName}' from input type '${this.getTypeName()}'. Field does not exist.`
);
}
return fields[fieldName];
}
removeField(fieldNameOrArray: string | Array<string>): InputTypeComposer {
const fieldNames = Array.isArray(fieldNameOrArray) ? fieldNameOrArray : [fieldNameOrArray];
const fields = this.getFields();
fieldNames.forEach(fieldName => delete fields[fieldName]);
this.setFields(fields);
return this;
}
removeOtherFields(fieldNameOrArray: string | Array<string>): InputTypeComposer {
const keepFieldNames = Array.isArray(fieldNameOrArray) ? fieldNameOrArray : [fieldNameOrArray];
const fields = this.getFields();
Object.keys(fields).forEach(fieldName => {
if (keepFieldNames.indexOf(fieldName) === -1) {
delete fields[fieldName];
}
});
this.setFields(fields);
return this;
}
extendField(
fieldName: string,
parialFieldConfig: $Shape<ComposeInputFieldConfigAsObject>
): InputTypeComposer {
let prevFieldConfig;
try {
prevFieldConfig = this.getField(fieldName);
} catch (e) {
throw new Error(
`Cannot extend field '${fieldName}' from input type '${this.getTypeName()}'. Field does not exist.`
);
}
const fieldConfig: ComposeInputFieldConfig = {
...(prevFieldConfig: any),
...(parialFieldConfig: any),
};
this.setField(fieldName, fieldConfig);
return this;
}
reorderFields(names: string[]): InputTypeComposer {
const orderedFields = {};
const fields = this.getFields();
names.forEach(name => {
if (fields[name]) {
orderedFields[name] = fields[name];
delete fields[name];
}
});
this.setFields({ ...orderedFields, ...fields });
return this;
}
isRequired(fieldName: string): boolean {
return this.getFieldType(fieldName) instanceof GraphQLNonNull;
}
getFieldType(fieldName: string): GraphQLInputType {
const field = this.getField(fieldName);
if (!field) {
throw new Error(`Type ${this.getTypeName()} does not have field with name '${fieldName}'`);
}
return field.type;
}
getFieldTC(fieldName: string): InputTypeComposer {
const fieldType = getNamedType(this.getFieldType(fieldName));
if (!(fieldType instanceof GraphQLInputObjectType)) {
throw new Error(
`Cannot get InputTypeComposer for field '${fieldName}' in type ${this.getTypeName()}. ` +
`This field should be InputObjectType, but it has type '${fieldType.constructor.name}'`
);
}
return InputTypeComposer.create(fieldType);
}
makeRequired(fieldNameOrArray: string | Array<string>): InputTypeComposer {
const fieldNames = Array.isArray(fieldNameOrArray) ? fieldNameOrArray : [fieldNameOrArray];
const fields = this.getFields();
fieldNames.forEach(fieldName => {
if (fields[fieldName] && fields[fieldName].type) {
if (!(fields[fieldName].type instanceof GraphQLNonNull)) {
fields[fieldName].type = new GraphQLNonNull(fields[fieldName].type);
}
}
});
this.setFields(fields);
return this;
}
makeOptional(fieldNameOrArray: string | Array<string>): InputTypeComposer {
const fieldNames = Array.isArray(fieldNameOrArray) ? fieldNameOrArray : [fieldNameOrArray];
const fields = this.getFields();
fieldNames.forEach(fieldName => {
if (fieldNames.indexOf(fieldName) > -1) {
if (fields[fieldName] && fields[fieldName].type instanceof GraphQLNonNull) {
fields[fieldName].type = fields[fieldName].type.ofType;
}
}
});
this.setFields(fields);
return this;
}
clone(newTypeName: string): InputTypeComposer {
if (!newTypeName) {
throw new Error('You should provide new type name for clone() method');
}
const fields = this.getFields();
const newFields = {};
Object.keys(fields).forEach(fieldName => {
newFields[fieldName] = { ...fields[fieldName] };
});
return new InputTypeComposer(
new GraphQLInputObjectType({
name: newTypeName,
fields: newFields,
})
);
}
getType(): GraphQLInputObjectType {
return this.gqType;
}
getTypeAsRequired(): GraphQLNonNull<GraphQLInputObjectType> {
return new GraphQLNonNull(this.gqType);
}
getTypeName(): string {
return this.gqType.name;
}
setTypeName(name: string): InputTypeComposer {
this.gqType.name = name;
return this;
}
getDescription(): string {
return this.gqType.description || '';
}
setDescription(description: string): InputTypeComposer {
this.gqType.description = description;
return this;
}
get(path: string | Array<string>): any {
return typeByPath(this, path);
}
}