Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo 3 pagination with Field Policies

Could someone provide an example of pagination implemented with Apollo Client 3.0 Field Policies. I've been following the example from the docs to implement infinite scroll but in my console I'm getting the following warning:

The updateQuery callback for fetchMore is deprecated, and will be removed in the next major version of Apollo Client.  Please convert updateQuery functions to field policies with appropriate read and merge functions, or use/adapt a helper function (such as concatPagination, offsetLimitPagination, or relayStylePagination) from @apollo/client/utilities.  The field policy system handles pagination more effectively than a hand-written updateQuery function, and you only need to define the policy once, rather than every time you call fetchMore. 

I'm fairly new to Apollo and I don't really get how to do that the 3.0 way. I would appreciate some examples to get better understanding.

Here is my current code:

import React from "react"; import { useGetUsersQuery } from "./generated/graphql"; import { Waypoint } from "react-waypoint";  const App = () => {   const { data, loading, error, fetchMore } = useGetUsersQuery({     variables: { limit: 20, offset: 0 },   });    if (loading) return <div>Loading...</div>;   if (error) return <div>Error</div>;    return (     <div className="App">       {data && data.users && (         <div>           {data.users.map((user, i) => {             return (               <div key={i} style={{ margin: "20px 0" }}>                 <div>{user.id}</div>                 <div>{user.name}</div>               </div>             );           })}           <Waypoint             onEnter={() => {               fetchMore({                 variables: { offset: data.users.length },                 updateQuery: (prev, { fetchMoreResult }) => {                   console.log("called");                   if (!fetchMoreResult) return prev;                   return Object.assign({}, prev, {                     users: [...prev.users, fetchMoreResult.users],                   });                 },               });             }}           />         </div>       )}     </div>   ); };  export default App; 
like image 961
Oleksandr Fomin Avatar asked Jul 05 '20 15:07

Oleksandr Fomin


People also ask

Is it possible to implement a pagination feature in Express Apollo server?

Pagination is the most common solution to this problem, and Apollo Client has built-in functionality that makes it quite easy to do. There are basically two ways of fetching paginated data: numbered pages, and cursors. There are also two ways for displaying paginated data: discrete pages, and infinite scrolling.

What is the difference between useQuery and useLazyQuery?

Unlike with useQuery , when you call useLazyQuery , it does not immediately execute its associated query. Instead, it returns a query function in its result tuple that you call whenever you're ready to execute the query.

What is offset based pagination?

APIs that use offset-based paging use the offset and limit query parameters to paginate through items in a collection. Offset-based pagination is often used where the list of items is of a fixed and predetermined length.

What is Refetch useQuery?

refetch() The fetchApi() function in our useQuery Hook is the function we'll want to run again if we needed to refetch query information and update state. In the useQuery. ts file, we'll restructure how our Hook is set up to have the fetchApi() function be returned from the Hook.


1 Answers

Remove updateQuery callback function completely:

fetchMore({ variables: { offset: data.users.length } }); 

And change your cache to:

import { offsetLimitPagination } from "@apollo/client/utilities";  const cache = new InMemoryCache({   typePolicies: {     Query: {       fields: {         users: offsetLimitPagination(),       },     },   }, }); 

So your query in qraphql must have offset and limit arguments.

Other options are: concatPagination and relayStylePagination

If you need to distinguish different request to same field users ex. put keyArg: offsetLimitPagination(["filters"]) and query your users with filters arg. Cache will store it separately.

More info in official release

like image 122
unlimittt Avatar answered Sep 22 '22 09:09

unlimittt