question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@connection directive ignored when used in conjunction with keyArgs

See original GitHub issue

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 the relayStylePagination(), the directive gets ignored and the data is stored according to the provided keyArgs only.

In the following screenshots, I’m using the same query with different keyArgs configuration on the typePolicy.

The query:

query {
  suggestions({ first: 3 }) @connection(key: "newsfeedSuggestions") {}
}

Here’s what my cache looks like with the keyArgs defined: Screenshot 2021-08-17 at 18 51 02

Here’s what my cache looks like without keyArgs defined: Screenshot 2021-08-17 at 18 50 19

Note 1: I realise I can still achieve the desired result using a key args function instead of an array of strings. If this is the intended route, then updating the docs and explicitly stating that keyArgs cannot be used in conjunction with the @connection directive would be nice.

Note 2: my need came from a weird other bug preventing me from having multiple useQuery hooks pointing on the same cache object for paginations. It seems like when I do so, each list is “fighting” the other and tries to write its own result to the cache in a loop, resulting in glitchy and unusable UI.

Intended outcome:

I’m expecting that when using @connection directive along with keyArgs both are used to generate identifiers for my endpoints.

Actual outcome:

@connection directive is ignored.

How to reproduce the issue:

  • Use relayStylePagination() to define a field policy. E.g: books: relayStylePagination(['filter'])
  • Query this field using @connection directive to set a custom key to store the query result. E.g: books(first: 2) @connection(key: "sideBarBooks") {
  • See that it gets stored under ROOT_QUERY.books:{} instead of something like ROOT_QUERY.books:{}:sideBarBooks

Versions

  System:
    OS: macOS 11.5.2
  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.159
    Firefox: 88.0
    Safari: 14.1.2
  npmPackages:
    @apollo/client: ^3.4.8 => 3.4.8 
    apollo3-cache-persist: ^0.9.1 => 0.9.1 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
habovhcommented, Aug 26, 2021

@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 keyArgs-less behaviour. However your solution is orders of magnitude better and far more flexible.

I see #8678 has been merged, is it part of a release already?

1reaction
habovhcommented, Aug 26, 2021

@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 keyArgs function instead of your simpler and more importantly tested approach if it can yield the desired results.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Key arguments in Apollo Client
Using the keyArgs API. ... The @connection directive is a Relay-inspired convention that Apollo ... and any other arguments (like filter ) are...
Read more >
What is the distinction between @connection and keyArgs?
This seems akin to the @connection directive, which provides similar functionality (specify a custom store key for results).
Read more >
Working with Engines and Connections
This section describes how to use transactions when working directly with Engine and Connection objects. When using the SQLAlchemy ORM, the public API...
Read more >
36 SWIG and Python
The name of the module is specified using the %module directive or the -module command ... or you can use the %ignore directive...
Read more >
tornado.web — RequestHandler and Application classes
request.body) # Access self.args directly instead of using self.get_argument. RequestHandler ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found