-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
@connection directive ignored when used in conjunction with keyArgs #8659
Comments
Here's the custom KeyArgsFunction I'm using as a "workaround": const keyArgsWithConnectionDirective: KeyArgsFunction = (args, context) => {
const { fieldName, field: { directives = [] } } = context
const keyArgs = ['context', 'randomize'] // These could be provided through params
const filteredArgs = keyArgs.reduce((acc, current) => {
const res = { ...acc }
if (context.variables[current]) {
res[current] = context.variables[current]
}
return res
}, {})
const connectionDirective = directives.find((directive) => directive.name.value === 'connection')
const connectionKey = connectionDirective?.arguments.find((dirArg) => dirArg.name.value === 'key')?.value.value
return `${fieldName}:${JSON.stringify(filteredArgs)}${connectionKey ? `:${connectionKey}` : ''}`
} The generated key for the type policy using this key args function along with
|
Thanks for surfacing this @habovh! You're right that the When AC3 was released, I hoped the I see a few different possible directions from here:
Do any of those ideas jump out at you as obviously the best or the worst? Improving the |
@habovh I think I found an ergonomic and flexible way to include directives (and variables) in the
These improvements will likely be released in a v3.5 beta version first, but I'm not opposed to back-porting them to v3.4.x, since they should be backwards-compatible. Please have a look at #8678 when you have a chance! |
@benjamn sorry for the delayed reply. The solution you've came up with is really elegant and I believe it fits perfectly this use case, plus many more. Following your previous comment I was about to suggest that the second approach was the best of the three because it felt like the closest to the current I see #8678 has been merged, is it part of a release already? |
Alright, #8678 ended up in Thanks for the kind words @habovh—really glad you see/agree this idea has many uses, though I'm sure that won't be obvious to everyone. Then again, maybe it doesn't need to be, since this trick should stay mostly hidden until you need it. |
@benjamn glad to see that! I may not have a chance to tryout a beta prerelease anytime soon but I'll definitely be using this once 3.5 gets released. It has many uses like you said, but this is a pretty advanced feature anyway. By the time you're looking for this kind of flexibility setting up AC, you're probably well aware of the inner workings of Apollo Client. That said, I think this deserves proper documentation under the advanced caching section: it would be a shame to go for a fully customized key |
This should now be resolved in |
I'm back with another caching issue @benjamn, hope you'll like this one too!
As I understand it, Apollo supports the @connection directive. I understand that
keyArgs
is better suited for most use-cases, however I'm finding myself in a tricky situation here.I have an endpoint that's a relay-style pagination that's configured to use Apollo Client's exported
relayStylePagination()
. This endpoint is called in multiple places in my app, and I would like each place to have its own data instead of sharing the same data from the cache. So my idea was to use a unique@connection
directive on each of these queries, so that the result would be stored under different keys in the cache and my lists would be independent.However, it seems that when I'm applying the
@connection
directive along with therelayStylePagination()
, the directive gets ignored and the data is stored according to the providedkeyArgs
only.In the following screenshots, I'm using the same query with different
keyArgs
configuration on thetypePolicy
.The query:
Here's what my cache looks like with the
keyArgs
defined:Here's what my cache looks like without
keyArgs
defined:Intended outcome:
I'm expecting that when using
@connection
directive along withkeyArgs
both are used to generate identifiers for my endpoints.Actual outcome:
@connection
directive is ignored.How to reproduce the issue:
relayStylePagination()
to define a field policy. E.g:books: relayStylePagination(['filter'])
@connection
directive to set a custom key to store the query result. E.g:books(first: 2) @connection(key: "sideBarBooks") {
ROOT_QUERY.books:{}
instead of something likeROOT_QUERY.books:{}:sideBarBooks
Versions
The text was updated successfully, but these errors were encountered: