Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(persisted): Add missing teardown source (#3312)

authored by

Phil Pluckthun and committed by
GitHub
1cf2e2ff caa029e0

+47 -29
+5
.changeset/five-penguins-unite.md
··· 1 + --- 2 + '@urql/exchange-persisted': patch 3 + --- 4 + 5 + Fix `persistedExchange` ignoring teardowns in its initial operation mapping. Since the hash function is promisified, which defers any persisted operation, it needs to respect teardowns.
+42 -29
exchanges/persisted/src/persistedExchange.ts
··· 5 5 filter, 6 6 merge, 7 7 mergeMap, 8 + takeUntil, 8 9 pipe, 9 10 } from 'wonka'; 10 11 ··· 135 136 ((enableForMutation && operation.kind === 'mutation') || 136 137 operation.kind === 'query'); 137 138 139 + const getPersistedOperation = async (operation: Operation) => { 140 + const persistedOperation = makeOperation(operation.kind, operation, { 141 + ...operation.context, 142 + persistAttempt: true, 143 + }); 144 + 145 + const sha256Hash = await hashFn( 146 + stringifyDocument(operation.query), 147 + operation.query 148 + ); 149 + if (sha256Hash) { 150 + persistedOperation.extensions = { 151 + ...persistedOperation.extensions, 152 + persistedQuery: { 153 + version: 1, 154 + sha256Hash, 155 + }, 156 + }; 157 + if ( 158 + persistedOperation.kind === 'query' && 159 + preferGetForPersistedQueries 160 + ) { 161 + persistedOperation.context.preferGetMethod = 162 + preferGetForPersistedQueries; 163 + } 164 + } 165 + 166 + return persistedOperation; 167 + }; 168 + 138 169 return operations$ => { 139 170 const retries = makeSubject<Operation>(); 140 171 ··· 146 177 const persistedOps$ = pipe( 147 178 operations$, 148 179 filter(operationFilter), 149 - map(async operation => { 150 - const persistedOperation = makeOperation(operation.kind, operation, { 151 - ...operation.context, 152 - persistAttempt: true, 153 - }); 154 - 155 - const sha256Hash = await hashFn( 156 - stringifyDocument(operation.query), 157 - operation.query 180 + mergeMap(operation => { 181 + const persistedOperation$ = getPersistedOperation(operation); 182 + return pipe( 183 + fromPromise(persistedOperation$), 184 + takeUntil( 185 + pipe( 186 + operations$, 187 + filter(op => op.kind === 'teardown' && op.key === operation.key) 188 + ) 189 + ) 158 190 ); 159 - if (sha256Hash) { 160 - persistedOperation.extensions = { 161 - ...persistedOperation.extensions, 162 - persistedQuery: { 163 - version: 1, 164 - sha256Hash, 165 - }, 166 - }; 167 - if ( 168 - persistedOperation.kind === 'query' && 169 - preferGetForPersistedQueries 170 - ) { 171 - persistedOperation.context.preferGetMethod = 172 - preferGetForPersistedQueries; 173 - } 174 - } 175 - 176 - return persistedOperation; 177 - }), 178 - mergeMap(fromPromise) 191 + }) 179 192 ); 180 193 181 194 return pipe(