-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathqueryMap.js
More file actions
280 lines (259 loc) · 9.82 KB
/
queryMap.js
File metadata and controls
280 lines (259 loc) · 9.82 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
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
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-use-before-define */
/* eslint-disable max-len */
/** ******************************************************
***** ARGS & QUERY OBJECT OUTPUT FUNCTION ***************
******************************************************* */
/**
*
* @param {*} manifest: the manifest object created by the user, which contains REST endpoints and corresponding GraphQL operations
* @param {*} schema: the schema that contains all relevant GraphQL operations and types, including those specified in the manifest; must be a GraphQLSchema object
* @param {*} customScalars: an array containing all custom scalar types that exist in the schema argument; each element should be a custom scalar type and in string format (e.g., ['Month', 'Year'])
// if no custom scalar types are used in the schema, then do not pass an argument for this parameter
* @returns: an object containing the two keys below; this is an input into the routerCreation function.
// args: an object composed of key-value pairs for any operations (keys) that have argument inputs (values); note that operations without argument inputs will not be included here
// queries: an object composed of key-value pairs for all operations (keys) specified in the manifest and the corresponding GraphQL schema (values) to be invoked
*/
const queryMap = (manifest, schema, customScalars = []) => {
if (typeof manifest !== 'object') {
throw new Error('manifest argument must an object');
}
if (typeof schema !== 'object') {
throw new Error('schema argument must be a GraphQLSchema object');
}
if (typeof customScalars !== 'object') {
throw new Error('customScalars argument must be an array');
}
const endPoints = manifest.endpoints;
const argsObj = {};
const queryObj = {};
const scalarTypes = [
'String',
'Int',
'ID',
'Boolean',
'Float',
...customScalars,
];
for (const path of Object.keys(endPoints)) {
for (const action of Object.keys(endPoints[path])) {
const operationName = endPoints[path][action].operation;
// generate the args object
const typeSchema = typeChecker(schema, operationName)[1];
const operationFields = typeSchema[operationName];
const varObj = grabArgs(schema, operationFields.args, scalarTypes)[1];
argsObj[operationName] = varObj;
// generate the query object
queryObj[operationName] = generateQuery(
schema,
operationName,
scalarTypes,
);
}
}
return {
args: argsObj,
queries: queryObj,
};
};
/** ********************************
***** QUERY GENERATOR FUNCTION ****
********************************* */
// This is invoked within queryMap; can also be used to generate the GraphQL fully expanded query string for any single operation
/**
*
* @param {*} schema: same schema that is passed into queryMap
* @param {*} operation: a GraphQL operation that has been mapped to a REST endpoint within the manifest object
* @param {*} scalarTypes: an array containing all standard scalar types as well as any custom scalar types that have been declared in the schema (scalarTypes is declared in queryMap and this is a pass-through parameter)
* @returns: a string comprising the GraphQL query that corresponds to the operation argument; this query will be passed to the GraphQL server each time a request is made to the corresponding REST endpoint
// The query string includes whichever parts are relevant to the operation, including the operation type, variables specification, operation name, arguments specification, fields specification (all available fields are requested)
*/
const generateQuery = (schema, operation, scalarTypes) => {
// determine whether it is a query or mutation
const typeInfo = typeChecker(schema, operation);
const operationType = typeInfo[0];
const typeSchema = typeInfo[1];
// look for all of the fields that need to be specified for the operation
let returnFields = {};
const operationFields = typeSchema[operation];
let customTypeFields;
let customType;
const recursiveBreak = [];
// check to see if the type is a scalar type -> if not, then need to look up the fields for each type
const operationFieldsTypeTrim = typeTrim(operationFields.type.toString());
if (scalarTypes.includes(operationFieldsTypeTrim)) {
returnFields[operationFieldsTypeTrim] = '';
} else {
customType = operationFields.type;
customTypeFields = schema
.getType(typeTrim(operationFields.type.toString()))
.getFields();
returnFields = grabFields(
schema,
customType,
customTypeFields,
recursiveBreak,
scalarTypes,
);
}
const queryString = buildString(returnFields);
// compose the variable and argument portions of the query if necessary
let argsString;
let varsString;
if (operationFields.args.length) {
const argsObj = grabArgs(schema, operationFields.args, scalarTypes)[0];
const argsVal = argsStrFormatter(buildString(argsObj));
argsString = `( ${argsVal} )`;
const varObj = grabArgs(schema, operationFields.args, scalarTypes)[1];
const varsVal = varStrBuild(varObj);
varsString = `( ${varsVal} )`;
argsObj[operation] = argsVal;
} else {
argsString = '';
varsString = '';
}
return `${operationType.toLowerCase()} ${varsString} { ${operation} ${argsString} { ${queryString} } }`;
};
/** ************************
***** ADDITIONAL FUNCTIONS
************************** */
/* determines whether the operation is a query or mutation */
const typeChecker = (schema, operation) => {
let operationType;
let typeSchema;
const querySchema = schema.getQueryType().getFields();
const mutationSchema = schema.getMutationType().getFields();
if (Object.keys(querySchema).includes(operation)) {
operationType = 'Query';
typeSchema = querySchema;
}
if (Object.keys(mutationSchema).includes(operation)) {
operationType = 'Mutation';
typeSchema = mutationSchema;
}
if (!operationType) {
throw new Error(`Operation '${operation}' is not defined in the schema`);
}
return [operationType, typeSchema];
};
/* converts custom type text to simple strings */
const typeTrim = (type) => {
const typeArr = type.split('');
const trimArr = [];
for (let i = 0; i < typeArr.length; i += 1) {
if (typeArr[i] !== '[' && typeArr[i] !== ']' && typeArr[i] !== '!') {
trimArr.push(typeArr[i]);
}
}
return trimArr.join('');
};
/* grabFields collects all of the fields associated with a type; if the field is scalar type, it adds to the return object;
if the field is a custom type, the function is invoked again on that field's schema fields continues recursively until only scalar types are found
countOccurrences is used to track the number of times each customType has been called */
const countOccurrences = (array, val) => array.reduce((a, v) => (v === val ? a + 1 : a), 0);
const grabFields = (
schema,
customTypeName,
customTypeSchema,
recursiveBreakArr,
scalarTypes,
) => {
const returnObj = {};
for (const key of Object.keys(customTypeSchema)) {
const typeString = typeTrim(customTypeSchema[key].type.toString());
if (scalarTypes.includes(typeString)) returnObj[key] = '';
else {
recursiveBreakArr.push(typeString);
if (countOccurrences(recursiveBreakArr, typeString) < 2) {
returnObj[key] = grabFields(
schema,
typeString,
schema.getType(typeString).getFields(),
recursiveBreakArr,
scalarTypes,
);
}
}
}
return returnObj;
};
/* convert the query/args object to string version; called recursively if there are nested type objs */
const buildString = (fieldsObj) => {
const queryArr = [];
for (const key of Object.keys(fieldsObj)) {
queryArr.push(key);
if (fieldsObj[key] !== '') {
queryArr.push('{');
queryArr.push(buildString(fieldsObj[key]));
queryArr.push('}');
}
}
return queryArr.join(' ');
};
/* collects all of the arguments, handling all potential cases:
1) single scalar arg
2) multiple scalar args
3) custom input types as args */
const grabArgs = (schema, argsArr, scalarTypes) => {
const returnArgsObj = {};
const returnVarsObj = {};
for (let i = 0; i < argsArr.length; i += 1) {
const typeString = typeTrim(argsArr[i].type.toString());
if (scalarTypes.includes(typeString)) {
returnArgsObj[argsArr[i].name] = '';
returnVarsObj[`$${argsArr[i].name}`] = argsArr[i].type.toString();
} else {
const nestedFields = grabFields(
schema,
typeString,
schema.getType(typeString).getFields(),
[],
scalarTypes,
);
returnArgsObj[argsArr[i].name] = nestedFields;
for (const field of Object.keys(nestedFields)) {
returnVarsObj[`$${field}`] = schema.getType(typeString).getFields()[
field
].type;
}
}
}
return [returnArgsObj, returnVarsObj];
};
/* formats the args string into the arg:$arg format */
const argsStrFormatter = (str) => {
const strArray = str.split(' ');
const insIndex = strArray.indexOf('{');
if (insIndex > 0) {
for (let i = insIndex + 1; i < strArray.length - 1; i += 1) {
strArray[i] = `${strArray[i]}:$${strArray[i]},`;
}
if (insIndex > 1) strArray[0] = `${strArray[0]}:$${strArray[0]},`;
strArray.splice(insIndex, 0, ':');
} else {
for (let i = 0; i < strArray.length; i += 1) {
strArray[i] = `${strArray[i]}:$${strArray[i]},`;
}
}
return strArray.join(' ');
};
/* formats the args string into the $var: type format for variables */
const varStrBuild = (varObj) => {
const varArr = [];
for (const key of Object.keys(varObj)) {
varArr.push(`${key}:`);
varArr.push(`${varObj[key]},`);
}
return varArr.join(' ');
};
module.exports = {
queryMap,
generateQuery,
typeChecker,
typeTrim,
grabFields,
buildString,
grabArgs,
argsStrFormatter,
varStrBuild,
};