-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
623 lines (537 loc) · 14.6 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*!
* ISC License
*
* Copyright (c) 2018-present, Mykhailo Stadnyk <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
import {
ArgumentNode,
DirectiveNode,
SelectionNode,
FragmentDefinitionNode,
GraphQLResolveInfo,
FieldNode,
} from 'graphql';
/**
* Pre-compiled wildcard replacement regexp
*
* @type {RegExp}
*/
const RX_AST: RegExp = /\*/g;
/**
* Fragment item type
*
* @access public
*/
export interface FragmentItem {
[name: string]: FragmentDefinitionNode;
}
/**
* Field names transformation map interface
*
* @access public
*/
export interface FieldNamesMap {
[name: string]: string;
}
/**
* fieldsList options argument interface
*
* @access public
*/
export interface FieldsListOptions {
/**
* Path to a tree branch which should be mapped during fields extraction
* @type {string}
*/
path?: string;
/**
* Transformation rules which should be used to re-name field names
* @type {FieldNamesMap}
*/
transform?: FieldNamesMap;
/**
* Flag which turns on/off GraphQL directives checks on a fields
* and take them into account during fields analysis
* @type {boolean}
*/
withDirectives?: boolean;
/**
* Flag which turns on/off whether to return the parent fields or not
* @type {boolean}
*/
keepParentField?: boolean;
/**
* Fields skip rule patterns. Usually used to ignore part of request field
* subtree. For example if query looks like:
* profiles {
* id
* users {
* name
* email
* password
* }
* }
* and you doo n not care about users, it can be done like:
* fieldsList(info, { skip: ['users'] }); // or
* fieldsProjection(info, { skip: ['users.*'] }); // more obvious notation
*
* If you want to skip only exact fields, it can be done as:
* fieldsMap(info, { skip: ['users.email', 'users.password'] })
*/
skip?: string[];
}
/**
* Type definition for variables values map
*
* @access public
*/
export interface VariablesValues {
[name: string]: any;
}
/**
* Fields projection object, where keys are dot-notated field paths
* ended-up with a truthy (1) value
*
* @access public
*/
export interface FieldsProjection {
[name: string]: 1;
}
/**
* Traverse query nodes tree options arg interface
* @access private
*/
interface TraverseOptions {
fragments: FragmentItem;
vars: any;
withVars?: boolean;
}
/**
* Retrieves a list of nodes from a given selection (either fragment or
* selection node)
*
* @param {FragmentDefinitionNode | FieldNode} selection
* @return {ReadonlyArray<FieldNode>}
* @access private
*/
function getNodes(
selection: FragmentDefinitionNode | SelectionNode,
): ReadonlyArray<SelectionNode> {
return (selection as any)?.selectionSet?.selections ||
[] as ReadonlyArray<SelectionNode>
;
}
/**
* Checks if a given directive name and value valid to return a field
*
* @param {string} name
* @param {boolean} value
* @return boolean
* @access private
*/
function checkValue(name: string, value: boolean): boolean {
return name === 'skip'
? !value
: name === 'include' ? value : true
;
}
/**
* Checks if a given directive arg allows to return field
*
* @param {string} name - directive name
* @param {ArgumentNode} arg
* @param {VariablesValues} vars
* @return {boolean}
* @access private
*/
function verifyDirectiveArg(
name: string,
arg: ArgumentNode,
vars: VariablesValues
): boolean {
switch (arg.value.kind) {
case 'BooleanValue':
return checkValue(name, arg.value.value);
case 'Variable':
return checkValue(name, vars[arg.value.name.value]);
}
return true;
}
/**
* Checks if a given directive allows to return field
*
* @param {DirectiveNode} directive
* @param {VariablesValues} vars
* @return {boolean}
* @access private
*/
function verifyDirective(
directive: DirectiveNode,
vars: VariablesValues,
): boolean {
const directiveName: string = directive.name.value;
if (!~['include', 'skip'].indexOf(directiveName)) {
return true;
}
let args: any[] = directive.arguments as any[];
if (!(args && args.length)) {
args = [];
}
for (const arg of args) {
if (!verifyDirectiveArg(directiveName, arg, vars)) {
return false;
}
}
return true;
}
/**
* Checks if a given list of directives allows to return field
*
* @param {ReadonlyArray<DirectiveNode>} directives
* @param {VariablesValues} vars
* @return {boolean}
* @access private
*/
function verifyDirectives(
directives: ReadonlyArray<DirectiveNode> | undefined,
vars: VariablesValues,
): boolean {
if (!directives || !directives.length) {
return true;
}
vars = vars || {};
for (const directive of directives) {
if (!verifyDirective(directive, vars)) {
return false;
}
}
return true;
}
type SkipValue = boolean | SkipTree;
type SkipTree = { [key: string]: SkipValue };
/**
* Checks if a given node is inline fragment and process it,
* otherwise does nothing and returns false.
*
* @param {SelectionNode} node
* @param {MapResult | MapResultKey} root
* @param {*} skip
* @param {TraverseOptions} opts
*/
function verifyInlineFragment(
node: SelectionNode,
root: MapResult | MapResultKey,
opts: TraverseOptions,
skip: SkipValue,
): boolean {
if (node.kind === 'InlineFragment') {
const nodes = getNodes(node);
nodes.length && traverse(nodes, root, opts, skip);
return true;
}
return false;
}
/**
* Builds skip rules tree from a given skip option argument
*
* @param {string[]} skip - skip option arguments
* @return {SkipTree} - skip rules tree
*/
function skipTree(skip: string[]): SkipTree {
const tree: SkipTree = {};
for (const pattern of skip) {
const props = pattern.split('.');
let propTree: SkipTree = tree;
for (let i = 0, s = props.length; i < s; i++) {
const prop = props[i];
const all = props[i + 1] === '*';
if (!propTree[prop]) {
propTree[prop] = i === s - 1 || all ? true : {};
all && i++;
}
propTree = propTree[prop] as SkipTree;
}
}
return tree;
}
/**
*
* @param node
* @param skip
*/
function verifySkip(node: string, skip: SkipValue): SkipValue {
if (!skip) {
return false;
}
// true['string'] is a valid operation is JS resulting in `undefined`
if ((skip as SkipTree)[node]) {
return (skip as SkipTree)[node];
}
// lookup through wildcard patterns
let nodeTree: SkipValue = false;
const patterns = Object.keys(skip).filter(
pattern => ~pattern.indexOf('*'),
);
for (const pattern of patterns) {
const rx: RegExp = new RegExp(pattern.replace(RX_AST, '.*'));
if (rx.test(node)) {
nodeTree = (skip as SkipTree)[pattern];
// istanbul ignore else
if (nodeTree === true) {
break;
}
}
}
return nodeTree;
}
export type MapResultKey = false | MapResult;
export type MapResult = { [key: string]: MapResultKey };
/**
* Traverses recursively given nodes and fills-up given root tree with
* a requested field names
*
* @param {ReadonlyArray<FieldNode>} nodes
* @param {MapResult | MapResultKey} root
* @param {TraverseOptions} opts
* @param {SkipValue} skip
* @return {MapResult}
* @access private
*/
function traverse(
nodes: ReadonlyArray<SelectionNode>,
root: MapResult | MapResultKey,
opts: TraverseOptions,
skip: SkipValue,
): MapResult | MapResultKey {
for (const node of nodes) {
if (opts.withVars && !verifyDirectives(node.directives, opts.vars)) {
continue;
}
if (verifyInlineFragment(node, root, opts, skip)) {
continue;
}
const name = (node as FieldNode).name.value;
if (opts.fragments[name]) {
traverse(getNodes(opts.fragments[name]), root, opts, skip);
continue;
}
const nodes = getNodes(node);
const nodeSkip = verifySkip(name, skip);
if (nodeSkip !== true) {
(root as MapResult)[name] = (root as MapResult)[name] || (
nodes.length ? {} : false
);
nodes.length && traverse(
nodes,
(root as MapResult)[name],
opts,
nodeSkip,
);
}
}
return root;
}
/**
* Retrieves and returns a branch from a given tree by a given path
*
* @param {MapResult} tree
* @param {string} [path]
* @return {MapResult}
* @access private
*/
function getBranch(tree: MapResult, path?: string): MapResult {
if (!path) {
return tree;
}
for (const fieldName of path.split('.')) {
const branch = tree[fieldName];
if (!branch) {
return {};
}
tree = branch;
}
return tree;
}
/**
* Verifies if a given info object is valid. If valid - returns
* proper FieldNode object for given resolver node, otherwise returns null.
*
* @param {GraphQLResolveInfo} info
* @return {FieldNode | null}
* @access private
*/
function verifyInfo(info: GraphQLResolveInfo): SelectionNode | null {
if (!info) {
return null;
}
if (!info.fieldNodes && (info as any).fieldASTs) {
(info as any).fieldNodes = (info as any).fieldASTs;
}
if (!info.fieldNodes) {
return null;
}
return verifyFieldNode(info);
}
/**
* Verifies if a proper fieldNode existing on given info object
*
* @param {GraphQLResolveInfo} info
* @return {FieldNode | null}
* @access private
*/
function verifyFieldNode(info: GraphQLResolveInfo): FieldNode | null {
const fieldNode: FieldNode | undefined = info.fieldNodes.find(
(node: FieldNode) =>
node && node.name && node.name.value === info.fieldName
);
if (!(fieldNode && fieldNode.selectionSet)) {
return null;
}
return fieldNode;
}
/**
* Parses input options and returns prepared options object
*
* @param {FieldsListOptions} options
* @return {FieldsListOptions}
* @access private
*/
function parseOptions(options?: FieldsListOptions): FieldsListOptions {
if (!options) {
return {};
}
if (options.withDirectives === undefined) {
options.withDirectives = true;
}
return options;
}
/**
* Extracts and returns requested fields tree.
* May return `false` if path option is pointing to leaf of tree
*
* @param {GraphQLResolveInfo} info
* @param {FieldsListOptions} options
* @return {MapResult}
* @access public
*/
export function fieldsMap(
info: GraphQLResolveInfo,
options?: FieldsListOptions,
): MapResult {
const fieldNode = verifyInfo(info);
if (!fieldNode) {
return {};
}
const { path, withDirectives, skip } = parseOptions(options);
const tree = traverse(getNodes(fieldNode), {}, {
fragments: info.fragments,
vars: info.variableValues,
withVars: withDirectives,
},
skipTree(skip || []),
) as MapResult;
return getBranch(tree, path);
}
/**
* Extracts list of selected fields from a given GraphQL resolver info
* argument and returns them as an array of strings, using the given
* extraction options.
*
* @param {GraphQLResolveInfo} info - GraphQL resolver info object
* @param {FieldsListOptions} [options] - fields list extraction options
* @return {string[]} - array of field names
* @access public
*/
export function fieldsList(
info: GraphQLResolveInfo,
options: FieldsListOptions = {},
): string[] {
return Object.keys(fieldsMap(info, options)).map((field: string) =>
(options.transform || {})[field] || field,
);
}
/**
* Combines parent path with child name to fully-qualified dot-notation path
* of a child
*
* @param {string} parent
* @param {string} child
* @return {string}
* @access private
*/
function toDotNotation(parent: string, child: string): string {
return `${parent ? parent + '.' : ''}${child}`
}
/**
* Extracts projection of selected fields from a given GraphQL resolver info
* argument and returns flat fields projection object, where keys are object
* paths in dot-notation form.
*
* @param {GraphQLResolveInfo} info - GraphQL resolver info object
* @param {FieldsListOptions} options - fields list extraction options
* @return {FieldsProjection} - fields projection object
* @access public
*/
export function fieldsProjection(
info: GraphQLResolveInfo,
options?: FieldsListOptions,
): FieldsProjection {
const tree = fieldsMap(info, options);
const stack: any[] = [];
const map: FieldsProjection = {};
const transform = (options || {}).transform || {};
stack.push({ node: '', tree });
while (stack.length) {
for (const j of Object.keys(stack[0].tree)) {
if (stack[0].tree[j]) {
const nodeDottedName = toDotNotation(stack[0].node, j);
stack.push({
node: nodeDottedName,
tree: stack[0].tree[j],
});
if (options?.keepParentField) {
map[nodeDottedName] = 1;
}
continue;
}
let dotName = toDotNotation(stack[0].node, j);
if (transform[dotName]) {
dotName = transform[dotName];
}
map[dotName] = 1;
}
stack.shift();
}
return map;
}
// istanbul ignore next
if (process.env['IS_UNIT_TEST']) {
// noinspection JSUnusedGlobalSymbols
Object.assign(module.exports, {
getNodes,
traverse,
getBranch,
verifyDirectives,
verifyDirective,
verifyDirectiveArg,
checkValue,
verifyInfo,
verifyFieldNode,
verifyInlineFragment,
verifySkip,
parseOptions,
toDotNotation,
});
}