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(core): Add typename formatting to SSR Exchange (#3288)

authored by

Phil Pluckthun and committed by
GitHub
c074a504 32e3e53d

+24 -9
+5
.changeset/honest-hairs-wave.md
··· 1 + --- 2 + '@urql/core': patch 3 + --- 4 + 5 + Fix `ssrExchange` not formatting query documents using `formatDocument`. Without this call we'd run the risk of not having `__typename` available on the client-side when rehydrating.
+12 -7
packages/core/src/exchanges/cache.ts
··· 17 17 const shouldSkip = ({ kind }: Operation) => 18 18 kind !== 'mutation' && kind !== 'query'; 19 19 20 + /** Adds unique typenames to query (for invalidating cache entries) */ 21 + export const mapTypeNames = (operation: Operation): Operation => { 22 + const query = formatDocument(operation.query); 23 + if (query !== operation.query) { 24 + const formattedOperation = makeOperation(operation.kind, operation); 25 + formattedOperation.query = query; 26 + return formattedOperation; 27 + } else { 28 + return operation; 29 + } 30 + }; 31 + 20 32 /** Default document cache exchange. 21 33 * 22 34 * @remarks ··· 40 52 export const cacheExchange: Exchange = ({ forward, client, dispatchDebug }) => { 41 53 const resultCache: ResultCache = new Map(); 42 54 const operationCache: OperationCache = new Map(); 43 - 44 - // Adds unique typenames to query (for invalidating cache entries) 45 - const mapTypeNames = (operation: Operation): Operation => { 46 - const formattedOperation = makeOperation(operation.kind, operation); 47 - formattedOperation.query = formatDocument(operation.query); 48 - return formattedOperation; 49 - }; 50 55 51 56 const isOperationCached = (operation: Operation) => 52 57 operation.kind === 'query' &&
+5 -1
packages/core/src/exchanges/ssr.test.ts
··· 4 4 import { Client } from '../client'; 5 5 import { queryOperation, queryResponse } from '../test-utils'; 6 6 import { ExchangeIO, Operation, OperationResult } from '../types'; 7 - import { CombinedError } from '../utils'; 7 + import { CombinedError, formatDocument } from '../utils'; 8 8 import { ssrExchange } from './ssr'; 9 9 10 10 let forward: ExchangeIO; ··· 254 254 }, 255 255 }, 256 256 }); 257 + 258 + expect(output.mock.calls[0][0].query).toBe( 259 + formatDocument(queryOperation.query) 260 + ); 257 261 }); 258 262 259 263 it('deletes cached results in non-suspense environments', async () => {
+2 -1
packages/core/src/exchanges/ssr.ts
··· 2 2 import { pipe, filter, merge, map, tap } from 'wonka'; 3 3 import { Exchange, OperationResult, Operation } from '../types'; 4 4 import { addMetadata, CombinedError } from '../utils'; 5 - import { reexecuteOperation } from './cache'; 5 + import { reexecuteOperation, mapTypeNames } from './cache'; 6 6 7 7 /** A serialized version of an {@link OperationResult}. 8 8 * ··· 226 226 !!data[operation.key]!.hasNext || 227 227 operation.context.requestPolicy === 'network-only' 228 228 ), 229 + map(mapTypeNames), 229 230 forward 230 231 ); 231 232