UNPKG

1.47 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 (obj, { depth = 5,
29 colors = true,
30 breakLength = 120 } = {}) {
31 console.log(util.inspect(obj, { depth, colors, breakLength }))
32}
33
34export function toFilteredArray (obj) {
35 return (Array.isArray(obj) ? obj : [obj]).filter(x => x)
36}
37
38export function extendIncludes (includes, moreIncludes) {
39 includes = toFilteredArray(includes)
40 moreIncludes = toFilteredArray(moreIncludes)
41 const seen = {}
42 return includes.concat(moreIncludes).filter(x => {
43 if (seen[x]) {
44 return false
45 }
46 seen[x] = true
47 return true
48 })
49}