Apollo getCacheKey() with TypeScript

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.)

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:

import { ApolloCache } from 'apollo-cache'
import { NormalizedCacheObject } from 'apollo-boost'

...

Mutation: {
  resolveSomeStuff: (
    _: undefined,
    { itemId, itemOptions }: { itemId: string; itemOptions: Array<string> },
    { cache }: { cache: ApolloCache<NormalizedCacheObject> }
  ) => {
  /* Now we resolve everything. Global warming, world
  hunger, over- and under-population.... */
  ...
  }
}

Inside my resolver, the Apollo docs tell me to get an item's cacheKey like this:

const cacheKey = cache.getCacheKey({ __typename: 'Item', id: itemId})

Unfortunately, if I use this formulation, tslint will yell at me:

"Property 'getCacheKey' does not exist on type 'ApolloCache'."

Well.

Half an hour of Googling was unhelpful. But eventually I found someone reporting a similar-sounding problem as an issue over at the apollo devtools repo. I adopted his workaround, and it was effective for me. So here is the code inside my resolver:

const cacheKey = cache['config'].dataIdFromObject({
  __typename: 'Item',
  id: itemId
})

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.


If you don’t know what some or all of that means, this post probably isn’t for you.↩︎

Ambient Privacy

Because our laws frame privacy as an individual right, we don’t have a mechanism for deciding whether we want to live in a surveillance society. Congress has remained silent on the matter, with both parties content to watch Silicon Valley make up its own rules. The large tech companies point to our willing use of their services as proof that people don’t really care about their privacy. But this is like arguing that inmates are happy to be in jail because they use the prison library. Confronted with the reality of a monitored world, people make the rational decision to make the best of it. 

That is not consent.

https://idlewords.com/2019/06/the_new_wilderness.htm

Openly

The expression “openly bisexual”[1] bugs me.

I know we’re still living in a world where coming out is difficult and fraught, and if LGBTQ people want to use that expression, it’s not my busines to stop them. But it sort of annoys me when it’s used in print or by straight people. Maybe I’m overly sensitive, but it always seems to backhandedly presume that some amount of shame is associated with being bisexual, and that this “openly” bi person is therefore being bold or transgressive.

Just because some parts of society will shame bi people doesn’t mean they have anything to be ashamed of. Maybe it’s time to retire “openly” as a modifier for people’s sexual orientation.


  1. See also “openly gay”, etc.  ↩