{"componentChunkName":"component---src-templates-post-js","path":"/pensieve/apollo-getcachkey-with-typescript","result":{"data":{"markdownRemark":{"html":"<h2>Problem</h2>\n<p>I’m currently working on the front end of an app that uses React + Apollo (via apollo-boost) + TypeScript. I’m to the point where I’m retrieving items from the cache in my resolvers using fragments1, which means I need to use getCacheKey() to search for the item's cache key using its ID field. (In this app, it would probably work to just assume the cache key is TypeName:UUID, but you never know, so we should probably do things properly.)</p>\n<p>Unfortunately, I have not been able to find the correct typings for getCacheKey(). When passing in cache as an argument to a resolver, one can import NormalizedCacheObject for the typings, like so:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> ApolloCache <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'apollo-cache'</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> NormalizedCacheObject <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'apollo-boost'</span>\n\n<span class=\"token operator\">...</span>\n\nMutation<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n  resolveSomeStuff<span class=\"token operator\">:</span> <span class=\"token punctuation\">(</span>\n    _<span class=\"token operator\">:</span> <span class=\"token keyword\">undefined</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">{</span> itemId<span class=\"token punctuation\">,</span> itemOptions <span class=\"token punctuation\">}</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> itemId<span class=\"token operator\">:</span> string<span class=\"token punctuation\">;</span> itemOptions<span class=\"token operator\">:</span> Array<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>string</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\"> },</span>\n<span class=\"gatsby-highlight-code-line\"><span class=\"token plain-text\">    </span><span class=\"token punctuation\">{</span> cache <span class=\"token punctuation\">}</span><span class=\"token plain-text\">: </span><span class=\"token punctuation\">{</span> cache<span class=\"token operator\">:</span> ApolloCache<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">NormalizedCacheObject</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\"> }</span></span><span class=\"token plain-text\">  ) => </span><span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">/* Now we resolve everything. Global warming, world\n  hunger, over- and under-population.... */</span>\n  <span class=\"token operator\">...</span>\n  <span class=\"token punctuation\">}</span><span class=\"token plain-text\"></span>\n<span class=\"token plain-text\">}</span></code></pre></div>\n<p>Inside my resolver, the Apollo docs tell me to get an item's cacheKey like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\"><span class=\"token keyword\">const</span> cacheKey <span class=\"token operator\">=</span> cache<span class=\"token punctuation\">.</span><span class=\"token function\">getCacheKey</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> __typename<span class=\"token operator\">:</span> <span class=\"token string\">'Item'</span><span class=\"token punctuation\">,</span> id<span class=\"token operator\">:</span> itemId <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Unfortunately, if I use this formulation, tslint will yell at me:</p>\n<div class=\"gatsby-highlight\" data-language=\"shell\"><pre class=\"language-shell\"><code class=\"language-shell\">ERROR: <span class=\"token string\">\"Property 'getCacheKey' does not exist on type 'ApolloCache'.\"</span></code></pre></div>\n<h2>Solution</h2>\n<p>Half an hour of Googling was unhelpful. But eventually I found someone reporting a similar-sounding problem as an issue over at the <a href=\"https://github.com/apollographql/apollo-client-devtools/issues/136\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">apollo devtools repo</a>. I adopted his workaround, and it was effective for me. So here is the code inside my resolver:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\"><span class=\"token keyword\">const</span> cacheKey <span class=\"token operator\">=</span> cache<span class=\"token punctuation\">[</span><span class=\"token string\">'config'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">dataIdFromObject</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  __typename<span class=\"token operator\">:</span> <span class=\"token string\">'Item'</span><span class=\"token punctuation\">,</span>\n  id<span class=\"token operator\">:</span> itemId<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<blockquote>\n<p>This essentially does an end run around the problem by not calling getCacheKey() at all. I don't know enough about the inner workings of the Apollo client to understand why this works, but I kind of don't care.</p>\n</blockquote>\n<p>If you don’t know what some or all of that means, this post probably isn’t for you.</p>","frontmatter":{"title":"Apollo getCacheKey() with TypeScript","description":"DEVELOPMENT","date":"2019-11-9","slug":"/pensieve/apollo-getcachkey-with-typescript","tags":["Apollo","React"]}}},"pageContext":{}}}