Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Redirect not causing query to run on partial data when using lists #9063

Open
amitozalvo opened this issue Nov 15, 2021 · 6 comments
Open

Comments

@amitozalvo
Copy link

amitozalvo commented Nov 15, 2021

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:

  1. Create a query that fetches a list of items based on a condition:
items(where: { id: $id }) {
    ...CoreItemFields
  }

This will return [item]

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

@dylanwulf
Copy link
Contributor

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

@sebastienva
Copy link

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.

@aronwoost
Copy link

I can confirm, that the issue is still there. How to we help to push this issue forward?

@aronwoost
Copy link

I provided a failing test for this issue: #9902

@thomasaull
Copy link

@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

@Tsymalyi
Copy link

Tsymalyi commented Jan 31, 2023

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,
        };
      }
    },
  },
},

},`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants