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

Is relaypagination() appending new collection to existing collection? #7207

Closed
albuen03 opened this issue Oct 22, 2020 · 9 comments
Closed

Comments

@albuen03
Copy link

I am working using relay pagination. I want to append incoming edges to the existing edges but relay pagination keeps on overriding existing edges. Is this a normal behavior?

@benjamn
Copy link
Member

benjamn commented Oct 22, 2020

Are you providing an args.after cursor in your query (using variables), either from the edge.cursor of the last edge or from pageInfo.endCursor?

You might also try setting a breakpoint (or debugger statement) inside the merge function provided by relayStylePagination, so you can step through and see exactly what it's doing.

@albuen03
Copy link
Author

albuen03 commented Oct 23, 2020

I provided args.after in the query based on endCursor. I created my own merge functionality based on relayStylePagination merge to achieve the desired behavior.

@tyler-dot-earth
Copy link

@albuen03 Would you mind sharing your solution? I am facing similar issues and it seems like we have a similar approach to our pagination variables.

@firmanjml
Copy link

firmanjml commented Nov 1, 2020

I am having the same behavior and I don't this is normal. Shouldn't it append new data on old list instead of overwrite the whole list ?
Here's my code snippet, and I'm not sure if I'm doing this right.

const client = new ApolloClient({
  cache: new InMemoryCache({
    typePolicies: {
      Query: {
        fields: {
          getMessages: relayStylePagination()
        }
      }
    }
  }),
  link
});
if (data.getMessages.pageInfo.hasNextPage) {
    fetchMore({
        variables: {
            before: data.getMessages.pageInfo.endCursor
        }
   });
}
query getMessages($id: Int!, $before: String) {
  getMessages(offerId: $id, before: $before) {
    pageInfo {
      endCursor
      hasNextPage
    }
    edges {
      node {
        _id: id
        text
        user: sender {
          _id: id
        }
      }
      cursor
    }
  }
}

@albuen03
Copy link
Author

albuen03 commented Nov 3, 2020

@tsnieman @firmanjml I created somewhat like, in the typePolicies

merge(existing, incoming) {
            return {
              ...existing,
              ...incoming,
              edges: compact(
                uniqBy(
                  'node.__ref',
                  concat(get('edges', existing), get('edges', incoming))
                )
              )
            };
          }

It appends the data to the existing collection.

@ruipneves
Copy link

ruipneves commented Dec 7, 2020

I am having a strange behavior using relayStylePagination and optimisticResponse.
For some reason, the optimisticResponse seems to be working just fine as it is immediately shown in the UI and updated when the response comes from the server but, the entire list gets re-rendered on both situations. It seems that the node references are constantly changing.

I have tried to modify the relayStylePagination merge function and apparently something before the merge function is modifying the array references.

If I don't try to modify the cache in the mutation and let the subscription handle the merge with updateQuery the references are not modified.

Can anyone help me?

Edit: After more debugging it seems that the refs are OK after my merge functions along the tree and the problem might be when the __ref is converted to the data object? In the merge functions I get correct refs but in my component they are never the same

Also, whenever I'm adding a new item to a list with relay pagination style, should I create a reference of the node or how should I do it? At the moment, i have a reference for the node only:

{ node: __ref: "Message:123456789", cursor: "123456789"}

@benjamn

Edit 2: I found out that optimisticResponse is the problem and not relayStylePagination or any merge that I have.
An issue already referenced this with a possible solution by modifying the entityStore.js file but it might have other repercussions.

#4141 (comment)

This will be solved in v3.4

@Flusinerd
Copy link

@firmanjml
Did you manage to find a solution?
I am running into the same issue, with after set and pageInfo.endCursor and edges[].cursor also queried

@alimoli
Copy link

alimoli commented Feb 3, 2022

Any news?

@bignimbus
Copy link
Contributor

Hi folks, this issue is a bit stale and it appears that this may have been resolved in #4141. If folks are still experiencing similar issues in the latest client version please do feel free to open a new issue or ping this one and we can consider re-opening depending on the details. Thanks so much all!

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

No branches or pull requests

8 participants