I have implemented relayStylePagination()
according to the apollo docs(https://www.apollographql.com/docs/react/pagination/cursor-based/#relay-style-cursor-pagination) in the following way:
index.js:
const httpLink=new HttpLink({
uri:'https://api.github.com/graphql',
headers:{
authorization: 'Bearer -'
}
})
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
repositories:relayStylePagination()
},
},
},
});
const client=new ApolloClient({
link:httpLink,
cache
})
ReactDOM.render(
<ApolloProvider client={client}>
<React.StrictMode>
<App />
</React.StrictMode>
</ApolloProvider>,
document.getElementById('root')
);
App.js:
const App=()=> {
const {loading,error,data,fetchMore}=useQuery(GET_REPOSITORIES_OF_CURRENT_USER,{
variables:{login:"rwieruch"}
})
if (loading) return <h1>Loading...</h1>
if (error) return <p>Error...</p>
console.log(data.user.repositories.edges)
console.log(data)
const pageInfo=data.user.repositories.pageInfo
console.log(pageInfo)
return(
<div>
<RepositoryList repositories={data.user.repositories} onLoadMore={()=>
{return fetchMore({
variables:{
after: data.user.repositories.pageInfo.endCursor,
}
})}
}
/>
</div>
)
}
How the button is rendered in the Child component:
<button onClick={onLoadMore}>Hey</button>
And , finally the gql query:
const GET_REPOSITORIES_OF_CURRENT_USER = gql`
query getUser($login:String!,$after:String){
user (login:$login){
repositories(
first: 10,
after:$after
) {
edges {
node {
id
name
url
descriptionHTML
primaryLanguage {
name
}
owner {
login
url
}
stargazers {
totalCount
}
viewerHasStarred
watchers {
totalCount
}
viewerSubscription
}
}
pageInfo{
endCursor
hasNextPage
}
}
}
}
`;
The problem is that when I press the button with the onClick prop corresponding to fetchMore , nothing is fetched. Also, there are no errors in my console- it just doesn't do anything. Can you please let me know why? I have been trying to figure it out for hours now. Thank you!
onLoadMore
is actually called? You could do that by logging before the return statement. – CremonaCache data may be lost when replacing the user field of a Query object. To address this problem (which is not a bug in Apollo Client), either ensure all objects of type User have an ID or a custom merge function, or define a custom merge function for the Query.user field, so InMemoryCache can safely merge these objects:
I have added an id to the user query and the warning dissapeared , yet it still didnt fetchMore.. – Hainan