Skip to content

Commit

Permalink
Improve token domain balances in apollo cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ceolson01 committed Jan 15, 2020
1 parent 4dcf807 commit 2d453c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/data/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
InMemoryCache,
IntrospectionFragmentMatcher,
defaultDataIdFromObject,
} from 'apollo-cache-inmemory';

import introspectionResult from './generated';
Expand All @@ -9,6 +10,18 @@ const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData: introspectionResult,
});

const dataIdFromObject = obj => {
// eslint-disable-next-line no-underscore-dangle
if (obj.__typename === 'DomainBalance') {
// Avoid having array arguments inside the cache key
// Avoid `${address}.${[...domainIds]}`. Cache creates individual entries for each combination
const { address, colonyAddress, domainId } = obj;
return `Colony:${colonyAddress}.Token:${address}.Domain:${domainId}`;
}
return defaultDataIdFromObject(obj);
};

export default new InMemoryCache({
dataIdFromObject,
fragmentMatcher,
});
5 changes: 4 additions & 1 deletion src/data/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ export const tokenResolvers = ({ colonyManager }: ContextType): Resolvers => ({

return domainIds.map((domainId, idx) => ({
__typename: 'DomainBalance',
domainId,
amount: balances[idx].toString(),
domainId,
// `address` & `colonyAddress` only used for cache key - NOT PART OF QUERY
address,
colonyAddress,
}));
},
async details(
Expand Down

0 comments on commit 2d453c1

Please sign in to comment.