Gatsby integration
Gatsby is a powerful static site generator, and integrating it with your CMS allows you to leverage your existing content and build blazing-fast websites. The gatsby-source-decoupla plugin provides a seamless way to fetch data from your Decoupla CMS and use it within your Gatsby project.
This article will guide you through the process of setting up and using gatsby-source-decoupla to pull content from your Decoupla CMS into your Gatsby site.
Prerequisites
- A Gatsby project (if you don't have one, you can create one using
gatsby new my-site) - A Decoupla CMS account with a workspace and content
- Your Decoupla Workspace ID and API tokens (Live and Preview)
Installation
-
Install the plugin:
npm install gatsby-source-decoupla
Configuration
-
Configure
gatsby-config.js:Open your
gatsby-config.jsfile and add thegatsby-source-decouplaplugin to thepluginsarray. You'll need to provide your Decoupla Workspace ID and API token in theoptionsobject.module.exports = { plugins: [ { resolve: `gatsby-source-decoupla`, options: { workspace: `your_workspace_id`, token: `your_token`, // Use your Live or Preview Token. }, }, ], };Important:
- Replace
your_workspace_idwith your actual Workspace ID. - Replace
your_tokenwith your Live API token for production builds or your Preview API token for development and preview builds. You can find these tokens in the API settings of your Decoupla dashboard. - It is highly recommended to store your API tokens as environment variables rather than directly in your
gatsby-config.jsfile for security reasons.
Example using environment variables:
require("dotenv").config({ path: `.env.${process.env.NODE_ENV}`, }) module.exports = { plugins: [ { resolve: `gatsby-source-decoupla`, options: { workspace: process.env.DECOUPLA_WORKSPACE_ID, token: process.env.DECOUPLA_API_TOKEN, }, }, ], };Then create
.env.developmentand/or.env.productionfiles and add your variables.DECOUPLA_WORKSPACE_ID=your_workspace_id DECOUPLA_API_TOKEN=your_token - Replace
-
Restart the development server:
If you're running
gatsby develop, stop the server and restart it to apply the changes.
Querying Data with GraphQL
Once the plugin is configured, you can query your Decoupla CMS data using Gatsby's GraphQL API.
-
Open the GraphiQL explorer:
In your browser, navigate to
http://localhost:8000/___graphql. This is the GraphiQL interface, which allows you to explore your GraphQL schema and test queries. -
Explore the schema:
In the GraphiQL explorer, you'll find your Decoupla data available under the
Decouplaroot query. Your content types will be available directly under theDecouplaroot, and will require afirstargument to retrieve the content. Explore the available types and fields to understand the structure of your data. -
Write a GraphQL query:
Here's an example query to fetch all posts from your Decoupla CMS:
query { Decoupla { allPost(first: 100) { # Adjust 'first' as needed. edges { node { title slug content } } } } }Replace
Postwith the name of your content type in Decoupla. Thefirst: 100argument specifies the number of items to retrieve. Adjust this value as needed.Note: All the generated GraphQL Types in the Gatsby integration are prefixed with
Decoupla_, e.g: The Content TypePostwill becomeDecoupla_Post. And all the available queries are under theDecoupla { ... }root field, the name of the queries and their arguments will remain the same as in our GraphQL API. -
Use the query in your Gatsby components:
You can use the
useStaticQueryhook or theStaticQuerycomponent to fetch data in your Gatsby components.Example using
useStaticQueryin a page component:import React from "react"; import { graphql, useStaticQuery } from "gatsby"; const PostsPage = () => { const data = useStaticQuery(graphql` query { Decoupla { allPost(first: 100) { edges { node { title slug content } } } } } `); const posts = data.Decoupla.allPost.edges; return ( <div> <h1>Posts</h1> {posts.map((postEdge) => ( <div key={postEdge.node.slug}> <h2>{postEdge.node.title}</h2> <p>{postEdge.node.content}</p> </div> ))} </div> ); }; export default PostsPage;
Previewing Content
To preview content from your Decoupla CMS, use your Preview API token in your gatsby-config.js or .env file. This will allow you to see draft and unpublished content in your development environment.