import { Query } from 'react-apollo' import gql from 'graphql-tag' import ErrorMessage from './ErrorMessage' import PostUpvoter from './PostUpvoter' export const allPostsQuery = gql` query allPosts($first: Int!, $skip: Int!) { allPosts(orderBy: createdAt_DESC, first: $first, skip: $skip) { id title votes url createdAt } _allPostsMeta { count } } ` export const allPostsQueryVars = { skip: 0, first: 10 } export default function PostList () { return ( {({ loading, error, data: { allPosts, _allPostsMeta }, fetchMore }) => { if (error) return if (loading) return
Loading
const areMorePosts = allPosts.length < _allPostsMeta.count return (
    {allPosts.map((post, index) => (
  • {index + 1}. {post.title}
  • ))}
{areMorePosts ? ( ) : ( '' )}
) }}
) } function loadMorePosts (allPosts, fetchMore) { fetchMore({ variables: { skip: allPosts.length }, updateQuery: (previousResult, { fetchMoreResult }) => { if (!fetchMoreResult) { return previousResult } return Object.assign({}, previousResult, { // Append the new posts results to the old one allPosts: [...previousResult.allPosts, ...fetchMoreResult.allPosts] }) } }) }