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

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 JS. 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 JS
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 JS 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 JS 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 JS 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 JS dashboard by installing the app to your Cosmic JS Bucket.

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

Was this article helpful?

Thank you! Your input helps us improve our articles and resources. If you have any other questions or comments, please contact support.

You may also like


In this tutorial I’ll demonstrate how to build an Authentication app using Angular 6 and Cosmic JS. Starting from scratch, learn how to build, configure and deploy this Angular6 Auth app to the Cosmic JS App Server for testing purposes.
Cosmic JS provides tools and resources to create your own unique content publishing workflow that satisfies your team's needs. In this tutorial I'll demonstrate setting up a simple workflow for an Editor and Contributor team that needs to manage content in a draft, review and publish process.
Single page app that allows you to showcase you digital products and accepts credit card payment online. Very similar to the marketing sales funnel, but done in simple single page sales funnel.
Grab and save content from any website to your Cosmic JS Bucket.
Cosmic JS makes it easy to deploy your websites and applications to the web. You can deploy an app from any git repository to the Cosmic App Server from any Bucket. Cosmic JS uses Dokku (Docker + Heroku-like deployment) to deploy your app from any GitHub, BitBucket or any other public repo.
This is a quick example to show some of the powerful things you can do with the Cosmic JS NPM Module. This script will, add a Bucket to your Cosmic JS account, add an Object Type (Pages) and add a few Pages.

Ready to Get Started?

No payment info required.
Start Now   Contact Sales