Is it possible to type apollo cache?

Hello,

Is there a way to configure apollo cache with typescript so fields in cache.modify won’t be any?

For example I have a query

feed () {
  items {
    id
  }
  nextToken
}

and later I want to optimistically update it via:

cache.modify({
          fields: {
            // existing is any here
            feed(existing) {
              const newPostRef = client.cache.writeFragment({
                fragment: PostCardFragment,
                fragmentName: 'PostCardFragment',
                data: post,
              });

              if (newPostRef) {
                return {
                  ...existing,
                  items: [newPostRef, ...existing.items],
                };
              }

              return existing;
            },
          },
        })

How am I supposed to type existing? Are there any tricks to do that?

Thank you

Hi @misha-erm :wave: have you tried passing a generic to cache.modify? Or annotating existing?

Or annotating existing ?
This is what I’m doing right now

have you tried passing a generic to cache.modify
This sounds cool but I failed to find a proper generic to pass there… I assume I should pass a generic that reflects structure of my whole schema?
Do you know if there is any example around to take a look at?

p.s. I’m using pretty basic graphql-codegen with preset: 'client'