-
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
Pagination Apollo v3 example #6502
Comments
Check this out: 48df9da |
I don't claim to understand how but I'm happy to report I was able to get Relay pagination working using @benjamn 's recent efforts in
Been trying to learn InMemoryCache 3.0 and while I don't have a strong handle on it yet, at least today it seems pretty convenient to have Apollo handle pagination 'automatically' vs a bunch of separate places in my app using largely the same boilerplate. |
If they are going to deprecate a feature, they should update the documentation to show the new way of doing things. I saw the same error and can't find "field policies" in the documentation at all. |
Pagination docs are definitely out of date. For now, you can find a little more info about new pagination patterns on this blog post: https://www.apollographql.com/blog/announcing-the-release-of-apollo-client-3-0/?mc_cid=e593721cc7&mc_eid=235a3fce14 Implementation of these new pagination policies can be found here: https://github.com/apollographql/apollo-client/blob/master/src/utilities/policies/pagination.ts |
https://www.apollographql.com/docs/react/caching/cache-field-behavior/#the-merge-function |
Not sure if it's a bug but while using In order to work around this issue I adjusted the 'read' function as:
|
@Salman-Maven wow great timing, I just ran in to this tonight as well. If you check your network tab you'll notice there isn't a network request on page load for your Note that the graphql XHR you see on page load is not related to the loading of my |
@Salman-Maven: thanks, your workaround works for me. I used your solution to create a custom field policy, which I will use for all my relay connections as long as this bug exists: import { relayStylePagination } from '@apollo/client/utilities';
import { FieldPolicy } from '@apollo/client/cache';
const fixedRelayStylePagination = (...args): FieldPolicy => ({
...relayStylePagination(...args),
read: (...readArgs) => {
const existing = readArgs[0];
const originalRead = relayStylePagination(...args).read;
if (!existing || !originalRead) {
return;
}
return originalRead(...readArgs);
},
}); usage: new InMemoryCache({
typePolicies: {
Query: {
fields: {
chat: fixedRelayStylePagination(),
},
},
},
}); |
@uncvrd the gif sums up the issue perfectly. Changing the Also in my case, I was using server-side rendering so I couldn't use For now, I'm using the |
Please try |
I also have questions about converting to use field policies instead of updateQueries. My current Query/fetchMore looks like this:
So, I would start by reading up on how field policies work here:
Then I'd add that to the InMemoryCache constructor. In my case it might look like something like this:
Does that sound right? |
This is a really weird implementation choice. We use Apollo across a number of projects and have a standard updateQuery for paginating data sets. So now we have to go back through all our projects and add field-level information for all our data models? |
What if you have a field under a Query that you want to merge the data one way, but you want it to merge differently on another list? Same query but merge differently |
This is exactly one of the roadblocks that I've run into. For example, I use I honestly would rather handle merging in multiple places using I'd also like to know why the And why is the |
In my case, the incoming data is not merged with existing data. When i click on load more button, it presents only the current data. This is how I have done const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
conversation: relayStylePagination(),
// conversation: {
// keyArgs: [],
// merge(existing = {}, incoming, { args }) {
// console.log('existing', existing);
// console.log('incominng', incoming);
// if (args && !args.params.after) {
// return incoming;
// }
// return {
// ...existing,
// pageInfo: incoming.data.pageInfo,
// edges: [...existing.data.edges, ...incoming.data.edges],
// };
// },
// },
},
},
},
}); const first = 10;
export default function ChatBox(): ReactElement {
const { channelId, contactId } = useParams<Record<string, string | undefined>>();
const { data, fetchMore, networkStatus, subscribeToMore } = useQuery<Pick<Query, 'conversation'>>(
CONVERSATION,
{
variables: {
channel: channelId,
contact: contactId,
params: {
first,
},
},
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-only',
},
);
const messages = data?.conversation?.data?.edges;
const hasNextPage = data?.conversation?.data?.pageInfo.hasNextPage;
const after = data?.conversation?.data?.pageInfo.endCursor;
const isRefetching = networkStatus === 3;
const onLoadNext = () => {
fetchMore({
variables: {
params: {
first,
after,
},
},
});
};
console.log('messages', messages);
return (
<>
<div className='d-flex flex-column h-100'>
<TopBar />
{!messages?.length ? (
<EmptyConversation />
) : (
<>
<ChatStyles className='px-24' id='scroll'>
<React.Suspense fallback={<Spinner />}>
<MessageList messages={messages ? generateChatMessages(messages) : []} />
{hasNextPage && (
<>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button
type='button'
id='buttonLoadMore'
disabled={isRefetching}
onClick={onLoadNext}
/>
</>
)}
</React.Suspense>
</ChatStyles>
</>
)}
</div>
</>
);
} this is the shape of query conversation(
channel: ShortId
contact: ShortId
params: ConnectionInput
): ConversationHistoryPayload!
type ConversationHistoryPayload {
status: Int!
error: ErrorData
data: ConversationConnection
} export const CONVERSATION = gql`
query conversation($channel: ShortId, $contact: ShortId, $params: ConnectionInput) {
conversation(channel: $channel, contact: $contact, params: $params) {
status
error {
...Error
}
data {
pageInfo {
...PageInfo
}
edges {
cursor
node {
...Communication
}
}
}
}
}
${ERROR_FRAGMENT}
${PAGE_INFO}
${COMMUNICATION}
`; both relayStylePagination and merge did not work in my case. Did i miss anything? |
Since, the input type and response type is a bit different like in above case the arugmennts like |
I'm having the same issue as @Tushant. I can see that the fetchMore() call is successfully querying for the next page of data, and if I remove the ids from my query the merge function complains, suggesting it is actually merging in the new data, but I never get a re-render, so the new data does not show up, and the cursor isn't updated, so it only ever sits there fetching page 2 and never displaying the new data. |
A lot has changed with |
Have you ever been able to resolve this? |
Hey, We had the same issue and after investigating we found out that Meaning that it won't work with your If you can't change your schema, you might be able to manually overwrite the arguments passed to |
I created an issue to find a solution to this problem (maybe pass settings to |
Thanks @Nightbr! Cross-posting my response #8524 (comment) in case it's useful to others. In short: please feel free to copy/paste/tweak/reimplement |
Hi,
I am not sure what is the correct place to ask, i am confused sorry in advance, asked already here as well apollographql/apollo-link#1290
I'm using fetchmore updateQueries but getting warning in console that updateQueries is maybe depreciated.
İ have searched on documents enough but i don't see any example how to implement offset pagination without using updateQueries method.
Could you provide a bare simple example please
Many thanks in advance.
The text was updated successfully, but these errors were encountered: