Skip to content

Commit

Permalink
Fix updateQuery type parameters in subscribeToMore.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Apr 8, 2019
1 parent bac653b commit 5c0760b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
37 changes: 20 additions & 17 deletions packages/apollo-client/src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
FetchMoreQueryOptions,
SubscribeToMoreOptions,
ErrorPolicy,
UpdateQueryFn,
} from './watchQueryOptions';

import { QueryStoreValue } from '../data/queries';
Expand Down Expand Up @@ -391,9 +390,13 @@ export class ObservableQuery<
// and you can only do it by stopping the subscription and then subscribing again with new variables.
public subscribeToMore<
TSubscriptionData = TData,
TSubscriptionVariables = TVariables,
TSubscriptionVariables = TVariables
>(
options: SubscribeToMoreOptions<TData, TSubscriptionVariables, TSubscriptionData>,
options: SubscribeToMoreOptions<
TData,
TSubscriptionVariables,
TSubscriptionData
>,
) {
const subscription = this.queryManager
.startGraphQLSubscription({
Expand All @@ -402,16 +405,14 @@ export class ObservableQuery<
})
.subscribe({
next: (subscriptionData: { data: TSubscriptionData }) => {
if (options.updateQuery) {
this.updateQuery((previous, { variables }) =>
(options.updateQuery as UpdateQueryFn<
TData,
TVariables,
TSubscriptionData
>)(previous, {
subscriptionData,
variables,
}),
const { updateQuery } = options;
if (updateQuery) {
this.updateQuery<TSubscriptionVariables>(
(previous, { variables }) =>
updateQuery(previous, {
subscriptionData,
variables,
}),
);
}
},
Expand Down Expand Up @@ -532,20 +533,22 @@ export class ObservableQuery<
}
}

public updateQuery(
public updateQuery<TVars = TVariables>(
mapFn: (
previousQueryResult: TData,
options: UpdateQueryOptions<TVariables>,
options: UpdateQueryOptions<TVars>,
) => TData,
): void {
const {
previousResult,
variables,
document,
} = this.queryManager.getQueryWithPreviousResult(this.queryId);
} = this.queryManager.getQueryWithPreviousResult<TData, TVars>(
this.queryId,
);

const newResult = tryFunctionOrLogError(() =>
mapFn(previousResult, { variables: variables as TVariables }),
mapFn(previousResult, { variables }),
);

if (newResult) {
Expand Down
8 changes: 4 additions & 4 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,14 +1161,14 @@ export class QueryManager<TStore> {
}
}

public getQueryWithPreviousResult<T>(
queryIdOrObservable: string | ObservableQuery<T>,
public getQueryWithPreviousResult<TData, TVariables = OperationVariables>(
queryIdOrObservable: string | ObservableQuery<TData, TVariables>,
): {
previousResult: any;
variables: OperationVariables | undefined;
variables: TVariables | undefined;
document: DocumentNode;
} {
let observableQuery: ObservableQuery<T>;
let observableQuery: ObservableQuery<TData, any>;
if (typeof queryIdOrObservable === 'string') {
const { observableQuery: foundObserveableQuery } = this.getQuery(
queryIdOrObservable,
Expand Down

0 comments on commit 5c0760b

Please sign in to comment.