Skip to content

Instantly share code, notes, and snippets.

@kkemple
Last active June 10, 2021 10:57
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kkemple/bececb840c99b47a2e25f8086f15f056 to your computer and use it in GitHub Desktop.
Save kkemple/bececb840c99b47a2e25f8086f15f056 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import { gql } from 'graphql-tools';
const QUERY = gql`
query MatchesByDate($date: Int) {
matches(date: $date) {
id
minute
period
highlights {
...
}
...
}
}
`;
class MatchList extends Component {
render() {
const ({ data: { loading, errors, matches } }) = this.props;
return (
<div className="matchlist">
{loading && <div className="loader" />}
{errors && <div className="errors">...</div>}
{!loading && !matches && <span className="none">No Matches Found!</span>}
{!loading &&
matches &&
matches.map(match => <div className="match">...</div>)}
{!loading &&
matches &&
<button onClick={this.onFetchMore}>
Fetch More Matches
</button>}
</div>
);
}
onFetchMore = () => {
const ({ data: { matches, fetchMore } }) = this.props;
fetchMore({
variables: { date: matches[matches.length - 1].date },
updateQuery: (previousResult, { fetchMoreResult, queryVariables }) => {
return {
...previousResult,
// Add the new matches data to the end of the old matches data.
matches: [
...previousResult.matches,
...fetchMoreResult.matches,
],
};
},
});
}
}
export default graphql(QUERY)(MatchList);
@m1m6
Copy link

m1m6 commented Feb 19, 2020

can u please share your setup?
in my case the previousResult return the same data as in fetchMoreResult, in other words, the previous results aren't stored.

@bipinrajbhar
Copy link

Thank You So Much It Helped Me A Lot In My Project :)

@yeomann
Copy link

yeomann commented Jun 10, 2021

can u please share your setup?
in my case the previousResult return the same data as in fetchMoreResult, in other words, the previous results aren't stored.

i have same problem, did you find solution and what was causing this problem? @m1m6

@mjbour
Copy link

mjbour commented Jun 10, 2021

can u please share your setup?
in my case the previousResult return the same data as in fetchMoreResult, in other words, the previous results aren't stored.

i have same problem, did you find solution and what was causing this problem? @m1m6

Check your fetch-policy configurations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment