UNPKG

1.81 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ONE_DAY = undefined;
7exports.getFields = getFields;
8exports.prettyPrint = prettyPrint;
9exports.toFilteredArray = toFilteredArray;
10exports.extendIncludes = extendIncludes;
11
12var _util = require('util');
13
14var _util2 = _interopRequireDefault(_util);
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18const ONE_DAY = exports.ONE_DAY = 24 * 60 * 60 * 1000;
19
20function 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
43function prettyPrint(obj, { depth = 5, colors = true, breakLength = 120 } = {}) {
44 console.log(_util2.default.inspect(obj, { depth, colors, breakLength }));
45}
46
47function toFilteredArray(obj) {
48 return (Array.isArray(obj) ? obj : [obj]).filter(x => x);
49}
50
51function 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