UNPKG

1.4 kBJavaScriptView Raw
1import util from 'util'
2
3export const ONE_DAY = 24 * 60 * 60 * 1000
4
5export function getFields(info, fragments = info.fragments) {
6 if (info.kind !== 'Field') {
7 info = info.fieldNodes[0]
8 }
9 const selections = info.selectionSet.selections
10 const reducer = (fields, selection) => {
11 if (selection.kind === 'FragmentSpread') {
12 const name = selection.name.value
13 const fragment = fragments[name]
14 if (!fragment) {
15 throw new Error(`Fragment '${name}' was not passed to getFields()`)
16 }
17 fragment.selectionSet.selections.reduce(reducer, fields)
18 } else if (selection.kind === 'InlineFragment') {
19 selection.selectionSet.selections.reduce(reducer, fields)
20 } else {
21 fields[selection.name.value] = selection
22 }
23 return fields
24 }
25 return selections.reduce(reducer, {})
26}
27
28export function prettyPrint(
29 obj,
30 { depth = 5, colors = true, breakLength = 120 } = {}
31) {
32 console.log(util.inspect(obj, { depth, colors, breakLength }))
33}
34
35export function toFilteredArray(obj) {
36 return (Array.isArray(obj) ? obj : [obj]).filter(x => x)
37}
38
39export function extendIncludes(includes, moreIncludes) {
40 includes = toFilteredArray(includes)
41 moreIncludes = toFilteredArray(moreIncludes)
42 const seen = {}
43 return includes.concat(moreIncludes).filter(x => {
44 if (seen[x]) {
45 return false
46 }
47 seen[x] = true
48 return true
49 })
50}