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(graphcache): Fix partial optimistic mutation results (#2740)

authored by

Phil Pluckthun and committed by
GitHub
7570219c 707b688c

+48 -1
+5
.changeset/lovely-monkeys-wink.md
··· 1 + --- 2 + '@urql/exchange-graphcache': patch 3 + --- 4 + 5 + Fix optimistic mutations containing partial results (`undefined` fields), which previously actually caused a hidden cache miss, which may then affect a subsequent non-optimistic mutation result.
+3 -1
exchanges/graphcache/src/operations/write.ts
··· 259 259 // Skip typename fields and assume they've already been written above 260 260 fieldName === '__typename' || 261 261 // Fields marked as deferred that aren't defined must be skipped 262 - (fieldValue === undefined && deferRef.current) 262 + // Otherwise, we also ignore undefined values in optimistic updaters 263 + (fieldValue === undefined && 264 + (deferRef.current || (ctx.optimistic && !isRoot))) 263 265 ) { 264 266 continue; 265 267 }
+40
exchanges/graphcache/src/store/store.test.ts
··· 784 784 }); 785 785 }); 786 786 787 + it('should be able to optimistically mutate with partial data', () => { 788 + const { dependencies } = writeOptimistic( 789 + store, 790 + { 791 + query: gql` 792 + mutation { 793 + addTodo(id: "0", complete: true, __typename: "Todo") { 794 + id 795 + text 796 + complete 797 + __typename 798 + } 799 + } 800 + `, 801 + }, 802 + 1 803 + ); 804 + expect(dependencies).toEqual(new Set(['Todo:0'])); 805 + let { data } = query(store, { query: Todos }); 806 + expect(data).toEqual({ 807 + __typename: 'Query', 808 + todos: [ 809 + { 810 + ...todosData.todos[0], 811 + complete: true, 812 + }, 813 + todosData.todos[1], 814 + todosData.todos[2], 815 + ], 816 + }); 817 + 818 + InMemoryData.noopDataState(store.data, 1); 819 + 820 + ({ data } = query(store, { query: Todos })); 821 + expect(data).toEqual({ 822 + __typename: 'Query', 823 + todos: todosData.todos, 824 + }); 825 + }); 826 + 787 827 describe('Invalidating an entity', () => { 788 828 it('removes an entity from a list.', () => { 789 829 store.invalidate(todosData.todos[1]);