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.

(core) - Fix maskTypename not handling nested arrays (#2074)

* fix arrays in maskTypename

* fix typing issue

* fix issue with nested data

authored by

Jovi De Croock and committed by
GitHub
f85b47ef 5e11f936

+38 -16
+5
.changeset/metal-monkeys-knock.md
··· 1 + --- 2 + '@urql/core': patch 3 + --- 4 + 5 + Fix issue where `maskTypename` would ignore array shapes
+14
packages/core/src/utils/maskTypename.test.ts
··· 25 25 ).toEqual({ id: 1, author: { id: 2 } }); 26 26 }); 27 27 28 + it('works with nested arrays', () => { 29 + expect( 30 + maskTypename({ 31 + __typename: 'Todo', 32 + id: 1, 33 + nodes: [[4, 5]], 34 + author: { 35 + id: 2, 36 + __typename: 'Author', 37 + }, 38 + }) 39 + ).toEqual({ id: 1, nodes: [[4, 5]], author: { id: 2 } }); 40 + }); 41 + 28 42 it('strips typename from nested objects with arrays', () => { 29 43 expect( 30 44 maskTypename({
+19 -16
packages/core/src/utils/maskTypename.ts
··· 1 1 export const maskTypename = (data: any): any => { 2 2 if (!data || typeof data !== 'object') return data; 3 3 4 - return Object.keys(data).reduce((acc, key: string) => { 5 - const value = data[key]; 6 - if (key === '__typename') { 7 - Object.defineProperty(acc, '__typename', { 8 - enumerable: false, 9 - value, 10 - }); 11 - } else if (Array.isArray(value)) { 12 - acc[key] = value.map(maskTypename); 13 - } else if (value && typeof value === 'object' && '__typename' in value) { 14 - acc[key] = maskTypename(value); 15 - } else { 16 - acc[key] = value; 17 - } 4 + return Object.keys(data).reduce( 5 + (acc, key: string) => { 6 + const value = data[key]; 7 + if (key === '__typename') { 8 + Object.defineProperty(acc, '__typename', { 9 + enumerable: false, 10 + value, 11 + }); 12 + } else if (Array.isArray(value)) { 13 + acc[key] = value.map(maskTypename); 14 + } else if (value && typeof value === 'object' && '__typename' in value) { 15 + acc[key] = maskTypename(value); 16 + } else { 17 + acc[key] = value; 18 + } 18 19 19 - return acc; 20 - }, {}); 20 + return acc; 21 + }, 22 + Array.isArray(data) ? [] : {} 23 + ); 21 24 };