by Charles Ouellet
Why headless CMS and GraphQL are a natural fit
Bonus: Takeshape Demo
The first time we wrote about GraphQL, back in 2017, we described it as a shiny new tool developed by Facebook.
GraphCMS, which we presented in that article, was one of the first softwares to use it as a core feature.
It was just another drop in a sea of development tools. No biggie, right?
I think we can all agree that GraphQL is now integral to the modern web ecosystem.
Proof #1: the number of static site generators and headless CMSs making it a prime built-in feature has been skyrocketing in the last few months.
One of them being TakeShape.io, which I’ll explore further in a technical demo at the end of this post.
First, I want to take a look at:
- The state of GraphQL in 2019
- The benefits it brings to headless CMSs
- The tools that feature GraphQL
- An introduction to TakeShape.io
Let’s begin by wrapping our heads around the concepts behind GraphQL.
1. Getting up-to-date with GraphQL
You may be reading without even knowing what GraphQL really is. For clarity’s sake, it’s a query language for APIs that defines how to fetch data from one or many databases.
For a proper technical GraphQL introduction I suggest our first post on the matter.
A lot has changed since its first use case with FB mobile apps back in 2012, mostly regarding its adoption. GraphQL was open-sourced in 2015, giving it an initial burst of life for a larger number of devs. 4 years later, its rising presence in our industry is undeniable.
Proof #2 that GraphQL is an integral part of web development: its adoption by tech giants. Apart from Facebook, other major players like GitHub, Shopify, Pinterest, Paypal & Twitter all use GraphQL in some way or another.
Impressively enough, it fits directly in the adoption timeline that Lee Byron, GraphQL’s creator, had in mind a few years ago. The next step according to him? Omnipresence in web platforms. We have every reason to believe him.
Prepare to hear about GraphQL a lot in 2019. Many publications rank it as one of the most important JS trends to keep an eye on in the next few months. Npm itself says that you’ll need to learn it in 2019, & GraphQL experts are already drooling thinking about the bright future of the tech.
But… wait a second here.
Hype is one thing, but one of my colleagues said something about it that I found pretty interesting:
It’s true that if intensive public talk can sometimes oversell something (that Birdbox movie, anyone?) it can also overshadow its initial merits.
For us, these pillars for GraphQL are:
- The GraphQL specification has been developed as a way to define single highly flexible API endpoints giving developers access to complete relational models.
- It is incredibly powerful because it gives consumers total control over how they want the requested data to be structured, as well as how deep they want to go into entity relations.
In the same vein, a GitHub engineer explains why he can’t wait for GraphQL to be “uncool” in this episode of JAMstack Radio.
What about that old GraphQL vs. REST talk?
I won’t lose myself too long here because it’s also a topic we discussed in our first GraphQL piece and the debate hasn’t really changed.
Of course, the rising GraphQL adoption is forcing us to keep this discussion going, but REST APIs are still alive & well and not under any threat of extinction, for now. You can’t put them aside and say that one is the other’s predecessor because they’re simply not of the same nature. They can live together just fine in the same environment.
Still, GraphQL comes as an upgrade for certain functions that weren’t optimal with REST APIs.
2. How headless CMSs are leveraging GraphQL
If you’ve paid attention in the last few months, you might have observed a lot of tools adding GraphQL as a built-in feature, or new ones breaking into the market with GraphQL as a selling point.
This list includes:
- Gatsby (SSG) — The popular React-powered static site generator leverages GraphQL to fetch data from multiple sources.
- Gridsome (SSG) — The Vue.js equivalent to Gatsby, also featuring GraphQL.
- GraphCMS — The name says it all. It was probably the first headless CMS to put GraphQL forward.
- Strapi — This neat Node.js headless CMS lets you use REST or GraphQL APIs.
- Unite CMS — Still in beta, this new headless CMS is built on Symfony 4 & GraphQL.
- CannerCMS — An open-source API-driven headless CMS.
- TakeShape — More on this new headless CMS down below.
In this post, I want to focus solely on the headless CMSs in this list. More precisely, how GraphQL is making them better and worthy of your attention.
There are 4 main advantages that I think it’s important to highlight:
→ Better API to retrieve data: The main premise of GraphQL is to offer the capacity for devs to chose exactly what they need from the database and receive that data in a predictable way. Not only that, but you can fetch many resources in a single request. Say goodbye to over bloated server/database requests.
→ Possibility to insert data from external applications: Isn’t it what APIs have always been used for? Sure, but once again GraphQL makes this process more structured and organized. One great thing that headless CMSs enable is modular development. So, when scaling an app you’ll need to plug some external functionalities and GraphQL assures that you’ll only get what you really need from each of them.
→ Great performances: Smaller, more efficient requests to databases and external apps will no doubt be beneficial for your system’s performance.
→ Easy delivery to multiple channels: GraphQL is embracing modern trends. With the rise of mobile, wearable technologies & IoT it’s becoming important to develop APIs that can easily serve multiple channels.
It’s time to move from theory to practice, with TakeShape.io as our guinea pig.
3. What is TakeShape.io?
TakeShape is a new JAMstack-oriented tool that combines the functionalities of a headless CMS, GraphQL API and static site generator.
It’s only a few months old since it was officially launched in September 2018. It caught our attention thanks to their JAMstack-related content and we’ve been eager to try it ever since.
It was built with the premise that “content management shouldn’t be hard”. TakeShape was crafted with developers in mind, giving them a frictionless framework to create projects on. But content crafters won’t be left in the dark, thanks to an intuitive interface and the necessary features to manage content efficiently.
It fully embraces the latest modern development trends, making the best of the built-in GraphQL API and even offering its own static site generator. But, don’t worry, as with every good headless CMS, you can also attach to it the frontend tech of your choice.
The founders also seem to be all-around good guys from the small discussion we’ve had with them prior to building this demo, so I’m really happy to finally dive into it!
4. Technical tutorial: Building an app with headless CMS & GraphQL
The idea behind this demo comes from a therapeutic practice that emphasizes the importance to list things you’re grateful for, every day. I’ll build a small app that will allow users to send entries to what I’ll call our Gratitude Journal. I’ll then use GraphQL to fetch the data submitted in the form and publish to a website that will showcase these entries.
Basically, I’m going to use TakeShape to build the website and store the data. I’ll then use Netlify functions to add entries to the GraphQL database with TakeShape API. Netlify functions will act as a kind of proxy to make sure the API keys aren’t exposed to the wild and that the entry I’ll insert into my database is in the right state.
Let’s do this!
4.1 Setting up TakeShape
First things first, you need to setup your TakeShape project.
Once your account is created and you’re logged in, you’ll be ready to create your first content type. A content type in TakeShape is the representation of any object or document in your application.
For example, if you were building a classic e-commerce project, you could have a content type for your products, another content type for the product categories and so on. You can then very easily define relationships between your entries.
I suggest watching the introduction video here. It lasts about 20 minutes and you should get everything you need to know to get started with TakeShape.
You’ll end up with a GraphQL database that can be queried directly into the API explorer in the TakeShape app or via their API endpoint. This is a very easy way to get started with GraphQL. For this case, create two content types.
The first one will be for the messages people can submit on the site. Let’s call this content type:
You can define your fields by drag-and-dropping the different controls available in the right side panel in TakeShape app. In this case, use a single line to represent the message. Please note that I used Multiple as Annotation value so we can create more than one entry.
Create the second content type, which will be
Home. This will be useful since you want your marketing team to have control over the wording on the homepage. This is where they’ll edit the content. This is going to be a Single entry since it can be added only once (there will be only one homepage).
Easy, wasn’t it? Your CMS is ready now, you only need to build the website and the function that will add new entries to your GraphQL database.
Time to dive into the code!
4.2 Creating the project
The code for TakeShape templates and the upcoming Netlify function will reside in the same Git repository.
You’ll need to use TakeShape CLI tools to initialize the project. So you can install it via npm.
npm install takeshape-cli -g
Create a new folder, and in this folder start a new npm project:
Follow the steps to create your project as usual.
Then, you’ll need to initialize the TakeShape project:
Follow the steps by entering your TakeShape credentials and, you’ll be prompted to answer a couple of questions. Their CLI is well made so this shouldn’t be complicated.
You should now have a couple of files in your folder. As suggested by TakeShape CLI, write a
.gitignore file in the root of the folder and add some files to ignore in there:
Then, you can initialize a new Git repository:
4.3 Adding function for the GraphQL database
It’s now time to generate the Netlify function that will be responsible for submitting new entries to TakeShape.
As mentioned earlier, one of the cool things about a GraphQL-powered CMS is that inserting new data is as simple as querying. This isn’t something you’ll often see in demos like this. Most of the time you’ll just gather the data with some queries and display it. In this case, you’ll use GraphQL mutations to create new entries.
Let’s start by adding a few dependencies again. You’ll need to install
netlify-lambda npm package which will be used during the build process. This is the CLI tool created by Netlify to compile and test functions locally:
npm install netlify-lambda --save-dev
node-fetch to interact with TakeShape API:
npm install node-fetch --save
Create a new file at the root of your project named netlify.toml. That’s where you’ll specify where the functions will be located in the project. The Netlify build process will use this file to deploy everything correctly.
The functions will need to be compiled into the build/functions folder. Otherwise, Netlify won’t see them and they won’t be available. You’ll need to use
netlify-lambda CLI tool to compile your function. The
build function is going to be helpful here, this function awaits one parameter that is the path to the uncompiled functions; your source code.
Open your package.json file and add a new script named build.
This is what you’re going to use in Netlify as the build command.
You’ll need two standalone Netlify sites for this demo. One that will be responsible to host your functions (this is going to be an API, in a way) and the other will be the output from TakeShape, the public website. I’ll come back to this later.
For now, create your function. Create a new folder:
src/functions at the root of your project. Then create a file named
add-entry.js in it.
You’ll need to run a mutation in this function using TakeShape API. It’ll look like this:
Basically, you’ll write a new entry in TakeShape’s GraphQL datasource that isn’t enabled by default. Your site administrators will therefore be able to review the messages before publishing them on the public website.
Here’s the code for the function itself:
Later on, you’ll need to configure some environment variables on Netlify’s side to store TakeShape’s API key and Project ID so we don’t commit them on GitHub. This API key would allow anyone to insert or delete data from your GraphQL database. You really don’t want this shared externally.
Now, commit these files. Using Github, create a new repository and push the project.
Then, open your Netlify account and create a new site from this repository.
In your site settings, go to Build & Deploy and scroll down to the Build environment variables section.
You’ll need to add two environment variables:
Now, in your TakeShape dashboard, you’ll need to craft a new API key with Read/Write permissions.
Make sure to note the API key value since you won’t be able to view it anymore. Use this value to specify the TAKESHAPE_API_KEY environment variable in Netlify’s dashboard.
To get your project ID, look at the URL in TakeShape’s dashboard (it’s the guid in it).
With this, your function should now be working as expected and you should be able to submit new entries to TakeShape CMS. You might need to redeploy your site in Netlify, though, to make sure your function is aware of new environment variables.
To test it, you can use an HTTP client such as Postman or Insomnia.
4.4 Building the site
Now that your API is ready, you can build your website. Awesome.
When I started playing around TakeShape in preparation for this demo, I wasn’t even aware that they had their own static site generation feature. That’s really cool in my opinion. It’s solid and enables a quick start for small projects, so that’s what I’m using here.
Make sure that the project you were working on is still open in your favorite code editor. Next thing to do is building the TakeShape config file. At the root of your project, create a new file named tsg.yml. This is where you’ll create your routes and map the templates/queries to them.
Here’s what’s the config looks like:
What does it mean? You’ll have a single route, the homepage, that will render the homepage.html template and use the data coming from the homepage.graphql query.
Each page specified in the routes array consist of three elements:
- The path, which is the URL of the page
- The template, which will be the view itself, the HTML markup
- And the context, which will be the GraphQL query that will run and populate the view model.
Once you start working with it a bit, you quickly realize it makes a lot of sense.
Also specified in there is the
Start by creating our query file. Generate a file named
Your query will return the list of entries and the homepage details. You’ll have access to all this information in your template file.
Let’s create a file named
Noticed the app.js file just before the closing
body tag? This is where you’ll add the code that will interact with your Netlify function. Build this file in a new folder named
static at the root of your project:
There’s also a file named
site.css that is included in the header of the site. Create this file in the
static folder and just like
app.js (please refer to this file on Github), this asset will be deployed and can be included it directly into your markup.
Essentially, you’re going to catch the form submit event, and make your request to the API. That’s pretty straightforward.
Now, you can test it locally using TakeShape tooling. In a command prompt, navigate to your project and use the watch task:
Your project will then be compiled into the build folder, which was configured in the
tsg.yml file. If you start a web server on this path, you should be able to see your site.
4.5 Deploying the website
Lastly, you need to deploy the site. TakeShape has plenty of deployment options. In this case, I’ll stick to Netlify since it’s easy to use and I’m already relying on their platform for the API.
Open your project settings:
Then, you’ll see the Netlify in the Integrations panel. Connect your Netlify account.
Next, you’ll need to create a Static site in TakeShape:
Fill the form by selecting Netlify as provider. You’ll be able to generate a new Netlify site directly from this form.
Now, the deployment.
It’s very simple using TakeShape CLI. Personally, I like to deploy my sites using the site ID. You can find it in your browser URL when you’re in the edit form of the static site:
Then, in your terminal:
tsg deploy --site <site_id>
Bing! Your website should now be live, and the API should work as expected.
Live demo & GitHub repo
In case you were wondering how to manually monitor and accept the messages submitted in the form by users, this GIF shows you the way:
See the live demo here
See the GitHub repo here
5. Closing thoughts
Proof #3 that GraphQL is now integral to web development: It’s awesome and integrates so well into modern dev workflows. It’s hard not going back to it once you’ve tasted it.
I had a blast using TakeShape and Netlify functions. These tools make it possible to create full-blown applications without having to deal with servers and infrastructure. You know that’s what we’re all about.
The TakeShape team really did a solid job with their application. Their dashboard is clear and simple, their API is exactly how we expect it to be and building relationships between content types is totally straightforward.
I spent more or less a day putting everything together. I didn’t hit any major hiccups and it was a pretty smooth development ride all around.
I didn’t feel the need to push anything further for this demo. It was meant to be pretty simple and to show how we can insert data into the CMS programmatically. I think this stack would be a great fit for a blog or any content-driven application.
I would also love to eventually use TakeShape for a straight-up e-commerce integration with Snipcart, so I’m saving this on the roadmap! ;)
Any questions? Feel free to hit comments to give us your thoughts, feedback, and questions. If you enjoyed this post, take a second to 👏 or share on Twitter!