Skip to content

Commit

Permalink
Fix type issues due to exposed SSR type
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Jul 6, 2021
1 parent 1bfd974 commit 867f423
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/exchanges/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ it('caches complex GraphQLErrors in query results correctly', () => {
publish(exchange);
next(queryOperation);

const error = ssr.extractData()[queryOperation.key].error;
const error = ssr.extractData()[queryOperation.key]!.error;

expect(error).toHaveProperty('graphQLErrors.0.message', 'Oh no!');
expect(error).toHaveProperty('graphQLErrors.0.path', ['Query']);
Expand Down Expand Up @@ -176,7 +176,7 @@ it('deletes cached results in non-suspense environments', async () => {

await Promise.resolve();

expect(ssr.extractData()[queryOperation.key]).toBe(null);
expect(Object.keys(ssr.extractData()).length).toBe(0);
expect(onPush).toHaveBeenCalledWith(queryResponse);

// NOTE: The operation should not be duplicated
Expand All @@ -202,12 +202,12 @@ it('never allows restoration of invalidated results', async () => {

await Promise.resolve();

expect(ssr.extractData()[queryOperation.key]).toBe(null);
expect(Object.keys(ssr.extractData()).length).toBe(0);
expect(onPush).toHaveBeenCalledTimes(1);
expect(output).not.toHaveBeenCalled();

ssr.restoreData(initialState);
expect(ssr.extractData()[queryOperation.key]).toBe(null);
expect(Object.keys(ssr.extractData()).length).toBe(0);

next(queryOperation);
expect(onPush).toHaveBeenCalledTimes(2);
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/exchanges/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface SerializedResult {
}

export interface SSRData {
[key: string]: SerializedResult | null;
[key: string]: SerializedResult;
}

export interface SSRExchangeParams {
Expand Down Expand Up @@ -91,7 +91,7 @@ const deserializeResult = (

/** The ssrExchange can be created to capture data during SSR and also to rehydrate it on the client */
export const ssrExchange = (params?: SSRExchangeParams): SSRExchange => {
const data: SSRData = {};
const data: Record<string, SerializedResult | null> = {};

// On the client-side, we delete results from the cache as they're resolved
// this is delayed so that concurrent queries don't delete each other's data
Expand Down Expand Up @@ -170,7 +170,11 @@ export const ssrExchange = (params?: SSRExchangeParams): SSRExchange => {
}
};

ssr.extractData = () => Object.assign({}, data);
ssr.extractData = () => {
const result: SSRData = {};
for (const key in data) if (data[key] != null) result[key] = data[key]!;
return result;
};

if (params && params.initialState) {
ssr.restoreData(params.initialState);
Expand Down

0 comments on commit 867f423

Please sign in to comment.