How to use Apollo's Cache Redirects with Strapi v4?

System Information
  • Strapi Version: v4.20.3

  • Operating System:

  • Database: PostgreSQL Version 16

  • Node Version: ~18.17.0

  • NPM Version:

  • Yarn Version:


I’m using the GraphQL API in Strapi v4 with Apollo client, and trying to set up Cache redirects in my React Native app (see: Advanced topics on caching in Apollo client: Cache redirects)

This is the example from the above link:


const client = new ApolloClient({
  cache: new InMemoryCache({
    typePolicies: {
      Query: {
        fields: {
          book: {
            read(_, { args, toReference }) {
              return toReference({
                __typename: 'Book',
                id: args.id,
              });
            }
          }
        }
      }
    }
  })
});

And this is my code:


      const cache = new InMemoryCache({
        typePolicies: {
          Query: {
            fields: {
              ArticleEntity: {
                read(_, { args, toReference }) {
                  return toReference({
                    __typename: 'ArticleEntity',
                    id: args?.id,
                  });
                },
              },
            },
          },
        },
      });

It doesn’t work.

Probably has something to do with how Strapi v4 fields are structured. For example, in my case, the query looks like this:

article(id: $id) {
  data {
    id
    __typename
    attributes {
      __typename
    }
  }
}

Where
data.__typenameArticleEntity.
data.attributes.__typenameArticle

So, my question is, how set this up so it works with Strapi v4’s custom structure?

I’d appreciate any help.