|
| 1 | +/** |
| 2 | + * Taken from https://github.com/TallerWebSolutions/apollo-cache-instorage |
| 3 | + */ |
| 4 | +import { visit, BREAK } from 'graphql'; |
| 5 | +import { checkDocument, cloneDeep } from '@apollo/client'; |
| 6 | + |
| 7 | +const PERSIST_FIELD = { |
| 8 | + kind: 'Field', |
| 9 | + name: { |
| 10 | + kind: 'Name', |
| 11 | + value: '__persist' |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +const addPersistFieldToSelectionSet = (selectionSet, isRoot = false) => { |
| 16 | + if (selectionSet.selections) { |
| 17 | + if (!isRoot) { |
| 18 | + const alreadyHasThisField = selectionSet.selections.some(selection => { |
| 19 | + return ( |
| 20 | + selection.kind === 'Field' && selection.name.value === '__typename' |
| 21 | + ) |
| 22 | + }) |
| 23 | + |
| 24 | + if (!alreadyHasThisField) { |
| 25 | + selectionSet.selections.push(PERSIST_FIELD) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + selectionSet.selections.forEach(selection => { |
| 30 | + // Must not add __typename if we're inside an introspection query |
| 31 | + if (selection.kind === 'Field') { |
| 32 | + if ( |
| 33 | + selection.name.value.lastIndexOf('__', 0) !== 0 && |
| 34 | + selection.selectionSet |
| 35 | + ) { |
| 36 | + addPersistFieldToSelectionSet(selection.selectionSet) |
| 37 | + } |
| 38 | + } |
| 39 | + else if (selection.kind === 'InlineFragment') { |
| 40 | + if (selection.selectionSet) { |
| 41 | + addPersistFieldToSelectionSet(selection.selectionSet) |
| 42 | + } |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const addPersistFieldToDocument = doc => { |
| 49 | + checkDocument(doc) |
| 50 | + const docClone = cloneDeep(doc) |
| 51 | + |
| 52 | + docClone.definitions.forEach(definition => { |
| 53 | + const isRoot = definition.kind === 'OperationDefinition' |
| 54 | + addPersistFieldToSelectionSet(definition.selectionSet, isRoot) |
| 55 | + }) |
| 56 | + |
| 57 | + return docClone |
| 58 | +} |
| 59 | + |
| 60 | +const extractPersistDirectivePaths = (originalQuery, directive = 'persist') => { |
| 61 | + const paths = [] |
| 62 | + const fragmentPaths = {} |
| 63 | + const fragmentPersistPaths = {} |
| 64 | + |
| 65 | + const query = visit(originalQuery, { |
| 66 | + FragmentSpread: ( |
| 67 | + { name: { value: name } }, |
| 68 | + key, |
| 69 | + parent, |
| 70 | + path, |
| 71 | + ancestors |
| 72 | + ) => { |
| 73 | + const root = ancestors.find( |
| 74 | + ({ kind }) => |
| 75 | + kind === 'OperationDefinition' || kind === 'FragmentDefinition' |
| 76 | + ) |
| 77 | + |
| 78 | + const rootKey = |
| 79 | + root.kind === 'FragmentDefinition' ? root.name.value : '$ROOT' |
| 80 | + |
| 81 | + const fieldPath = ancestors |
| 82 | + .filter(({ kind }) => kind === 'Field') |
| 83 | + .map(({ name: { value: name } }) => name) |
| 84 | + |
| 85 | + fragmentPaths[name] = [rootKey].concat(fieldPath) |
| 86 | + }, |
| 87 | + Directive: ({ name: { value: name } }, key, parent, path, ancestors) => { |
| 88 | + if (name === directive) { |
| 89 | + const fieldPath = ancestors |
| 90 | + .filter(({ kind }) => kind === 'Field') |
| 91 | + .map(({ name: { value: name } }) => name) |
| 92 | + |
| 93 | + const fragmentDefinition = ancestors.find( |
| 94 | + ({ kind }) => kind === 'FragmentDefinition' |
| 95 | + ) |
| 96 | + |
| 97 | + // If we are inside a fragment, we must save the reference. |
| 98 | + if (fragmentDefinition) { |
| 99 | + fragmentPersistPaths[fragmentDefinition.name.value] = fieldPath |
| 100 | + } |
| 101 | + else if (fieldPath.length) { |
| 102 | + paths.push(fieldPath) |
| 103 | + } |
| 104 | + |
| 105 | + return null |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + // In case there are any FragmentDefinition items, we need to combine paths. |
| 111 | + if (Object.keys(fragmentPersistPaths).length) { |
| 112 | + visit(originalQuery, { |
| 113 | + FragmentSpread: ( |
| 114 | + { name: { value: name } }, |
| 115 | + key, |
| 116 | + parent, |
| 117 | + path, |
| 118 | + ancestors |
| 119 | + ) => { |
| 120 | + if (fragmentPersistPaths[name]) { |
| 121 | + let fieldPath = ancestors |
| 122 | + .filter(({ kind }) => kind === 'Field') |
| 123 | + .map(({ name: { value: name } }) => name) |
| 124 | + |
| 125 | + fieldPath = fieldPath.concat(fragmentPersistPaths[name]) |
| 126 | + |
| 127 | + let fragment = name |
| 128 | + let parent = fragmentPaths[fragment][0] |
| 129 | + |
| 130 | + while (parent && parent !== '$ROOT' && fragmentPaths[parent]) { |
| 131 | + fieldPath = fragmentPaths[parent].slice(1).concat(fieldPath) |
| 132 | + parent = fragmentPaths[parent][0] |
| 133 | + } |
| 134 | + |
| 135 | + paths.push(fieldPath) |
| 136 | + } |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + return { query, paths } |
| 142 | +} |
| 143 | + |
| 144 | +const hasPersistDirective = doc => { |
| 145 | + let hasDirective = false |
| 146 | + |
| 147 | + visit(doc, { |
| 148 | + Directive: ({ name: { value: name } }) => { |
| 149 | + if (name === 'persist') { |
| 150 | + hasDirective = true |
| 151 | + return BREAK |
| 152 | + } |
| 153 | + } |
| 154 | + }) |
| 155 | + |
| 156 | + return hasDirective |
| 157 | +} |
| 158 | + |
| 159 | +export { |
| 160 | + addPersistFieldToDocument, |
| 161 | + extractPersistDirectivePaths, |
| 162 | + hasPersistDirective |
| 163 | +} |
0 commit comments