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

Nested keyfields fails when using alias #8639

Closed
habovh opened this issue Aug 12, 2021 · 4 comments · Fixed by #8643
Closed

Nested keyfields fails when using alias #8639

habovh opened this issue Aug 12, 2021 · 4 comments · Fixed by #8643

Comments

@habovh
Copy link

habovh commented Aug 12, 2021

According to the cache configuration docs:

[...] keyFields strings always refer to the actual field names as defined in your schema, meaning the ID computation is not sensitive to field aliases.

I'm in somewhat of a corner case here. Because of a lack of UUIDs on some of my schema models, I'm using typePolicy's keyFields to configure an association of keys that are unique as the key.

Here's an example configuration:

    ...
    Book: {
      keyFields: ["title", "author", ["name"]],
    },
    ...

This works fine when I'm actually querying these fields as-is, but if in my query I aliased the author key to user, Apollo throws the following error when trying to call toReference() in another resolver's merge function:

 WARN  Possible Unhandled Promise Rejection (id: 0):
TypeError: undefined is not an object (evaluating 'hasOwn.call(response, responseName)')
hasOwnProperty@[native code]
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:126626:24
forEach@[native code]
computeKeyObject@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:126615:22
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:126620:45
forEach@[native code]
computeKeyObject@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:126615:22
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:126575:59
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:126176:34
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:125742:33
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:125868:46
map@[native code]
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:125793:56
forEach@[native code]
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:125774:22
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:125793:56
forEach@[native code]
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:125774:22
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:125705:41
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:126735:45
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:156813:33
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:151457:46
tryCallOne@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:28134:16
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:28235:27
_callTimer@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:29223:17
_callImmediatesPass@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:29262:17
callImmediates@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:29480:33
__callImmediates@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:3771:35
http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:3549:34
__guard@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:3754:15
flushedQueue@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.example.app:3548:21

Note that it was not trivial to figure out, since there was no explicit message. Some searching led me to #6665 where I would eventually find the complete expected error message in @benjamn's reply: #6665 (comment). I guess the error I'm looking at is actually the key 3 on his snippet.

Intended outcome:

keyFields should work for nested keys when using an alias.

Actual outcome:

keyFields is unable to resolve nested keys when using an alias along the path.

How to reproduce the issue:

  • setup a type policy keyFields with nested field. Example: ["title", "author", ["name"]]
  • query an object of this type but instead of querying author directly, use an alias. Example: user: author
  • call toReference() with the resulting object. Example: in a merge() field policy.

Versions

  System:
    OS: macOS 11.5
  Binaries:
    Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.7.6 - ~/.nvm/versions/node/v15.14.0/bin/npm
  Browsers:
    Chrome: 92.0.4515.131
    Firefox: 88.0
    Safari: 14.1.2
  npmPackages:
    @apollo/client: ^3.3.16 => 3.3.16 
    apollo3-cache-persist: ^0.9.1 => 0.9.1 
@benjamn
Copy link
Member

benjamn commented Aug 13, 2021

@habovh Fix incoming! #8643

@habovh
Copy link
Author

habovh commented Aug 13, 2021

@benjamn wow that was fast! Thanks!

@benjamn
Copy link
Member

benjamn commented Aug 16, 2021

This should be fixed in @apollo/[email protected] (just published). Thanks for catching this bug @habovh!

@benjamn benjamn closed this as completed Aug 16, 2021
@habovh
Copy link
Author

habovh commented Aug 17, 2021

@benjamn just tested this right now, seems to be working fine! Thank you for such a fast fix & release!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.