EXPLORE
Home
About us
Case Studies
Thoughts
Contact
SOCIAL
Since the release of Craft 3.3, which introduced ‘headless mode’ and GraphQL integration, building a headless craft website is now much more feasible. Going headless is becoming more desired for the flexibility, speed, and the security it adds to a website over a traditional CMS. GraphQL, which was developed by Facebook, is a query language for API’s and responds with all the requested content that can be manipulated and rendered in your Front-End Library of choice.
The first step in building a headless Craft CMS website is to create a GraphQL schema and generate a private token. In order to do this, navigate to GraphQL within Craft, and then into schemas where you name the schema, set its privacy, and can copy the generated token. Then create a .env file to hold these secrets:
GRAPHQL_TOKEN=[token]; GRAPHQL_URL=http://example.com/api;
Then the next step is to decide which Front-End Static Site Generator to use. A few examples of Static Site Generators which are all open-source are Nuxt.js (used with Vue), Next.js, and Gatsby.js This headless Craft tutorial will be going over how to integrate Gatsby.js, which is React-based, to our headless Craft CMS site. Gatsby.js is high-preformative and connects well with GraphQL.
npm install -g gatsby-cli;
Then create a new project: gatsby new project-name
Next, cd into that project and install the Gatsby’s GraphQL plugin:
npm install --save gatsby-source-craft;
Require the ‘dotenv’ package at the top of gatsby-config.js and add the source:
plugins: [ { resolve: 'gatsby-source-craft', options: { craftGqlToken: '${process.env.GRAPHQL_TOKEN}', craftGqlUrl: '${process.env.GRAPHQL_URL}' } ]
That’s all that is required for configuring a headless CMS website with Craft, GraphQL, and Gatsby.js! The next step is to create a query to pull data from your newly created API. A very general query that would just return the title of the site would be:
const data = useStaticQuery(graphql' query TitleQuery { site { siteMetadata { title } } } ')
useStaticQuery is the React-Hook form of <StaticQuery /> which we import from Gatsby. Static Queries can be used on data that won’t be dynamic, like the metadata of a site. Now that we have our query created, we can use it to fetch the title of our site. In our Index component (/src/components/index.js), we can display the title of our site like so:
import React from "react"; import { useStaticQuery, graphql } from "gatsby"; export default function Header() { const data = useStaticQuery(graphql' query TitleQuery { site { siteMetadata { title } } } ‘) return ( <header> <h1>{data.site.siteMetadata.title}</h1> </header> )}
Running `gatsby-develop` will start running the app on your local server at http://localhost:8000
And provide you with a second link http://localhost:8000/___graphql which can be used to test your queries on a local GraphQL server.
The initial site will look like this:
But with the ability to pull in all of your content from Craft, this default starter page can turn into anything! It was just that straightforward to create a headless Craft CMS website using React.js, GraphQL, and Gatsby.js. For how easy and painless setting up was, and all the other advantages going headless provides, there is not much reason not to!
Schedule a free strategy session.
This call is about your goals, not sales pitches. No pressure, just clarity.
You’ll walk away with practical insights—even if you don’t hire us.
Get your fixed-price proposal quickly, so you can make an informed decision fast.
Their initial website creative inspired us and brought us down a new path as a brand and led us to take on a new strategy. These guys are constantly looking to improve our site and help us in any way possible
I had the pleasure of working with the Electric Enjin team for over three years and can confidently say that they are a top-notch digital agency.
Whether you’re looking to launch or evolve your brand, Electric Enjin brings the expertise to realize scale and growth through creative/UX transformations and innovation.