apollo-client
                                
                                 apollo-client copied to clipboard
                                
                                    apollo-client copied to clipboard
                            
                            
                            
                        Cache Redirect not causing query to run on partial data when using lists
Intended outcome: Query should be sent to the server to receive the missing reference
Actual outcome: Query not sent to server and I get no data
How to reproduce the issue:
- Create a query that fetches a list of items based on a condition:
items(where: { id: $id }) {
    ...CoreItemFields
  }
This will return [item]
- Create a cache redirect to the query
new InMemoryCache({
        typePolicies: {
            Query: {
                fields: {
                    items: {
                        read(_, { args, toReference }) {
                            if (!!args.where?.id) {
                                return [toReference({ __typename: "Item", id: args.where.id })];
                            }
                        }
                    }
                }
            }
        }
    }
If you run a query that queries items that do not exist in the cache, the read function will run, returning a list of 1 reference as expected. But since the reference references to something that does not exist in cache, the query should run. But it doesn't.
Versions System: OS: macOS 12.0.1 Binaries: Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node Yarn: 1.22.15 - ~/.yarn/bin/yarn npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm Browsers: Chrome: 95.0.4638.69 Safari: 15.1 npmPackages: apollo-server-express: ^3.3.0 => 3.4.0
I think I ran into this same problem here: https://community.apollographql.com/t/apollo-inmemory-cache-type-policy-for-query-with-list-of-ids-as-argument/1989
Same here since we upgraded to 3.x (no issue in 2.x), but I use a workaround. If one ref is missing from cache, I disable the cache redirect to trigger the query.
Something like that :
read(_, { args, toReference, canRead }) {
   if (!!args.where?.id) {
     const refs =  [toReference({ __typename: "Item", id: args.where.id })];
     if (refs.find(ref => !canRead(ref))) {
       return undefined;
     }
     return refs;
  }
}
I hope this can help.
I can confirm, that the issue is still there. How to we help to push this issue forward?
I provided a failing test for this issue: https://github.com/apollographql/apollo-client/pull/9902
@sebastienva Thank you for your workaround, I've been running in to the same issue and was wasting a lot of time trying to solive it until I finally found this issue :)
@jpvajda I can confirm this bug still exists in 3.6.9 and 3.7.0-beta.6
Same here since we upgraded to 3.x (no issue in 2.x), but I use a workaround. If one ref is missing from cache, I disable the cache redirect to trigger the query.
Something like that :
read(_, { args, toReference, canRead }) { if (!!args.where?.id) { const refs = [toReference({ __typename: "Item", id: args.where.id })]; if (refs.find(ref => !canRead(ref))) { return undefined; } return refs; } }I hope this can help.
Yes, it works. But with this cache redirect doesn't work updateQuery - previousResult return empty object.
TypePolicy :
  ShortBonusTemplate: { keyFields: ['uuid'], },
`  Query: {
fields: {
shortBonus: {
read(_, { args, toReference, canRead }) {
if (args?.uuids?.length) {
const refs = args?.uuids?.map((uuid) =>
toReference({
__typename: 'ShortBonus',
uuid,
})
);
        if (
          refs.find((ref) => {
            return !canRead(ref);
          })
        ) {
          return undefined;
        }
        return {
          content: refs,
          page: args?.page ?? 0,
          size: args?.uuids.length,
          last: true,
          totalPages: 1,
        };
      }
    },
  },
},
},`