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

add public method to retrieve all current observable queries #7813

Merged
12 changes: 12 additions & 0 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,18 @@ describe('client', () => {
expect(spy).toHaveBeenCalledWith(options);
});

it('has a getObservableQueries method which calls QueryManager', async () => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache(),
});

// @ts-ignore
const spy = jest.spyOn(client.queryManager, 'getObservableQueries');
await client.getObservableQueries();
expect(spy).toHaveBeenCalled();
});

itAsync('should propagate errors from network interface to observers', (resolve, reject) => {
const link = ApolloLink.from([
() =>
Expand Down
165 changes: 165 additions & 0 deletions src/__tests__/refetchQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,171 @@ describe("client.refetchQueries", () => {
resolve();
});

itAsync('includes all queries when options.include === "all"', async (resolve, reject) => {
const client = makeClient();
const [
aObs,
bObs,
abObs,
] = await setup(client);

const ayyResults = await client.refetchQueries({
include: "all",

updateCache(cache) {
cache.writeQuery({
query: aQuery,
data: {
a: "Ayy",
},
});
},

onQueryUpdated(obs, diff) {
if (obs === aObs) {
expect(diff.result).toEqual({ a: "Ayy" });
} else if (obs === bObs) {
expect(diff.result).toEqual({ b: "B" });
} else if (obs === abObs) {
expect(diff.result).toEqual({ a: "Ayy", b: "B" });
} else {
reject(`unexpected ObservableQuery ${
obs.queryId
} with name ${obs.queryName}`);
}
return Promise.resolve(diff.result);
},
});

sortObjects(ayyResults);

expect(ayyResults).toEqual([
{ a: "Ayy" },
{ a: "Ayy", b: "B" },
{ b: "B" },
]);

const beeResults = await client.refetchQueries({
include: "all",

updateCache(cache) {
cache.writeQuery({
query: bQuery,
data: {
b: "Bee",
},
});
},

onQueryUpdated(obs, diff) {
if (obs === aObs) {
expect(diff.result).toEqual({ a: "Ayy" });
} else if (obs === bObs) {
expect(diff.result).toEqual({ b: "Bee" });
} else if (obs === abObs) {
expect(diff.result).toEqual({ a: "Ayy", b: "Bee" });
} else {
reject(`unexpected ObservableQuery ${
obs.queryId
} with name ${obs.queryName}`);
}
return diff.result;
},
});

sortObjects(beeResults);

expect(beeResults).toEqual([
{ a: "Ayy" },
{ a: "Ayy", b: "Bee" },
{ b: "Bee" },
]);

unsubscribe();
resolve();
});

itAsync('includes all active queries when options.include === "active"', async (resolve, reject) => {
const client = makeClient();
const [
aObs,
bObs,
abObs,
] = await setup(client);

const extraObs = client.watchQuery({ query: abQuery });
expect(extraObs.hasObservers()).toBe(false);

const activeResults = await client.refetchQueries({
include: "active",

onQueryUpdated(obs, diff) {
if (obs === aObs) {
expect(diff.result).toEqual({ a: "A" });
} else if (obs === bObs) {
expect(diff.result).toEqual({ b: "B" });
} else if (obs === abObs) {
expect(diff.result).toEqual({ a: "A", b: "B" });
} else {
reject(`unexpected ObservableQuery ${
obs.queryId
} with name ${obs.queryName}`);
}
return Promise.resolve(diff.result);
},
});

sortObjects(activeResults);

expect(activeResults).toEqual([
{ a: "A" },
{ a: "A", b: "B" },
{ b: "B" },
]);

subs.push(extraObs.subscribe({
next(result) {
expect(result).toEqual({ a: "A", b: "B" });
},
}));
expect(extraObs.hasObservers()).toBe(true);

const resultsAfterSubscribe = await client.refetchQueries({
include: "active",

onQueryUpdated(obs, diff) {
if (obs === aObs) {
expect(diff.result).toEqual({ a: "A" });
} else if (obs === bObs) {
expect(diff.result).toEqual({ b: "B" });
} else if (obs === abObs) {
expect(diff.result).toEqual({ a: "A", b: "B" });
} else if (obs === extraObs) {
expect(diff.result).toEqual({ a: "A", b: "B" });
} else {
reject(`unexpected ObservableQuery ${
obs.queryId
} with name ${obs.queryName}`);
}
return Promise.resolve(diff.result);
},
});

sortObjects(resultsAfterSubscribe);

expect(resultsAfterSubscribe).toEqual([
{ a: "A" },
{ a: "A", b: "B" },
// Included thanks to extraObs this time.
{ a: "A", b: "B" },
// Sorted last by sortObjects.
{ b: "B" },
]);

unsubscribe();
resolve();
});

itAsync("refetches watched queries if onQueryUpdated not provided", async (resolve, reject) => {
const client = makeClient();
const [
Expand Down
15 changes: 15 additions & 0 deletions src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
RefetchQueriesOptions,
RefetchQueriesResult,
InternalRefetchQueriesResult,
RefetchQueriesInclude,
} from './types';

import {
Expand Down Expand Up @@ -564,6 +565,20 @@ export class ApolloClient<TCacheShape> implements DataProxy {
return result;
}

/**
* Get all currently active `ObservableQuery` objects, in a `Map` keyed by
* query ID strings. An "active" query is one that has observers and a
* `fetchPolicy` other than "standby" or "cache-only". You can include all
* `ObservableQuery` objects (including the inactive ones) by passing "all"
* instead of "active", or you can include just a subset of active queries by
* passing an array of query names or DocumentNode objects.
*/
public getObservableQueries(
include: RefetchQueriesInclude = "active",
): Map<string, ObservableQuery<any>> {
return this.queryManager.getObservableQueries(include);
}
Comment on lines +576 to +580
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, all "active" queries will be included, but you can pass any RefetchQueriesInclude-typed value (in other words, anything you can pass as options.include to client.refetchQueries).


/**
* Exposes the cache's complete state, in a serializable format for later restoration.
*/
Expand Down
Loading