Make sure you have your Drupal site ready and your Gatsby development server running. Once you do that, open the GraphiQL by navigating to http://localhost:8000/___graphql. We are going to create a query that pulls in the last 10 articles with the most recent in front. We want the title, the image, the path alias, and the body text. For the body text, we want the processed body text, but also the summary text if it exists. Note: you will only see the summary option available if you add a summary to one of your articles on your Drupal site.

Now open up your code editor and create a new file in src/pages and call it articles.js. Enter the code below into the new file. This code creates a new Page component called Articles that takes in a data prop. This data prop is populated by the GraphQL query at the botton. This GraphQL query is what you created in the GraphiQL interface. The list of articles is then mapped to a new component called ArticlePreview that we have not created yet. We will create this next.
import React from "react";
import PropTypes from 'prop-types';
import { graphql } from 'gatsby';
import Layout from "../components/layout";
import SEO from "../components/seo";
import ArticlePreview from "../components/articlePreview";
const Articles = ({ data }) => {
const articles = data.allNodeArticle.nodes;
return (
<Layout>
<SEO title="Articles" />
<h1>Articles</h1>
{articles.map(article => (
<ArticlePreview
key={article.id}
title={article.title}
path={article.path.alias}
image={article.relationships.field_image.localFile.publicURL}
alt={article.field_image.alt}
summary={article.body.summary ? article.body.summary : article.body.processed.substring(0, 300)}
/>
))}
</Layout>
);
};
Articles.propTypes = {
data: PropTypes.object.isRequired,
};
export const data = graphql`
{
allNodeArticle(
sort: {fields: created, order: DESC}
limit: 10
) {
nodes {
id
title
created
body {
processed
summary
}
path {
alias
}
field_image {
alt
}
relationships {
field_image {
localFile {
publicURL
}
}
}
}
}
}
`;
export default Articles;
In the src/components folder create a file called articlePreview.js and enter the code below. This file is a simple React component that outputs a preview of the Article with a title that links off to the actual Article page.
import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'gatsby'
const ArticlePreview = ({ title, path, image, alt, summary }) => (
<div>
<Link to={path}>
<h2>{title}</h2>
</Link>
<img src={image} alt={alt} />
<div dangerouslySetInnerHTML={{__html: summary}} />
</div>
);
ArticlePreview.propTypes = {
title: PropTypes.string.isRequired,
path: PropTypes.string.isRequired,
image: PropTypes.string.isRequired,
alt: PropTypes.string.isRequired,
summary: PropTypes.string.isRequired,
};
export default ArticlePreview;
Now go ahead and try it out. You may find you need to stop and restart the Gatsby development server depending on if you have any errors in your code. Once you have it running you should be able to go to the /articles page and see a listing of the articles!

If you click on the title you will be taken to the full article page. You now have a somewhat working blog site on Gatsby! Next time we will look at the Gatsby Image component to improve how our images are handled and in the future we will look at styling in Gatsby.