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

Prepare devtools for Apollo Client 3.3 + fix queries not updating #302

Merged
merged 3 commits into from
Oct 29, 2020
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
58 changes: 42 additions & 16 deletions src/backend/broadcastQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,41 @@ function filterQueryInfo(queryInfoMap) {
return filteredQueryInfo;
}

function getQueries(client) {
if (!client || !client.queryManager) {
return () => {};
}

// Apollo Client 2
if (client.queryManager.queryStore) {
if (client.queryManager.queryStore.getStore) {
return () => client.queryManager.queryStore.getStore();
}
// Apollo Client 3
} else if (client.queryManager.queries) {
return () => filterQueryInfo(client.queryManager.queries);
}
}

function getMutations(client) {
if (!client || !client.queryManager) {
return () => {};
}

// Apollo Client 2 to 3.2
if (client.queryManager.mutationStore && client.queryManager.mutationStore.getStore) {
return () => client.queryManager.mutationStore.getStore();
} else {
// Apollo Client 3.3+
return () => client.queryManager.mutationStore;
}
}

export const initBroadCastEvents = (hook, bridge) => {
let client = null;
let queries = () => {};
let mutations = () => {};

// Counters for diagnostics
let counter = 0;

Expand Down Expand Up @@ -53,14 +87,14 @@ export const initBroadCastEvents = (hook, bridge) => {
}

let logger = ({
state: { queries, mutations },
_,
dataWithOptimisticResults: inspector,
}) => {
counter++;
enqueued = {
counter,
queries,
mutations,
queries: queries(),
mutations: mutations(),
inspector,
};
if (acknowledged) {
Expand All @@ -77,22 +111,14 @@ export const initBroadCastEvents = (hook, bridge) => {
});

bridge.on("panel:ready", () => {
const client = hook.ApolloClient;
client = hook.ApolloClient;

const queries =
client.queryManager
? client.queryManager.queryStore
// Apollo Client 2
? client.queryManager.queryStore.getStore()
// Apollo Client 3
: filterQueryInfo(client.queryManager.queries)
: {};
queries = getQueries(client);
mutations = getMutations(client);

const initial = {
queries,
mutations: client.queryManager
? client.queryManager.mutationStore.getStore()
: {},
queries: queries(),
mutations: mutations(),
inspector: client.cache.extract(true),
};

Expand Down
2 changes: 1 addition & 1 deletion src/devtools/components/WatchedQueries/WatchedQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const queryLabel = (queryId, query) => {
// If we haven't been able to find the query name, make one last
// attempt to parse and pull it from the source of a document
// (if it exsts).
if (!queryName && query.document.loc.source) {
if (!queryName && query.document && query.document.loc && query.document.loc.source && query.document.loc.source.body) {
queryName = getOperationName(parse(query.document.loc.source.body));
}

Expand Down