Skip to content

Commit

Permalink
feat(apollo-client): Adds type vars to subscribeToMore for variables
Browse files Browse the repository at this point in the history
  • Loading branch information
timhwang21 committed Apr 6, 2019
1 parent 5a52c23 commit 12f0283
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
72 changes: 72 additions & 0 deletions packages/apollo-client/src/__tests__/subscribeToMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubscriptionData, TypedSubscriptionVariables>({
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]);
}
});
});
10 changes: 5 additions & 5 deletions packages/apollo-client/src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,24 @@ export interface FetchMoreQueryOptions<TVariables, K extends keyof TVariables> {

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<TData, TVariables, TSubscriptionData>;
variables?: TSubscriptionVariables;
updateQuery?: UpdateQueryFn<TData, TSubscriptionVariables, TSubscriptionData>;
onError?: (error: Error) => void;
};

Expand Down

0 comments on commit 12f0283

Please sign in to comment.