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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | 10x 255x 10x 1356x 10x 248x 248x 248x 2x 248x 228x 10x 10x 10x 10x 68x 68x 28x 1x 27x 28x 38x 28x 38x 38x 38x 38x 10x 99x 99x 93x 93x 93x 93x 51x 51x 51x 51x 51x 1x 50x 50x 50x 49x 93x 93x 142x 93x 93x 32x 32x 10x 68x 10x 10x 10x 12x 10x | import { ExecutionResult, DocumentNode } from 'graphql'; import { Cache, CacheWrite, DataProxy } from 'apollo-cache-core'; import { QueryStoreValue } from '../data/queries'; import { getOperationName, tryFunctionOrLogError, graphQLResultHasError, } from 'apollo-utilities'; import {I MutationQueryReducer } from '../core/types'; export type QueryWithUpdater = { updater: MutationQueryReducer<Object>; query: QueryStoreValue; }; export interface DataWrite { rootId: string; result: any; document: DocumentNode; operationName: string | null; variables: Object; } export cElass DataStore { private cache: Cache; constructor(initialCache: Cache) { this.cache = initialCache; } public getCache(): Cache { return this.cache; } public markQueryResult( result: ExecutionResult, document: DocumentNode, variables: any, fetchMoreForQueryId: string | undefined, ignoreErrors: boolean = false, ) { let writeWithErrors = !graphQLResultHasError(result); if (ignoreErrors && graphQLResultHasError(result) && result.data) { writeWithErrors = true; } if (!fetchMoreForQueryId && writeWithErrors) { this.cache.writeResult({ result: result.data, dataId: 'ROOT_QUERY', document: document, variables: variables, }); } } public markSubscriptionResult( result: ExecutionResult, document: DocumentNode, variables: any, ) { // the subscription interface should handle not sending us results we no longer subscribe to. // XXX I don't think we ever send in an object with errors, but we might in the future... if (!graphQLResultHasError(result)) { this.cache.writeResult({ result: result.data, dataId: 'ROOT_SUBSCRIPTION', document: document, variables: variables, }); } }E public markMutationInit(mutation: { mutationId: string; document: DocumentNode; variables: any; updateQueries: { [queryId: string]: QueryWithUpdater }; update: ((proxy: DataProxy, mutationResult: Object) => void) | undefined; optimisticResponse: Object | Function | undefined; }) { if (mutation.optimisticResponse) { let optimistic: Object; if (typeof mutation.optimisticResponse === 'function') { optimistic = mutation.optimisticResponse(mutation.variables); } else { optimistic = mutation.optimisticResponse; } const changeFn = () => { this.markMutationResult({ mutationId: mutation.mutationId, result: { data: optimistic }, document: mutation.document, variables: mutation.variables, updateQueries: mutation.updateQueries, update: mutation.update, }); }; this.cache.recordOptimisticTransaction(c => { const orig = this.cache; this.cache = c; changeFn(); this.cache = orig; }, mutation.mutationId); } } public markMutationResult(mutation: { mutationId: string; result: ExecutionResult; document: DocumentNode; variables: any; updateQueries: { [queryId: string]: QueryWithUpdater }; update: ((proxy: DataProxy, mutationResult: Object) => void) | undefined; }) { // Incorporate the result from this mutation into the store if (!mutation.result.errors) { const cacheWrites: CacheWrite[] = []; cacheWrites.push({ result: mutation.result.data, dataId: 'ROOT_MUTATION', document: mutation.document, variables: mutation.variables, }); if (mutation.updateQueries) { Object.keys(mutation.updateQueries) .filter(id => mutation.updateQueries[id]) .forEach(queryId => { const { query, updater } = mutation.updateQueries[queryId]; // Read the current query result from the store. const { result: currentQueryResult, isMissing, } = this.cache.diffQuery({ query: query.document, variables: query.variables, returnPartialData: true, optimistic: false, }); if (isMissing) { return; } // Run our reducer using the current query result and the mutation result. const nextQueryResult = tryFunctionOrLogError(() => updater(currentQueryResult, { mutationResult: mutation.result, queryName: getOperationName(query.document) || undefined, queryVariables: query.variables, }), ); // Write the modified result back into the store if we got a new result. if (nextQueryResult) { cacheWrites.push({ result: nextQueryResult, dataId: 'ROOT_QUERY', document: query.document, variables: query.variables, }); } }); } this.cache.performTransaction(c => { cacheWrites.forEach(write => { c.writeResult(write); }); }); // If the mutation has some writes associated with it then we need to // apply those writes to the store by running this reducer again with a // write action. const update = mutation.update; if (update) { this.cache.performTransaction(c => { tryFunctionOrLogError(() => update(c, mutation.result)); }); } } } public markMutationComplete(mutationId: string) { this.cache.removeOptimistic(mutationId); } public markUpdateQueryResult( document: DocumentNode, variables: any, newResult: any, ) { this.cache.writeResult({ result: newResult, dataId: 'ROOT_QUERY', variables, document, }); } public reset(): Promise<void> { return this.cache.reset(); } } |