Created
April 1, 2018 18:51
-
-
Save yann-yinn/5b2f038a198c5990ff415752ea368c4d to your computer and use it in GitHub Desktop.
Get content from graphCMS with Next.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 from "react"; | |
import { request, GraphQLClient } from "graphql-request"; | |
export default class HomePage extends React.Component { | |
static async getInitialProps() { | |
const query = `{ | |
allPosts { | |
id | |
slug | |
coverImage { | |
url | |
} | |
title | |
content | |
authors { | |
id | |
name | |
} | |
} | |
} | |
`; | |
return request( | |
"https://api.graphcms.com/simple/v1/cjfgpbd8s1bj101427u4eu2b0", | |
query | |
); | |
} | |
render() { | |
return ( | |
<div className="container section"> | |
{this.props.allPosts.map((post, index) => ( | |
<div> | |
{post.coverImage.url && ( | |
<img height="200px" src={post.coverImage.url} /> | |
)} | |
<h2>{post.title}</h2> | |
<p>{post.content.substr(0, 500)} ... </p> | |
</div> | |
))} | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment