Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly type setQuery and fix now typed callers #4369

Merged
merged 3 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- Resolve "invalidate" -> "invalidated" typo in `QueryManager`. <br/>
[@quazzie](https://github.com/quazzie) in [#4041](https://github.com/apollographql/apollo-client/pull/4041)
- Properly type `setQuery` and fix now typed callers. <br/>
[@danilobuerger](https://github.com/danilobuerger) in [#4369](https://github.com/apollographql/apollo-client/pull/4369)
- Documentation updates. <br/>
[@danilobuerger](https://github.com/danilobuerger) in [#4340](https://github.com/apollographql/apollo-client/pull/4340)

Expand Down
18 changes: 10 additions & 8 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface QueryInfo {
// with them in case of some destabalizing action (e.g. reset of the Apollo store).
observableQuery: ObservableQuery<any> | null;
subscriptions: Subscription[];
cancel?: (() => void);
cancel?: () => void;
}

export class QueryManager<TStore> {
Expand Down Expand Up @@ -246,7 +246,7 @@ export class QueryManager<TStore> {
return Promise.all(
awaitRefetchQueries ? refetchQueryPromises : [],
).then(() => {
this.setQuery(mutationId, () => ({ document: undefined }));
this.setQuery(mutationId, () => ({ document: null }));

if (
errorPolicy === 'ignore' &&
Expand Down Expand Up @@ -292,7 +292,7 @@ export class QueryManager<TStore> {
});
this.broadcastQueries();

this.setQuery(mutationId, () => ({ document: undefined }));
this.setQuery(mutationId, () => ({ document: null }));
reject(
new ApolloError({
networkError: err,
Expand Down Expand Up @@ -757,7 +757,7 @@ export class QueryManager<TStore> {
variables: options.variables,
optimistic: true,
previousResult,
callback: (newData: ApolloQueryResult<any>) => {
callback: newData => {
this.setQuery(queryId, () => ({ invalidated: true, newData }));
},
});
Expand Down Expand Up @@ -932,7 +932,7 @@ export class QueryManager<TStore> {
obs.complete();
}
});
}
},
};

// TODO: Should subscriptions also accept a `context` option to pass
Expand Down Expand Up @@ -1093,7 +1093,7 @@ export class QueryManager<TStore> {
return new Promise<ApolloQueryResult<T>>((resolve, reject) => {
// Need to assign the reject function to the rejectFetchPromise variable
// in the outer scope so that we can refer to it in the .catch handler.
this.fetchQueryRejectFns.add(rejectFetchPromise = reject);
this.fetchQueryRejectFns.add((rejectFetchPromise = reject));

const subscription = execute(this.deduplicator, operation).subscribe({
next: (result: ExecutionResult) => {
Expand Down Expand Up @@ -1188,7 +1188,6 @@ export class QueryManager<TStore> {
this.setQuery(queryId, ({ subscriptions }) => ({
subscriptions: subscriptions.concat([subscription]),
}));

}).catch(error => {
this.fetchQueryRejectFns.delete(rejectFetchPromise);
throw error;
Expand Down Expand Up @@ -1231,7 +1230,10 @@ export class QueryManager<TStore> {
);
}

private setQuery(queryId: string, updater: (prev: QueryInfo) => any) {
private setQuery<T extends keyof QueryInfo>(
queryId: string,
updater: (prev: QueryInfo) => Pick<QueryInfo, T>,
) {
const prev = this.getQuery(queryId);
const newInfo = { ...prev, ...updater(prev) };
this.queries.set(queryId, newInfo);
Expand Down