/
query-builder.ts
278 lines (240 loc) · 9.5 KB
/
query-builder.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
266
267
268
269
270
271
272
273
274
275
276
277
278
import Model from '../orm/model';
import { Arguments, Field } from '../support/interfaces';
import { upcaseFirstLetter } from '../support/utils';
import gql from 'graphql-tag';
import Context from '../common/context';
const inflection = require('inflection');
/**
* Contains all logic to build GraphQL queries/mutations.
*/
export default class QueryBuilder {
/**
* Builds a field for the GraphQL query and a specific model
*
* @param {Model|string} model The model to use
* @param {boolean} multiple Determines whether plural/nodes syntax or singular syntax is used.
* @param {Arguments} args The args that will be passed to the query field ( user(role: $role) )
* @param {Array<Model>} path The relations in this list are ignored (while traversing relations).
* Mainly for recursion
* @param {string} name Optional name of the field. If not provided, this will be the model name
* @param filter
* @param {boolean} allowIdFields Optional. Determines if id fields will be ignored for the argument generation.
* See buildArguments
* @returns {string}
*
* @todo Do we need the allowIdFields param?
*/
public static buildField (model: Model | string,
multiple: boolean = true,
args?: Arguments,
path: Array<string> = [],
name?: string,
filter: boolean = false,
allowIdFields: boolean = false): string {
const context = Context.getInstance();
model = context.getModel(model);
let params: string = this.buildArguments(model, args, false, filter, allowIdFields);
path = path.length === 0 ? [model.singularName] : path;
const fields = `
${model.getQueryFields().join(' ')}
${this.buildRelationsQuery(model, path)}
`;
if (multiple) {
const header: string = `${name ? name : model.pluralName}${params}`;
if (context.connectionQueryMode === 'nodes') {
return `
${header} {
nodes {
${fields}
}
}
`;
} else if (context.connectionQueryMode === 'edges') {
return `
${header} {
edges {
node {
${fields}
}
}
}
`;
} else {
return `
${header} {
${fields}
}
`;
}
} else {
return `
${name ? name : model.singularName}${params} {
${fields}
}
`;
}
}
/**
* Generates a query.
* Currently only one root field for the query is possible.
* @param {string} type 'mutation' or 'query'
* @param {Model | string} model The model this query or mutation affects. This mainly determines the query fields.
* @param {string} name Optional name of the query/mutation. Will overwrite the name from the model.
* @param {Arguments} args Arguments for the query
* @param {boolean} multiple Determines if the root query field is a connection or not (will be passed to buildField)
* @param {boolean} filter When true the query arguments are passed via a filter object.
* @returns {any} Whatever gql() returns
*/
public static buildQuery (type: string, model: Model | string, name?: string, args?: Arguments, multiple?: boolean,
filter?: boolean) {
const context = Context.getInstance();
// model
model = context.getModel(model);
if (!model) throw new Error('No model provided to build the query!');
// args
args = args ? JSON.parse(JSON.stringify(args)) : {};
if (!args) throw new Error('args is undefined');
Object.keys(args).forEach((key: string) => {
if (args && args[key] && typeof args[key] === 'object') {
args[key] = { __type: upcaseFirstLetter(key) };
}
});
// multiple
multiple = multiple === undefined ? !args['id'] : multiple;
// name
if (!name) name = (multiple ? model.pluralName : model.singularName);
// build query
const query: string =
`${type} ${upcaseFirstLetter(name)}${this.buildArguments(model, args, true, false)} {\n` +
` ${this.buildField(model, multiple, args, [], name, filter, true)}\n` +
`}`;
return gql(query);
}
/**
* Generates the arguments string for a graphql query based on a given map.
*
* There are three types of arguments:
*
* 1) Signatures with primitive types (signature = true)
* => 'mutation createUser($name: String!)'
*
* 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
* => 'mutation createUser($user: UserInput!)'
*
* 3) Fields with variables (signature = false)
* => 'user(id: $id)'
*
* 4) Filter fields with variables (signature = false, filter = true)
* => 'users(filter: { active: $active })'
*
* @param model
* @param {Arguments | undefined} args
* @param {boolean} signature When true, then this method generates a query signature instead of key/value pairs
* @param filter
* @param {boolean} allowIdFields If true, ID fields will be included in the arguments list
* @returns {String}
*/
private static buildArguments (model: Model, args?: Arguments, signature: boolean = false, filter: boolean = false,
allowIdFields: boolean = true): string {
if (args === undefined) return '';
let returnValue: string = '';
let first: boolean = true;
if (args) {
Object.keys(args).forEach((key: string) => {
let value: any = args[key];
const isForeignKey = model.skipField(key);
const skipFieldDueId = (key === 'id' || isForeignKey) && !allowIdFields;
// Ignore null fields, ids and connections
if (value && !(value instanceof Array || skipFieldDueId)) {
let typeOrValue: any = '';
if (signature) {
if (typeof value === 'object' && value.__type) {
// Case 2 (User!)
typeOrValue = value.__type + 'Input!';
} else if (key === 'id' || isForeignKey) {
// Case 1 (ID!)
typeOrValue = 'ID!';
} else {
// Case 1 (String!)
typeOrValue = this.determineAttributeType(model, key, value);
typeOrValue = typeOrValue + '!';
}
} else {
// Case 3 or 4
typeOrValue = `$${key}`;
}
returnValue = `${returnValue}${first ? '' : ', '}${(signature ? '$' : '') + key}: ${typeOrValue}`;
first = false;
}
});
if (!first) {
if (filter) returnValue = `filter: { ${returnValue} }`;
returnValue = `(${returnValue})`;
}
}
return returnValue;
}
/**
* Determines the GraphQL primitive type of a field in the variables hash by the field type or (when
* the field type is generic attribute) by the variable type.
* @param {Model} model
* @param {string} key
* @param {string} value
* @returns {string}
*/
private static determineAttributeType (model: Model, key: string, value: any): string {
const context: Context = Context.getInstance();
const field: undefined | Field = model.fields.get(key);
const schemaField = context.schema!.getType(model.singularName).fields!.find(f => f.name === key);
if (schemaField && schemaField.type.name) {
return schemaField.type.name;
} else {
if (field instanceof context.components.String) {
return 'String';
} else if (field && field instanceof context.components.Number) {
return 'Int';
} else if (field && field instanceof context.components.Boolean) {
return 'Boolean';
} else {
if (typeof value === 'number') return 'Int';
if (typeof value === 'string') return 'String';
if (typeof value === 'boolean') return 'Boolean';
throw new Error(`Can't find suitable graphql type for field '${model.singularName}.${key}'.`);
}
}
}
/**
* Generates the fields for all related models.
*
* @param {Model} model
* @param {Array<Model>} path
* @returns {string}
*/
private static buildRelationsQuery (model: (null | Model), path: Array<string> = []): string {
if (model === null) return '';
const context = Context.getInstance();
const relationQueries: Array<string> = [];
model.getRelations().forEach((field: Field, name: string) => {
let relatedModel: Model;
if (field.related) {
relatedModel = context.getModel(field.related.entity);
} else if (field.parent) {
relatedModel = context.getModel(field.parent.entity);
} else {
relatedModel = context.getModel(name);
context.logger.log('WARNING: field has neither parent nor related property. Fallback to attribute name', field);
}
const singularizedFieldName = inflection.singularize(name);
const ignore = path.includes(singularizedFieldName);
// console.log(`-----> Will ${ignore ? '' : 'not'} ignore ${model.singularName}.${name}, path: ${path.join('.')}`);
if (model.shouldEagerLoadRelation(name, field, relatedModel) && !ignore) {
const multiple: boolean = !(field instanceof context.components.BelongsTo ||
field instanceof context.components.HasOne);
const newPath = path.slice(0);
newPath.push(singularizedFieldName);
relationQueries.push(this.buildField(relatedModel, multiple, undefined, newPath, name, false));
}
});
return relationQueries.join('\n');
}
}