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

(graphcache) - Alias resolvers/updates parent to its parentKey in cache methods #1208

Merged
merged 8 commits into from
Dec 9, 2020

Conversation

kitten
Copy link
Member

@kitten kitten commented Dec 9, 2020

Resolves #1207

Summary

This updates the shared context object to be provided on a global ref, which is then used in various cache methods, via cache.keyOfEntity, to alias the parent argument, which can be found on resolvers and updaters, to info.parentKey. This is done by adding (publicly) info.parent, which is the current parent's data as it is. (This avoids us having to share it on a separate object)

This further refactors store.keyOfEntity to take care of all key normalisation that has been done repeatedly in separate methods before, which further ensures that cache[method](parent) is going to work consistently as expected.

The reasoning here is that writing resolvers with parent as the first argument to further cache methods is so intuitive that we even did it ourselves in the docs, without thinking about it, although its support wasn't guaranteed as #1185 demonstrates. As an example:

cacheExchange({
  resolvers: {
    Todo: {
      createdAt(parent, args, cache) {
        const date = cache.resolve(parent, 'createdAt');
        return new Date(date);
      }
    }
  },
})

Set of changes

  • Add parent field to Context and ResolveInfo
  • Add global ref for the current Context to be exposed
  • Add store.keyOfEntity alias if entity === ctx.parent to ctx.parentKey
  • Refactor store methods to decrease repetition
  • Update API docs

@kitten kitten requested a review from JoviDeCroock December 9, 2020 03:05
@changeset-bot
Copy link

changeset-bot bot commented Dec 9, 2020

🦋 Changeset detected

Latest commit: f7d9e33

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@urql/exchange-graphcache Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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

Successfully merging this pull request may close these issues.

(graphcache) - cache.resolve(parent, ...) doesn't seem to work
2 participants