Skip to content
New issue

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

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

Already on GitHub? # to your account

Cache invalidation doesn't work on aliased queries #83

Closed
SavelevMatthew opened this issue Aug 6, 2024 · 3 comments
Closed

Cache invalidation doesn't work on aliased queries #83

SavelevMatthew opened this issue Aug 6, 2024 · 3 comments

Comments

@SavelevMatthew
Copy link
Contributor

SavelevMatthew commented Aug 6, 2024

Hi, thanks for such a useful library for Apollo-client. Recently I decided to apply it to my project and found the following error:

If you use alias for query, the cache will not be invalidated:

query getMetersByAddressKey($where: MeterWhereInput!, $first: Int!, $skip: Int!) {
    meters: allMeters(
        where: $where
        first: $first
        skip: $skip
    ) {
        id
    }

    meta: _allMetersMeta(where: $where) {
        count
    }
}

I've started digging into why this was happening and discovered the following:

  1. When you call super.diff right here it returns result in format like so (aliases as keys):
{
  "meters": [
    {"id": 123, "__typename": "Meter"},
    {"id": 456, "__typename": "Meter"},
  ]
  "meta" {}
}
  1. When it goes to cacheResultProcessor.processReadResult and here you obtain fieldName (allMeters in my case) and resultKeyName (meters in my case) and then pass fieldName to processReadSubResult function
  2. The first thing processReadSubResult does is extracting field from result. So in my case it tries to get result['allMeters'], which resolved in undefined and processReadSubResult returns Completed status.

I propose to fix this by passing in processReadSubResult in step 2 value like so resultKeyName || fieldName.

What do you think?

@SavelevMatthew
Copy link
Contributor Author

Implemented a fix:
#84

@pahaz
Copy link

pahaz commented Aug 6, 2024

Nice catch! I have similar issues

@danReynolds
Copy link
Collaborator

lgtm! Merging

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants