Created
June 23, 2018 05:05
-
-
Save alfianlosari/6a89d7a8dc5ebe9fd2b65d227eb44d0d to your computer and use it in GitHub Desktop.
Apollo Github Pagination App.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { Query } from "react-apollo"; | |
import "./App.css"; | |
import Repos from "./components/repos"; | |
import { trendingRepositoriesGQLQuery } from "./data/query"; | |
import moment from "moment"; | |
class App extends Component { | |
render() { | |
const date = new moment(new Date()).subtract(1, "weeks"); | |
const formattedDate = date.format("YYYY-MM-DD"); | |
const query = `created:>${formattedDate} sort:stars-desc`; | |
return ( | |
<div> | |
<h1>Last Week Trending Repositories</h1> | |
<Query | |
notifyOnNetworkStatusChange={true} | |
query={trendingRepositoriesGQLQuery} | |
variables={{ | |
query | |
}} | |
> | |
{({ data, loading, error, fetchMore }) => { | |
if (error) return <p>{error.message}</p>; | |
const search = data.search; | |
return ( | |
<Repos | |
loading={loading} | |
entries={search} | |
onLoadMore={() => | |
fetchMore({ | |
variables: { | |
query, | |
cursor: search.pageInfo.endCursor | |
}, | |
updateQuery: (prevResult, { fetchMoreResult }) => { | |
const newEdges = fetchMoreResult.search.edges; | |
const pageInfo = fetchMoreResult.search.pageInfo; | |
return newEdges.length | |
? { | |
search: { | |
__typename: prevResult.search.__typename, | |
edges: [...prevResult.search.edges, ...newEdges], | |
pageInfo | |
} | |
} | |
: prevResult; | |
} | |
}) | |
} | |
/> | |
); | |
}} | |
</Query> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment