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.

fetchMore/keyArgs not working as expected (duplicate queries and no concatenation)

See original GitHub issue

Intended outcome: Calling fetchMore on a paginated query concatenates results to existing query.

Actual outcome: I see 2 requests in the network tab, one with the correct offset value, another with the original value. Results of the fetchMore query are not concatenated to the original list.

How to reproduce the issue: I have a schema that looks like this:

input PostFindArgs {
   ...
   order: String
   offset: Int
}

type Post {
   id: Int
   text: String
   ...
}

type Query {
   posts(params: PostFindArgs): [Post]
}

Cache configuration like this:

const client = new ApolloClient({
  link: ApolloLink.from([authLink, errorLink, httpLink]),
  cache: new InMemoryCache({
    typePolicies: {
      Query: {
        fields: {
          posts: {
            // keyArgs: ['params.order'],
            merge(existing = [], incoming) {
              return [...existing, ...incoming];
            },
          }, 
         //concatPagination(['params']),
        },
      },
    },
  }),

And usage like this:

const { data, refetch, loading, fetchMore } = useQuery(POSTS_QUERY, {
    variables: { params: { order: 'newest', offset: 0, ... } },
});

// button calls this
fetchMore({variables: {params: {offset: 10}}});

I didn’t see anything in the documentation about how to handle keyArgs if the query arguments are an object. I would like to do something like keyArgs: ['params.order'], but that throws an error. I imagine the problem here is that without keyArgs, the object containing {offset: 10} resolves in the cache as a separate query, so it doesn’t concatenate results to the existing query. I also tried using the concatPagination helper, with no luck.

However, while this might explain the missing concatenated results, it doesn’t explain why I see 2 fetches in the network tab. A console.log shows that the fetchMore function is only called once, but I still see 2 network requests, one with the correct offset, and a second with the original offset.

Does Apollo Client v3 support object-based keyArgs? If not, should I go back to using updateQuery?

Versions System: OS: macOS 10.15.7 Binaries: Node: 14.15.0 - ~/.nvm/versions/node/v14.15.0/bin/node Yarn: 1.13.0 - /usr/local/bin/yarn npm: 6.14.8 - ~/.nvm/versions/node/v14.15.0/bin/npm Browsers: Chrome: 86.0.4240.198 Edge: 86.0.622.63 Firefox: 66.0.3 Safari: 14.0 npmPackages: @apollo/client: ^3.2.5 => 3.2.5 apollo-upload-client: ^14.1.3 => 14.1.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

40reactions
benjamncommented, Nov 12, 2020

@CaptainStiggz We do support nested objects for both keyArgs and keyFields, though the syntax is a little unusual: keyArgs: ["params", ["order"]] gives you a field key like posts({"params":{"order":...}}), and keyArgs: ["params", ["order", "offset"]] gives a field key like posts({"params":{"order":...,"offset":...}}). The benefit of this array-based syntax is that it fully specifies the order of the keys, so you don’t have to worry about non-deterministic JSON.stringify serialization.

26reactions
justinmwarnercommented, Nov 17, 2020

@benjamn , could we add that syntax for nested args to the docs? I spent an hour this morning trying to figure that out when I came across this issue. Would be super useful as a callout/example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange duplicate behavior from GROUP_CONCAT of two ...
TL;DR When you join (q1 JOIN q2) to q3 it is not on a key/UNIQUE of ... for expected amount of duplication), timing...
Read more >
Remove duplicates not working.. Can't create relationship
I created a concatenate in the edit query screen of the customer, and there are a couple duplicates. Highlighted the Concatenate column, and...
Read more >
sql server - Concatenate duplicates within table column
Is there any way that I can concatenate them in the SQL definition to avoid creating multiple rows for each po_line.line_no that has ......
Read more >
Avoid Errors due to Concatenated Values Generated by ...
This 15-minute tutorial shows you how to avoid errors due to concatenated values generated by the LISTAGG function in a query, exceeding the...
Read more >
An overview of the CONCAT function in SQL with examples
The table scan operator indicates that the query optimizer is required to read whole data of the Test_NumericValue because it does not contain ......
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