Get Started Building a Gatsby Website with DirectusBy Eron Powell & Azri Kahar on February 21, 2022
This example will show you how to link a simple Gatsby frontend blog template to a Directus Project with a simple SQLite database containing a few blog posts.
Scope and Purpose
The purpose of this article is to focus on the bare basics of linking a Gatsby frontend to Directus, then show you where to learn about more robust authentication methods, roles & permissions configurations, and API functionalities. It is important to stress to new users that Directus is a general purpose database wrapper, which mirrors any linked database. No matter your data model or use-case, Directus can serve as a backend (plus no-code admin panel) for any type of project and also connect to any (or many) different front-ends.
Please keep in mind that this is not a demo showing what Directus can be used for, but rather a low variable entry-point on the path to understanding how to do anything you want with it.
This example was created for demonstration purposes and is not intended to be production ready, but PRs that address this and any other issues are always welcome! See Contribution Guidelines.
- Clone the examples repo.
Every frontend template in the
examples repo references the
shared folder. So moving these directories around from one location to another could break the relative file path.
From that repo, setup the provided Directus instance and get it running. Instructions are on the
Install dependencies for this example.
cd gatsby npm install
.envfile for this Gatsby template by copying and pasting the provided
.env.examplefile. Nothing will need to be changed in the copied
.envfile since the URL, username, and password are all set.
Start the development server.
npm run develop
- Your Directus Gatsby example is now running at http://localhost:8000.
Now that you have the frontend and backend linked up, let's touch on what's happening in this project and where to find more sophisticated options.
Gatsby is a react-based Static Site Generator (SSG), which is the rendering option used here. But starting with Gatsby 4, Deferred Static Generation (DSG) and Server-side Rendering (SSR) are supported as well. To learn more, see the documentation on Gatsby's build process.
The provided Gatsby instance uses the official
gatsby-source-directus plugin to pull data in from the Directus API. You can find this project's config file under
examples > gatsby > gatsby-config.js. To learn more, see the Gatsby Directus plugin documentation.
Aside from the default
local authentication mechanism, Directus also supports SSO through
ldap. To learn more about SSO options, read our SSO Documentation or follow our guide on how to setup SSO.
Users, Roles and Permissions
For simplicity's sake, this Directus instance provided has Public read permissions activated for
directus_users. Find Permissions in your local Directus Instance under
Settings > Roles and Permissions.
Additionally, any number of Roles can be created and fully configured. Permissions are completely granular. Learn more about this in our documentation on Users, Roles and Permissions.
There is a simple API call made from
gatsby-node.js, which fetches
articles. If you'd like to see this, you can find
gatsby-node.js on GitHub. However, please note the Directus API is exhaustive for any database linked.
Directus uses Database Mirroring to dynamically generate REST endpoints and a GraphQL schema based on the connected database's architecture. This means the REST and the GraphQL APIs will both fit any project with any database schema, out of the box. Learn more in the API Reference.
Looking for technical support for your non-enterprise project? Please visit the Directus Discord Community.
Want to stay focused on your content and apps? Let Directus manage the platform with our Directus Cloud service.