skillbox/client/src/graphql/cache.js

136 lines
4.3 KiB
JavaScript

import {makeVar, defaultDataIdFromObject, InMemoryCache} from '@apollo/client/core';
const showNavigationSidebarVar = makeVar(false);
const showProfileSidebarVar = makeVar(false);
const scrollToElementVar = makeVar('');
const currentFilterVar = makeVar('');
const helloEmailVar = makeVar('');
const idToRefFactory = (__typename) => (_, {args, toReference}) => {
return toReference({
__typename,
id: args.id
});
};
const typePolicies = {
// https://www.apollographql.com/docs/react/local-state/managing-state-with-field-policies/#example
Query: {
fields: {
sidebar: {
read() {
return {
profile: showProfileSidebarVar(),
navigation: showNavigationSidebarVar()
};
}
} ,
scrollPosition: {
read() {
return {
scrollTo: scrollToElementVar()
};
}
},
instrumentFilter: {
read() {
return {
currentFilter: currentFilterVar()
};
}
},
helloEmail: {
read() {
return {
email: helloEmailVar()
};
}
},
// these used to be in cacheRedirects, now here
// contentBlock: (_, args, {getCacheKey}) => getCacheKey({__typename: 'ContentBlockNode', id: args.id}),
contentBlock: {
read: idToRefFactory('ContentBlockNode')
// read(_, {args, toReference}) {
// return toReference({
// __typename: 'ContentBlockNode',
// id: args.id
// });
// }
}
},
// chapter: (_, args, {getCacheKey}) => getCacheKey({__typename: 'ChapterNode', id: args.id}),
chapter: {read: idToRefFactory('ChapterNode')},
assignment: {read: idToRefFactory('AssignmentNode')},
objective: {read: idToRefFactory('ObjectiveNode')},
objectiveGroup: {read: idToRefFactory('ObjectiveGroupNode')},
projectEntry: {read: idToRefFactory('ProjectEntryNode')},
// assignment: (_, args, {getCacheKey}) => getCacheKey({__typename: 'AssignmentNode', id: args.id}),
// objective: (_, args, {getCacheKey}) => getCacheKey({__typename: 'ObjectiveNode', id: args.id}),
// objectiveGroup: (_, args, {getCacheKey}) => getCacheKey({__typename: 'ObjectiveGroupNode', id: args.id}),
// projectEntry: (_, args, {getCacheKey}) => getCacheKey({__typename: 'ProjectEntryNode', id: args.id}),
},
};
const possibleTypes = {
ContentBlockInterface: ['ContentBlockNode', 'SnapshotContentBlockNode'],
ChapterInterface: ['ChapterNode', 'SnapshotChapterNode']
};
const cache = new InMemoryCache({
// used to 'override' the behavior in resolving different types. We use it for local state management
// https://www.apollographql.com/docs/react/local-state/managing-state-with-field-policies/#example
typePolicies,
possibleTypes,
// todo: document what this does, or link the doc for it at least
dataIdFromObject: obj => {
switch (obj.__typename) {
case 'InstrumentNode':
case 'ModuleNode':
case 'RoomEntryNode':
return `${obj.__typename}:${obj.slug}`;
default:
return defaultDataIdFromObject(obj);
}
},
// todo: document what this does, or link the doc for it at least
// todo: this is deprecated, it's now included in typePolicies
cacheRedirects: {
Query: {
project: (_, args, {getCacheKey}) => {
if (args.slug) {
return getCacheKey({__typename: 'ProjectNode', id: args.slug});
} else {
return getCacheKey({__typename: 'ProjectNode', id: args.id});
}
},
},
},
});
// TODO: Monkey-patching in a fix for an open issue suggesting that
// `readQuery` should return null or undefined if the query is not yet in the
// cache: https://github.com/apollographql/apollo-feature-requests/issues/1
// probably not needed any more, as per https://github.com/apollographql/apollo-client/pull/7098
// cache.originalReadQuery = cache.readQuery;
// cache.readQuery = (...args) => {
// try {
// return cache.originalReadQuery(...args);
// } catch (err) {
// console.error(err);
// return undefined;
// }
// };
export {
showProfileSidebarVar,
showNavigationSidebarVar,
scrollToElementVar,
currentFilterVar,
helloEmailVar,
cache
};
export default cache;