From 12f0283ca70fda9a701bf0910623d1f96468280e Mon Sep 17 00:00:00 2001 From: timhwang21 Date: Wed, 20 Mar 2019 15:58:15 -0400 Subject: [PATCH] feat(apollo-client): Adds type vars to subscribeToMore for variables Addresses #4246. --- .../src/__tests__/subscribeToMore.ts | 72 +++++++++++++++++++ .../src/core/watchQueryOptions.ts | 10 +-- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/packages/apollo-client/src/__tests__/subscribeToMore.ts b/packages/apollo-client/src/__tests__/subscribeToMore.ts index 9778db7ed92..75558cd386d 100644 --- a/packages/apollo-client/src/__tests__/subscribeToMore.ts +++ b/packages/apollo-client/src/__tests__/subscribeToMore.ts @@ -337,4 +337,76 @@ describe('subscribeToMore', () => { done(); }); // TODO add a test that checks that subscriptions are cancelled when obs is unsubscribed from. + + it('allows specification of custom types for variables and payload (#4246)', done => { + interface TypedOperation extends Operation { + variables: { + someNumber: number; + }; + } + const typedReq = { + request: { query, variables: { someNumber: 1 } } as TypedOperation, + result, + }; + interface TypedSubscriptionVariables { + someString: string; + } + + let latestResult: any = null; + const wSLink = mockObservableLink(); + const httpLink = mockSingleLink(typedReq); + + const link = ApolloLink.split(isSub, wSLink, httpLink); + let counter = 0; + + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); + + const obsHandle = client.watchQuery< + typeof typedReq['result']['data'], + typeof typedReq['request']['variables'] + >({ + query, + variables: { someNumber: 1 }, + }); + + const sub = obsHandle.subscribe({ + next(queryResult) { + latestResult = queryResult; + counter++; + }, + }); + + obsHandle.subscribeToMore({ + document: gql` + subscription newValues { + name + } + `, + variables: { + someString: 'foo', + }, + updateQuery: (_, { subscriptionData }) => { + return { entry: { value: subscriptionData.data.name } }; + }, + }); + + setTimeout(() => { + sub.unsubscribe(); + expect(counter).toBe(3); + expect(stripSymbols(latestResult)).toEqual({ + data: { entry: { value: 'Amanda Liu' } }, + loading: false, + networkStatus: 7, + stale: false, + }); + done(); + }, 15); + + for (let i = 0; i < 2; i++) { + wSLink.simulateResult(results[i]); + } + }); }); diff --git a/packages/apollo-client/src/core/watchQueryOptions.ts b/packages/apollo-client/src/core/watchQueryOptions.ts index 7a155fca653..6b7a945d3dc 100644 --- a/packages/apollo-client/src/core/watchQueryOptions.ts +++ b/packages/apollo-client/src/core/watchQueryOptions.ts @@ -113,24 +113,24 @@ export interface FetchMoreQueryOptions { export type UpdateQueryFn< TData = any, - TVariables = OperationVariables, + TSubscriptionVariables = OperationVariables, TSubscriptionData = TData > = ( previousQueryResult: TData, options: { subscriptionData: { data: TSubscriptionData }; - variables?: TVariables; + variables?: TSubscriptionVariables; }, ) => TData; export type SubscribeToMoreOptions< TData = any, - TVariables = OperationVariables, + TSubscriptionVariables = OperationVariables, TSubscriptionData = TData > = { document: DocumentNode; - variables?: TVariables; - updateQuery?: UpdateQueryFn; + variables?: TSubscriptionVariables; + updateQuery?: UpdateQueryFn; onError?: (error: Error) => void; };