Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Add typename formatting to SSR Exchange #3288

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/honest-hairs-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

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.
19 changes: 12 additions & 7 deletions packages/core/src/exchanges/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ type OperationCache = Map<string, Set<number>>;
const shouldSkip = ({ kind }: Operation) =>
kind !== 'mutation' && kind !== 'query';

/** Adds unique typenames to query (for invalidating cache entries) */
export const mapTypeNames = (operation: Operation): Operation => {
const query = formatDocument(operation.query);
if (query !== operation.query) {
const formattedOperation = makeOperation(operation.kind, operation);
formattedOperation.query = query;
return formattedOperation;
} else {
return operation;
}
};

/** Default document cache exchange.
*
* @remarks
Expand All @@ -41,13 +53,6 @@ export const cacheExchange: Exchange = ({ forward, client, dispatchDebug }) => {
const resultCache: ResultCache = new Map();
const operationCache: OperationCache = new Map();

// Adds unique typenames to query (for invalidating cache entries)
const mapTypeNames = (operation: Operation): Operation => {
const formattedOperation = makeOperation(operation.kind, operation);
formattedOperation.query = formatDocument(operation.query);
return formattedOperation;
};

const isOperationCached = (operation: Operation) =>
operation.kind === 'query' &&
operation.context.requestPolicy !== 'network-only' &&
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/exchanges/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { vi, expect, it, beforeEach, afterEach } from 'vitest';
import { Client } from '../client';
import { queryOperation, queryResponse } from '../test-utils';
import { ExchangeIO, Operation, OperationResult } from '../types';
import { CombinedError } from '../utils';
import { CombinedError, formatDocument } from '../utils';
import { ssrExchange } from './ssr';

let forward: ExchangeIO;
Expand Down Expand Up @@ -254,6 +254,10 @@ it('resolves deferred, cached query results correctly', () => {
},
},
});

expect(output.mock.calls[0][0].query).toBe(
formatDocument(queryOperation.query)
);
});

it('deletes cached results in non-suspense environments', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/exchanges/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { GraphQLError } from '../utils/graphql';
import { pipe, filter, merge, map, tap } from 'wonka';
import { Exchange, OperationResult, Operation } from '../types';
import { addMetadata, CombinedError } from '../utils';
import { reexecuteOperation } from './cache';
import { reexecuteOperation, mapTypeNames } from './cache';

/** A serialized version of an {@link OperationResult}.
*
Expand Down Expand Up @@ -226,6 +226,7 @@ export const ssrExchange = (params: SSRExchangeParams = {}): SSRExchange => {
!!data[operation.key]!.hasNext ||
operation.context.requestPolicy === 'network-only'
),
map(mapTypeNames),
forward
);

Expand Down