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 Mar 20, 2019
1 parent 1e5497c commit d20df71
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
32 changes: 19 additions & 13 deletions packages/apollo-client/src/__tests__/subscribeToMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,18 @@ describe('subscribeToMore', () => {
});
// 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 => {
it('allows specification of custom types for variables and payload (#4246)', done => {
interface TypedOperation extends Operation {
variables: {
someNumber: number
}
someNumber: number;
};
}
const typedReq = { request: { query, variables: { someNumber: 1 } } as TypedOperation, result };
const typedReq = {
request: { query, variables: { someNumber: 1 } } as TypedOperation,
result,
};
interface TypedSubscriptionVariables {
someString: string
someString: string;
}

let latestResult: any = null;
Expand All @@ -358,18 +361,21 @@ describe('subscribeToMore', () => {

const client = new ApolloClient({
cache: new InMemoryCache({ addTypename: false }),
link
link,
});

const obsHandle = client.watchQuery<typeof typedReq["result"]["data"], typeof typedReq["request"]["variables"]>({
query
const obsHandle = client.watchQuery<
typeof typedReq['result']['data'],
typeof typedReq['request']['variables']
>({
query,
});

const sub = obsHandle.subscribe({
next(queryResult) {
latestResult = queryResult;
counter++;
}
},
});

obsHandle.subscribeToMore<SubscriptionData, TypedSubscriptionVariables>({
Expand All @@ -379,21 +385,21 @@ describe('subscribeToMore', () => {
}
`,
variables: {
someString: 'foo'
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" } },
data: { entry: { value: 'Amanda Liu' } },
loading: false,
networkStatus: 7,
stale: false
stale: false,
});
done();
}, 15);
Expand Down
20 changes: 15 additions & 5 deletions packages/apollo-client/src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ export class ObservableQuery<
if (!partial) {
this.lastResult = { ...result, stale: false };
this.lastResultSnapshot = this.queryManager.assumeImmutableResults
? this.lastResult : cloneDeep(this.lastResult);
? this.lastResult
: cloneDeep(this.lastResult);
}

return { ...result, partial };
Expand Down Expand Up @@ -339,7 +340,8 @@ export class ObservableQuery<
FetchMoreOptions<TData, TVariables>,
): Promise<ApolloQueryResult<TData>> {
// early return if no update Query
invariant(fetchMoreOptions.updateQuery,
invariant(
fetchMoreOptions.updateQuery,
'updateQuery option is required. This function defines how to update the query data with the new results.',
);

Expand Down Expand Up @@ -389,8 +391,15 @@ export class ObservableQuery<
// XXX the subscription variables are separate from the query variables.
// if you want to update subscription variables, right now you have to do that separately,
// and you can only do it by stopping the subscription and then subscribing again with new variables.
public subscribeToMore<TSubscriptionData = TData, TSubscriptionVariables = TVariables>(
options: SubscribeToMoreOptions<TData, TSubscriptionVariables, TSubscriptionData>,
public subscribeToMore<
TSubscriptionData = TData,
TSubscriptionVariables = TVariables
>(
options: SubscribeToMoreOptions<
TData,
TSubscriptionVariables,
TSubscriptionData
>,
) {
const subscription = this.queryManager
.startGraphQLSubscription({
Expand Down Expand Up @@ -613,7 +622,8 @@ export class ObservableQuery<
next: (result: ApolloQueryResult<TData>) => {
this.lastResult = result;
this.lastResultSnapshot = this.queryManager.assumeImmutableResults
? result : cloneDeep(result);
? result
: cloneDeep(result);
this.observers.forEach(obs => obs.next && obs.next(result));
},
error: (error: ApolloError) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-client/src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export type UpdateQueryFn<
options: {
subscriptionData: { data: TSubscriptionData };
variables?: TSubscriptionVariables;
}
},
) => TData;

export type SubscribeToMoreOptions<
Expand Down

0 comments on commit d20df71

Please sign in to comment.