The new Cosmic dashboard is here!
Test drive it now →

How to Build a Simple Blog Using React and GraphQL

Community Articles
Community Articles How to Build a Simple Blog Using React and GraphQL

In this tutorial I’m going to show you how to create a simple blog using React, GraphQL and Cosmic. This is going to be the fastest and most light-weight blog built on a modern technology stack.  Let’s get started.

TL;DR

View the demo
Install the Simple React Blog on Cosmic
View the codebase on GitHub

Getting Started

Make sure that you have Node.js and NPM installed on your machine, if not, visit the Node.js website to install the latest version.

Let’s start by creating a folder for our app. In your favorite terminal run the following commands:

mkdir simple-react-blog
cd simple-react-blog

Now let’s add a package.json file to import all of our dependencies for our app:

vim package.json

Add the following to our package.json file:


It’s a pretty light dependency list for a pretty light app. So what we will install is:

1. Axios for our promise-based HTTP client to get content from to the Cosmic GraphQL API.
2. Next.js as our React Universal framework.
3. Next routes for dynamic routes.
4. Express for our server-side web app framework.
5. React to handle our user interface.

Our scripts are necessary for starting our app in production and development.

Run the following command to install our dependencies:

npm i

Building Our Blog

Next, let’s begin building our blog pages. Create a pages folder and add the index.js file:

vim index.js

and add the following to index.js:

There are a few things happening here:

1. We are importing our essential modules: Axios, Lodash, and other helpers and components.
2. We are pulling in some partials: Header
and Footer, you can reference these partials from the codebase on GitHub.
3. We query the Cosmic GraphQL API to only return the stuff we need: _id, type_slug, slug, title, metadata and created_at.
4. We set the main props in our component to the `cosmic` object.  And use lodash to parse through Posts and Global Object types.
5. We return our posts array data and image URLs into our main blog feed.

Single Post Query

For our single post, we add a `post` property to our props.  Post is found by matching the query.slug to the Object slug:

Check out the full file on GitHub.

Conclusion

This is the abridged version of the Simple React Blog available for download in the Cosmic Apps page. The full codebase includes a single post page view as well as a page dedicated to each author’s posts. View the full codebase on GitHub and deploy this app in a few clicks from your Cosmic dashboard by installing the app to your Cosmic Bucket.

I hope you enjoyed this tutorial, if you have any questions reach out to us on Twitter and join our community on Slack.

You may also like


Hi, In this tutorial, we are going to create an application that filters twitter tweets by selecting a specific location on the map. We are going to use React and Cosmic. We will be using Cosmic npm package for saving and sorting the most popular searches in our application. Let's get started.
Add the power of Algolia search to your Bucket. This Extension enables you to sync your Cosmic Objects with Algolia, making your data searchable. Set up automatic syncing to automatically add / edit / remove content from Algolia. Follow the simple steps below to install and setup Algolia.
Cosmic makes it easy to manage content for your Bootstrap applications. In this blog we'll quickstart a Bootstrap Landing Page using the Cosmic CLI. This single page website landing page is built using Node.js and a minimal theme from Start Bootstrap.
Every blog needs a solid Content Management System (CMS). This allows content owners to control their content without having to hire a developer to make every single change to their site. Cosmic is a great choice for a CMS. In this how-to article, we’ll connect a React/Next.js app to Cosmic .
In this video, Cosmic co-founder Tony Spiro takes you on a tour of the new Cosmic CLI which allows you to do all of the awesome things you can with the Cosmic API, from the comfort of your command line interface.
In this tutorial I'm going to demonstrate how to build a photography portfolio website using React, Styled-Components and Cosmic. Let's get started.

Get Started with Cosmic

Build personal projects for free. Add your team at unbeatable prices.
Start Building Talk to Sales