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

Corrected ApolloClient.queryManager typing as it may be undefined #3661

Merged
merged 5 commits into from
Aug 2, 2018
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
4 changes: 3 additions & 1 deletion packages/apollo-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ the top level [`CHANGELOG.md`](https://github.com/apollographql/apollo-client/bl

### vNext

- Updated `graphql` `peerDependencies` to handle 14.x versions.
- Corrected `ApolloClient.queryManager` typing as it may be undefined.
[PR #3661](https://github.com/apollographql/apollo-client/pull/3661)
- Updated `graphql` `peerDependencies` to handle 14.x versions.
[PR #3598](https://github.com/apollographql/apollo-client/pull/3598)
- Document `setVariables` internal API status.
[PR #3692](https://github.com/apollographql/apollo-client/pull/3692)
Expand Down
71 changes: 34 additions & 37 deletions packages/apollo-client/src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
public link: ApolloLink;
public store: DataStore<TCacheShape>;
public cache: ApolloCache<TCacheShape>;
public queryManager: QueryManager<TCacheShape>;
public queryManager: QueryManager<TCacheShape> | undefined;
public disableNetworkFetches: boolean;
public version: string;
public queryDeduplication: boolean;
Expand Down Expand Up @@ -207,8 +207,6 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
public watchQuery<T, TVariables = OperationVariables>(
options: WatchQueryOptions<TVariables>,
): ObservableQuery<T> {
this.initQueryManager();

if (this.defaultOptions.watchQuery) {
options = {
...this.defaultOptions.watchQuery,
Expand All @@ -225,7 +223,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
options = { ...options, fetchPolicy: 'cache-first' };
}

return this.queryManager.watchQuery<T>(options);
return this.initQueryManager().watchQuery<T>(options);
}

/**
Expand All @@ -240,8 +238,6 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
public query<T, TVariables = OperationVariables>(
options: QueryOptions<TVariables>,
): Promise<ApolloQueryResult<T>> {
this.initQueryManager();

if (this.defaultOptions.query) {
options = { ...this.defaultOptions.query, ...options } as QueryOptions<
TVariables
Expand All @@ -260,7 +256,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
options = { ...options, fetchPolicy: 'cache-first' };
}

return this.queryManager.query<T>(options);
return this.initQueryManager().query<T>(options);
}

/**
Expand All @@ -273,16 +269,14 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
public mutate<T, TVariables = OperationVariables>(
options: MutationOptions<T, TVariables>,
): Promise<FetchResult<T>> {
this.initQueryManager();

if (this.defaultOptions.mutate) {
options = {
...this.defaultOptions.mutate,
...options,
} as MutationOptions<T, TVariables>;
}

return this.queryManager.mutate<T>(options);
return this.initQueryManager().mutate<T>(options);
}

/**
Expand All @@ -292,9 +286,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
public subscribe<T = any, TVariables = OperationVariables>(
options: SubscriptionOptions<TVariables>,
): Observable<T> {
this.initQueryManager();

return this.queryManager.startGraphQLSubscription(options);
return this.initQueryManager().startGraphQLSubscription(options);
}

/**
Expand Down Expand Up @@ -335,7 +327,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
options: DataProxy.WriteQueryOptions<TData, TVariables>,
): void {
const result = this.initProxy().writeQuery(options);
this.queryManager.broadcastQueries();
this.initQueryManager().broadcastQueries();
return result;
}

Expand All @@ -354,7 +346,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
options: DataProxy.WriteFragmentOptions<TData, TVariables>,
): void {
const result = this.initProxy().writeFragment(options);
this.queryManager.broadcastQueries();
this.initQueryManager().broadcastQueries();
return result;
}

Expand All @@ -372,7 +364,7 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
options: DataProxy.WriteDataOptions<TData>,
): void {
const result = this.initProxy().writeData(options);
this.queryManager.broadcastQueries();
this.initQueryManager().broadcastQueries();
return result;
}

Expand All @@ -387,27 +379,32 @@ export default class ApolloClient<TCacheShape> implements DataProxy {
/**
* This initializes the query manager that tracks queries and the cache
*/
public initQueryManager() {
if (this.queryManager) return;

this.queryManager = new QueryManager({
link: this.link,
store: this.store,
queryDeduplication: this.queryDeduplication,
ssrMode: this.ssrMode,
onBroadcast: () => {
if (this.devToolsHookCb) {
this.devToolsHookCb({
action: {},
state: {
queries: this.queryManager.queryStore.getStore(),
mutations: this.queryManager.mutationStore.getStore(),
},
dataWithOptimisticResults: this.cache.extract(true),
});
}
},
});
public initQueryManager(): QueryManager<TCacheShape> {
if (!this.queryManager) {
this.queryManager = new QueryManager({
link: this.link,
store: this.store,
queryDeduplication: this.queryDeduplication,
ssrMode: this.ssrMode,
onBroadcast: () => {
if (this.devToolsHookCb) {
this.devToolsHookCb({
action: {},
state: {
queries: this.queryManager
? this.queryManager.queryStore.getStore()
: {},
mutations: this.queryManager
? this.queryManager.mutationStore.getStore()
: {},
},
dataWithOptimisticResults: this.cache.extract(true),
});
}
},
});
}
return this.queryManager;
}

/**
Expand Down