|
| 1 | +// this file is based on https://github.com/apollographql/apollo-cache-persist/blob/master/examples/react-native/src/utils/persistence/persistLink.ts |
| 2 | +// with some heavy refactororing |
| 3 | + |
| 4 | +/* eslint-disable consistent-return */ |
| 5 | +/* eslint-disable @gitlab/require-i18n-strings */ |
| 6 | +/* eslint-disable no-param-reassign */ |
| 7 | +import { visit } from 'graphql'; |
| 8 | +import { ApolloLink } from '@apollo/client/core'; |
| 9 | +import traverse from 'traverse'; |
| 10 | + |
| 11 | +const extractPersistDirectivePaths = (originalQuery, directive = 'persist') => { |
| 12 | + const paths = []; |
| 13 | + const fragmentPaths = {}; |
| 14 | + const fragmentPersistPaths = {}; |
| 15 | + |
| 16 | + const query = visit(originalQuery, { |
| 17 | + FragmentSpread: ({ name: { value: name } }, _key, _parent, _path, ancestors) => { |
| 18 | + const root = ancestors.find( |
| 19 | + ({ kind }) => kind === 'OperationDefinition' || kind === 'FragmentDefinition', |
| 20 | + ); |
| 21 | + |
| 22 | + const rootKey = root.kind === 'FragmentDefinition' ? root.name.value : '$ROOT'; |
| 23 | + |
| 24 | + const fieldPath = ancestors |
| 25 | + .filter(({ kind }) => kind === 'Field') |
| 26 | + .map(({ name: { value } }) => value); |
| 27 | + |
| 28 | + fragmentPaths[name] = [rootKey].concat(fieldPath); |
| 29 | + }, |
| 30 | + Directive: ({ name: { value: name } }, _key, _parent, _path, ancestors) => { |
| 31 | + if (name === directive) { |
| 32 | + const fieldPath = ancestors |
| 33 | + .filter(({ kind }) => kind === 'Field') |
| 34 | + .map(({ name: { value } }) => value); |
| 35 | + |
| 36 | + const fragmentDefinition = ancestors.find(({ kind }) => kind === 'FragmentDefinition'); |
| 37 | + |
| 38 | + // If we are inside a fragment, we must save the reference. |
| 39 | + if (fragmentDefinition) { |
| 40 | + fragmentPersistPaths[fragmentDefinition.name.value] = fieldPath; |
| 41 | + } else if (fieldPath.length) { |
| 42 | + paths.push(fieldPath); |
| 43 | + } |
| 44 | + return null; |
| 45 | + } |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + // In case there are any FragmentDefinition items, we need to combine paths. |
| 50 | + if (Object.keys(fragmentPersistPaths).length) { |
| 51 | + visit(originalQuery, { |
| 52 | + FragmentSpread: ({ name: { value: name } }, _key, _parent, _path, ancestors) => { |
| 53 | + if (fragmentPersistPaths[name]) { |
| 54 | + let fieldPath = ancestors |
| 55 | + .filter(({ kind }) => kind === 'Field') |
| 56 | + .map(({ name: { value } }) => value); |
| 57 | + |
| 58 | + fieldPath = fieldPath.concat(fragmentPersistPaths[name]); |
| 59 | + |
| 60 | + const fragment = name; |
| 61 | + let parent = fragmentPaths[fragment][0]; |
| 62 | + |
| 63 | + while (parent && parent !== '$ROOT' && fragmentPaths[parent]) { |
| 64 | + fieldPath = fragmentPaths[parent].slice(1).concat(fieldPath); |
| 65 | + // eslint-disable-next-line prefer-destructuring |
| 66 | + parent = fragmentPaths[parent][0]; |
| 67 | + } |
| 68 | + |
| 69 | + paths.push(fieldPath); |
| 70 | + } |
| 71 | + }, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + return { query, paths }; |
| 76 | +}; |
| 77 | + |
| 78 | +/** |
| 79 | + * Given a data result object path, return the equivalent query selection path. |
| 80 | + * |
| 81 | + * @param {Array} path The data result object path. i.e.: ["a", 0, "b"] |
| 82 | + * @return {String} the query selection path. i.e.: "a.b" |
| 83 | + */ |
| 84 | +const toQueryPath = (path) => path.filter((key) => Number.isNaN(Number(key))).join('.'); |
| 85 | + |
| 86 | +const attachPersists = (paths, object) => { |
| 87 | + const queryPaths = paths.map(toQueryPath); |
| 88 | + function mapperFunction() { |
| 89 | + if ( |
| 90 | + !this.isRoot && |
| 91 | + this.node && |
| 92 | + typeof this.node === 'object' && |
| 93 | + Object.keys(this.node).length && |
| 94 | + !Array.isArray(this.node) |
| 95 | + ) { |
| 96 | + const path = toQueryPath(this.path); |
| 97 | + |
| 98 | + this.update({ |
| 99 | + __persist: Boolean( |
| 100 | + queryPaths.find( |
| 101 | + (queryPath) => queryPath.indexOf(path) === 0 || path.indexOf(queryPath) === 0, |
| 102 | + ), |
| 103 | + ), |
| 104 | + ...this.node, |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return traverse(object).map(mapperFunction); |
| 110 | +}; |
| 111 | + |
| 112 | +export const getPersistLink = () => { |
| 113 | + return new ApolloLink((operation, forward) => { |
| 114 | + const { query, paths } = extractPersistDirectivePaths(operation.query); |
| 115 | + |
| 116 | + // Noop if not a persist query |
| 117 | + if (!paths.length) { |
| 118 | + return forward(operation); |
| 119 | + } |
| 120 | + |
| 121 | + // Replace query with one without @persist directives. |
| 122 | + operation.query = query; |
| 123 | + |
| 124 | + // Remove requesting __persist fields. |
| 125 | + operation.query = visit(operation.query, { |
| 126 | + Field: ({ name: { value: name } }) => { |
| 127 | + if (name === '__persist') { |
| 128 | + return null; |
| 129 | + } |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + return forward(operation).map((result) => { |
| 134 | + if (result.data) { |
| 135 | + result.data = attachPersists(paths, result.data); |
| 136 | + } |
| 137 | + |
| 138 | + return result; |
| 139 | + }); |
| 140 | + }); |
| 141 | +}; |
0 commit comments