1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.ONE_DAY = undefined;
|
7 | exports.getFields = getFields;
|
8 | exports.prettyPrint = prettyPrint;
|
9 | exports.toFilteredArray = toFilteredArray;
|
10 | exports.extendIncludes = extendIncludes;
|
11 |
|
12 | var _util = require('util');
|
13 |
|
14 | var _util2 = _interopRequireDefault(_util);
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | const ONE_DAY = exports.ONE_DAY = 24 * 60 * 60 * 1000;
|
19 |
|
20 | function getFields(info, fragments = info.fragments) {
|
21 | if (info.kind !== 'Field') {
|
22 | info = info.fieldNodes[0];
|
23 | }
|
24 | const selections = info.selectionSet.selections;
|
25 | const reducer = (fields, selection) => {
|
26 | if (selection.kind === 'FragmentSpread') {
|
27 | const name = selection.name.value;
|
28 | const fragment = fragments[name];
|
29 | if (!fragment) {
|
30 | throw new Error(`Fragment '${name}' was not passed to getFields()`);
|
31 | }
|
32 | fragment.selectionSet.selections.reduce(reducer, fields);
|
33 | } else if (selection.kind === 'InlineFragment') {
|
34 | selection.selectionSet.selections.reduce(reducer, fields);
|
35 | } else {
|
36 | fields[selection.name.value] = selection;
|
37 | }
|
38 | return fields;
|
39 | };
|
40 | return selections.reduce(reducer, {});
|
41 | }
|
42 |
|
43 | function prettyPrint(obj, { depth = 5, colors = true, breakLength = 120 } = {}) {
|
44 | console.log(_util2.default.inspect(obj, { depth, colors, breakLength }));
|
45 | }
|
46 |
|
47 | function toFilteredArray(obj) {
|
48 | return (Array.isArray(obj) ? obj : [obj]).filter(x => x);
|
49 | }
|
50 |
|
51 | function extendIncludes(includes, moreIncludes) {
|
52 | includes = toFilteredArray(includes);
|
53 | moreIncludes = toFilteredArray(moreIncludes);
|
54 | const seen = {};
|
55 | return includes.concat(moreIncludes).filter(x => {
|
56 | if (seen[x]) {
|
57 | return false;
|
58 | }
|
59 | seen[x] = true;
|
60 | return true;
|
61 | });
|
62 | } |
\ | No newline at end of file |