Skip to content

Instantly share code, notes, and snippets.

@zoonderkins
Created September 14, 2019 15:34
Show Gist options
  • Save zoonderkins/57b7f219f30c81e26bde4afff16693c6 to your computer and use it in GitHub Desktop.
Save zoonderkins/57b7f219f30c81e26bde4afff16693c6 to your computer and use it in GitHub Desktop.
Fix-Home-JS-classed-React GraphQL App (MERNG): #3 Displaying Posts
import React from 'react'
import { useQuery } from '@apollo/react-hooks'
import gql from 'graphql-tag'
import { Grid, Divider } from 'semantic-ui-react'
import PostCard from '../components/PostCard'
function Home() {
let posts = ''
const { loading, data } = useQuery(FETCH_POSTS_QUERY)
console.log(`Loading: ${loading}`)
console.log(data)
if (data) {
posts = { data: data.getPosts }
}
return (
<Grid columns={3}>
<Grid.Row className="page-title">
<h1>Recent Posts</h1>
</Grid.Row>
{loading ? (
<h1>Loading posts..</h1>
) : (
posts.data &&
posts.data.map(post => (
<Grid.Column key={post.id} style={{ marginBottom: 20 }}>
<PostCard post={post} />
</Grid.Column>
))
)}
<Grid.Row></Grid.Row>
</Grid>
)
}
const FETCH_POSTS_QUERY = gql`
{
getPosts {
id
body
createdAt
username
likeCount
likes {
username
}
commentCount
comments {
id
username
createdAt
body
}
}
}
`
export default Home
@mprkatti
Copy link

thank you. helped break the stalemate.

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