Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 3x 3x 120x 120x 84x 249x 84x 24x 120x 324x 302x 60x 22x 6x 6x 3x 34x 34x 34x 50x 50x 34x 3x 13x 13x 13x 13x 6x 18x 6x 6x 45x 6x 6x 15x 15x 96x 15x 15x 93x 15x 4x 11x 11x 15x 13x 4x 6x 4x 31x 4x 4x 4x 4x 4x 2x 2x 4x 13x 3x | import { visit, BREAK } from 'graphql' import { checkDocument, cloneDeep } from 'apollo-utilities' const PERSIST_FIELD = { kind: 'Field', name: { kind: 'Name', value: '__persist' } } const addPersistFieldToSelectionSet = (selectionSet, isRoot = false) => { Eif (selectionSet.selections) { if (!isRoot) { const alreadyHasThisField = selectionSet.selections.some(selection => { return ( selection.kind === 'Field' && selection.name.value === '__typename' ) }) if (!alreadyHasThisField) { selectionSet.selections.push(PERSIST_FIELD) } } selectionSet.selections.forEach(selection => { // Must not add __typename if we're inside an introspection query if (selection.kind === 'Field') { if ( selection.name.value.lastIndexOf('__', 0) !== 0 && selection.selectionSet ) { addPersistFieldToSelectionSet(selection.selectionSet) } } else if (selection.kind === 'InlineFragment') { Eif (selection.selectionSet) { addPersistFieldToSelectionSet(selection.selectionSet) } } }) } } const addPersistFieldToDocument = doc => { checkDocument(doc) const docClone = cloneDeep(doc) docClone.definitions.forEach(definition => { const isRoot = definition.kind === 'OperationDefinition' addPersistFieldToSelectionSet(definition.selectionSet, isRoot) }) return docClone } const extractPersistDirectivePaths = (originalQuery, directive = 'persist') => { const paths = [] const fragmentPaths = {} const fragmentPersistPaths = {} const query = visit(originalQuery, { FragmentSpread: ( { name: { value: name } }, key, parent, path, ancestors ) => { const root = ancestors.find( ({ kind }) => kind === 'OperationDefinition' || kind === 'FragmentDefinition' ) const rootKey = root.kind === 'FragmentDefinition' ? root.name.value : '$ROOT' const fieldPath = ancestors .filter(({ kind }) => kind === 'Field') .map(({ name: { value: name } }) => name) fragmentPaths[name] = [rootKey].concat(fieldPath) }, Directive: ({ name: { value: name } }, key, parent, path, ancestors) => { Eif (name === directive) { const fieldPath = ancestors .filter(({ kind }) => kind === 'Field') .map(({ name: { value: name } }) => name) const fragmentDefinition = ancestors.find( ({ kind }) => kind === 'FragmentDefinition' ) // If we are inside a fragment, we must save the reference. if (fragmentDefinition) { fragmentPersistPaths[fragmentDefinition.name.value] = fieldPath } else Eif (fieldPath.length) { paths.push(fieldPath) } return null } } }) // In case there are any FragmentDefinition items, we need to combine paths. if (Object.keys(fragmentPersistPaths).length) { visit(originalQuery, { FragmentSpread: ( { name: { value: name } }, key, parent, path, ancestors ) => { if (fragmentPersistPaths[name]) { let fieldPath = ancestors .filter(({ kind }) => kind === 'Field') .map(({ name: { value: name } }) => name) fieldPath = fieldPath.concat(fragmentPersistPaths[name]) let fragment = name let parent = fragmentPaths[fragment][0] while (parent && parent !== '$ROOT' && fragmentPaths[parent]) { fieldPath = fragmentPaths[parent].slice(1).concat(fieldPath) parent = fragmentPaths[parent][0] } paths.push(fieldPath) } } }) } return { query, paths } } const hasPersistDirective = doc => { let hasDirective = false visit(doc, { Directive: ({ name: { value: name } }) => { if (name === 'persist') { hasDirective = true return BREAK } } }) return hasDirective } export { addPersistFieldToDocument, extractPersistDirectivePaths, hasPersistDirective } |