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(core): Fix early closing of subscriptions (#3137)

authored by

Phil Pluckthun and committed by
GitHub
83d5853b 21ccbad4

+46 -4
+5
.changeset/nice-flowers-think.md
··· 1 + --- 2 + '@urql/core': patch 3 + --- 4 + 5 + Fix `hasNext` being defaulted to `false` when a new subscription event is received on the `subscriptionExchange` that doesn't have `hasNext` set.
+39 -3
packages/core/src/utils/result.test.ts
··· 1 1 import { describe, it, expect } from 'vitest'; 2 2 import { OperationResult } from '../types'; 3 - import { queryOperation } from '../test-utils'; 3 + import { queryOperation, subscriptionOperation } from '../test-utils'; 4 4 import { makeResult, mergeResultPatch } from './result'; 5 5 6 6 describe('makeResult', () => { 7 7 it('adds extensions and errors correctly', () => { 8 - const response = {}; 9 8 const origResult = { 10 9 data: undefined, 11 10 errors: ['error message'], ··· 14 13 }, 15 14 }; 16 15 17 - const result = makeResult(queryOperation, origResult, response); 16 + const result = makeResult(queryOperation, origResult); 18 17 18 + expect(result.hasNext).toBe(false); 19 19 expect(result.operation).toBe(queryOperation); 20 20 expect(result.data).toBe(undefined); 21 21 expect(result.extensions).toEqual(origResult.extensions); ··· 23 23 `[CombinedError: [GraphQL] error message]` 24 24 ); 25 25 }); 26 + 27 + it('default hasNext to true for subscriptions', () => { 28 + const origResult = { 29 + data: undefined, 30 + errors: ['error message'], 31 + extensions: { 32 + extensionKey: 'extensionValue', 33 + }, 34 + }; 35 + 36 + const result = makeResult(subscriptionOperation, origResult); 37 + expect(result.hasNext).toBe(true); 38 + }); 26 39 }); 27 40 28 41 describe('mergeResultPatch', () => { 42 + it('should default hasNext to true if the last result was set to true', () => { 43 + const prevResult: OperationResult = { 44 + operation: subscriptionOperation, 45 + data: { 46 + __typename: 'Subscription', 47 + event: 1, 48 + }, 49 + stale: false, 50 + hasNext: true, 51 + }; 52 + 53 + const merged = mergeResultPatch(prevResult, { 54 + data: { 55 + __typename: 'Subscription', 56 + event: 2, 57 + }, 58 + }); 59 + 60 + expect(merged.data).not.toBe(prevResult.data); 61 + expect(merged.data.event).toBe(2); 62 + expect(merged.hasNext).toBe(true); 63 + }); 64 + 29 65 it('should ignore invalid patches', () => { 30 66 const prevResult: OperationResult = { 31 67 operation: queryOperation,
+2 -1
packages/core/src/utils/result.ts
··· 141 141 ? new CombinedError({ graphQLErrors: errors, response }) 142 142 : undefined, 143 143 extensions: hasExtensions ? extensions : undefined, 144 - hasNext: !!nextResult.hasNext, 144 + hasNext: 145 + nextResult.hasNext != null ? nextResult.hasNext : prevResult.hasNext, 145 146 stale: false, 146 147 }; 147 148 };