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 error serializer/rehydrator in ssrExchange (#515)

Fix #508

In all error cases the ssrExchange would be generated
while assuming that networkError may never be undefined.
This could cause an edge case where networkError was
set to Error('undefined') on the client-side.

authored by

Phil Plückthun and committed by
Jovi De Croock
410cc39a 011e222a

+44 -9
+31
src/exchanges/ssr.test.ts
··· 3 3 import { Client } from '../client'; 4 4 import { queryOperation, queryResponse } from '../test-utils'; 5 5 import { ExchangeIO, Operation } from '../types'; 6 + import { CombinedError } from '../utils'; 6 7 import { ssrExchange } from './ssr'; 7 8 8 9 let forward: ExchangeIO; ··· 36 37 [queryOperation.key]: { 37 38 data: queryResponse.data, 38 39 error: undefined, 40 + }, 41 + }); 42 + }); 43 + 44 + it('caches errored query results correctly', () => { 45 + output.mockReturnValueOnce({ 46 + ...queryResponse, 47 + data: null, 48 + error: new CombinedError({ 49 + graphQLErrors: ['Oh no!'], 50 + }), 51 + }); 52 + 53 + const ssr = ssrExchange(); 54 + const { source: ops$, next } = input; 55 + const exchange = ssr(exchangeInput)(ops$); 56 + 57 + publish(exchange); 58 + next(queryOperation); 59 + 60 + const data = ssr.extractData(); 61 + expect(Object.keys(data)).toEqual(['' + queryOperation.key]); 62 + 63 + expect(data).toEqual({ 64 + [queryOperation.key]: { 65 + data: null, 66 + error: { 67 + graphQLErrors: ['Oh no!'], 68 + networkError: undefined, 69 + }, 39 70 }, 40 71 }); 41 72 });
+13 -9
src/exchanges/ssr.ts
··· 37 37 const result: SerializedResult = { data, error: undefined }; 38 38 if (error !== undefined) { 39 39 result.error = { 40 - networkError: '' + error.networkError, 41 - graphQLErrors: error.graphQLErrors.map(x => '' + x), 40 + graphQLErrors: error.graphQLErrors.map(x => x.message), 41 + networkError: error.networkError ? '' + error.networkError : undefined, 42 42 }; 43 43 } 44 44 ··· 55 55 operation, 56 56 data, 57 57 extensions: undefined, 58 - error: undefined, 58 + error: error 59 + ? new CombinedError({ 60 + networkError: error.networkError 61 + ? new Error(error.networkError) 62 + : undefined, 63 + graphQLErrors: 64 + error.graphQLErrors && error.graphQLErrors.length 65 + ? error.graphQLErrors 66 + : undefined, 67 + }) 68 + : undefined, 59 69 }; 60 - if (error !== undefined) { 61 - deserialized.error = new CombinedError({ 62 - networkError: new Error(error.networkError), 63 - graphQLErrors: error.graphQLErrors, 64 - }); 65 - } 66 70 67 71 return deserialized; 68 72 };