I’m trying to implement the Offset Pagination in React using Apollo GraphQL. I’m using the fetchMore() function against a button click to show the updated data but the data is not being updated nor the component rerenders.
Things I have done so far by following the official documentation of Apollo GraphQL:
Added the field policy to the App.js file
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
missions: offsetLimitPagination()
},
},
},
}),
Query to call public GraphQL API (GraphiQL Explorer) which takes limit and offset as input and is successfully returning the expected data
import { gql } from "@apollo/client"
export const GET_ITEMS = gql`
query Missions($offset: Int, $limit: Int){
missions(offset: $offset, limit: $limit){
id
manufacturers
}
}
`
And calling the query in my component which successfully returns the expected data
const {data, error, loading, fetchMore} = useQuery(GET_ITEMS, {
variables: {
offset: 0,
limit: 3
}
})
Providing new variables to fetchMore function on a button click but it doesnt update the data:
<button onClick={() => {
fetchMore({
offset: data.missions.length
})
console.log(data.missions.length)
}} >Show Items</button>
I don’t know what I’m missing here. I’ve a very tight deadline to make this pagination happen. I’m pretty tensed. I can even pay you to let me know what I’m missing here.