Skip to content

Instantly share code, notes, and snippets.

@yann-yinn
Created April 1, 2018 18:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yann-yinn/5b2f038a198c5990ff415752ea368c4d to your computer and use it in GitHub Desktop.
Save yann-yinn/5b2f038a198c5990ff415752ea368c4d to your computer and use it in GitHub Desktop.
Get content from graphCMS with Next.js
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