-
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
What kind of Apollo Client Type Policy for infinite scrolling and paged search both? #8468
Comments
@sssmi I appreciate your clever use of a dynamic function for Here's some intermediate/advanced documentation about how I/we think about More specifically, I would guess your |
By the way : I feel like when an alias is setup on a query, apollo should use it instead of the original query name. |
@benjamin, Working on a simulair thing. We have an interface with sliders (like netflix) and we have the merge part workign using offset pagination in combination with some keyargs ( which are very unintuetive to say the least) We all our items live in an item table in the DB, they are differentieted by item_type, show_data, Date_added. and we query with offset and limit. Like described, we have pagination working as in a slider fetches more data when it hits the end. However, if i navigate from tha page and navigate back with cache still there.... it displays all items in one go, as in all of the data that has been fetched before. Shoudl we use keyargs, or should we build some sort of custom read function. @onair-lena you might want to track this thread ;) |
We have a similar problem. Depending on the viewport, we are using an infinite scroll (with
typePolicies: {
Query: {
fields: {
search: {
keyArgs: ['query'],
merge(existing, incoming, { args: { offset }}) {
// similar to https://github.com/apollographql/apollo-client/blob/5d0c881a4c20c5c48842d9a8d246d26a917eccf9/src/utilities/policies/pagination.ts#L33
},
read(existing, { args: { offset, limit }}) {
// how do we know here if we need to return the whole list or slice it?
},
},
},
},
};
query searchPaginated($query: String!, $offset: Int){
search(
query: $query
offset: $offset
limit: 10
) @connection(key: "searchPaginated", filter: ["query", "offset"]) {
...Result
}
}
query searchInfinite($query: String!, $offset: Int) {
search(
query: $query
offset: $offset
limit: 10
) @connection(key: "searchInfinite", filter: ["query"]) {
...Result
}
} and in typePolicies: {
Query: {
fields: {
search: {
merge(existing, incoming, { args: { offset }}) {
// similar to https://github.com/apollographql/apollo-client/blob/5d0c881a4c20c5c48842d9a8d246d26a917eccf9/src/utilities/policies/pagination.ts#L33
},
},
},
},
}; |
I honestly think returning everything from the This difference between paginated and non-paginated If you're not sold on the one-big-list philosophy, and you want to keep using Since While new InMemoryCache({
typePolicies: {
Query: {
fields: {
search: {
keyArgs: ["query", "@connection", ["key"]],
merge(...) {...},
},
},
},
},
}) This configuration will produce field keys like |
i tried to use @connection directive as below and it returns me an error saying "Variable "$key" is never used in operation" . Can someone help with this? query searchPaginated($query: String!, $offset: Int, $key: String = "key"){ |
Any resolution to this? Why can't Apollo be updated to give access to the name of the custom query, seems like pretty important functionality for uses cases like what @simeonkerkola is doing. If an Apollo user ever wants to store data in the cache differently for any two custom queries then it more or less seems like that behavior is not supported by Apollo, because the only workable solution is hacking something together with @connection. This is basic functionality. |
I recently updated the apollo client package in my work to use version 3, and some things I can’t quite grasp just yet. Like defining type policy globally per query name.
Here’s my issue, at work we have a search query, an offset based one, and it’s used in very different
UI contexts. We have views that are table like with lots of paged data, and then we have infinite scroll type of feeds.
I have found that a single type policy for query search does not really quite support this. Here is our type policies:
The hacky part here is that we are using field alias:
pagedSearch: search(input: $input from: $from)
for search query and pushing offset variable to the keyArgs of paged queries, so user gets fresh results when they change the table’s page.if (context.field?.alias?.value === 'pagedSearch') { keyArgs.push('from'); }
This does get the job done for now, but I want to know how would one implement a single type policy for these type of queries?
I have asked this question on stack overflow a while a go, but no answers yet
https://stackoverflow.com/questions/68157953/what-kind-of-apollo-client-type-policy-for-infinite-scrolling-and-paged-results
The text was updated successfully, but these errors were encountered: