All Projects → Jonesus → gatsby-source-directus7

Jonesus / gatsby-source-directus7

Licence: other
Source plugin for pulling data into GatsbyJS from Directus CMS (https://directus.io)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to gatsby-source-directus7

gatsby-plugin-robots-txt
Gatsby plugin that automatically creates robots.txt for your site
Stars: ✭ 105 (+517.65%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-theme-jam-example
An example submission for the Gatsby Theme Jam.
Stars: ✭ 89 (+423.53%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-theme-nehalem
A Gatsby blog theme.
Stars: ✭ 168 (+888.24%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-source-behance
Gatsby source plugin for loading information from Behance
Stars: ✭ 17 (+0%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-remark-images-anywhere
Handle images with relative, absolute & remote path for gatsby-transformer-remark & gatsby-plugin-mdx
Stars: ✭ 22 (+29.41%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-17.65%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-remark-classes
Automatically add class attributes to markdown elements
Stars: ✭ 12 (-29.41%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-plugin-optimize-svgs
A Gatsby Plugin to minify SVGs output to the filesystem during the build.
Stars: ✭ 39 (+129.41%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-plugin-tailwindcss
Plug Tailwind CSS to your Gatsby website
Stars: ✭ 46 (+170.59%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-theme-dox
Documentation made easy with Gatsby. 🎉
Stars: ✭ 29 (+70.59%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-theme-gallery
🏞 A Gatsby Theme for adding a gallery to your site.
Stars: ✭ 40 (+135.29%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-plugin-asset-path
Move all of your JS and CSS build files, as well as the static folder into a subdirectory of your choice
Stars: ✭ 14 (-17.65%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-source-moltin
🚀 Gatsby source plugin for building Elastic Path Commerce Cloud powered eCommerce websites
Stars: ✭ 21 (+23.53%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-theme-deck-n-blog
Create a deck (with mdx-deck) and a blog post from the same MDX
Stars: ✭ 17 (+0%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-plugin-disqus
💬 A plugin for adding Disqus comments to GatsbyJS
Stars: ✭ 40 (+135.29%)
Mutual labels:  gatsby, gatsby-plugin
chandrikadeb7.github.io
Personal portfolio website hosted using GitHub Pages - Version 2
Stars: ✭ 99 (+482.35%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-plugin-apollo-client
📡Inject a Shopify Apollo Client into the browser.
Stars: ✭ 20 (+17.65%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-source-firestore
Gatsby source plugin for building websites using the Firestore as a data source.
Stars: ✭ 30 (+76.47%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-plugin-open-graph-images
🖼 Open-Graph Images derived and generated from React Components
Stars: ✭ 16 (-5.88%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-graphcms-example
Example of Gatsby source plugin for GraphCMS
Stars: ✭ 32 (+88.24%)
Mutual labels:  gatsby, gatsby-plugin

NOTE

This project is deprecated in favor for the official plugin: @directus/gatsby-source. This version will no longer be maintained.

gatsby-source-directus7

Source plugin for pulling data into Gatsby from Directus CMS. Inspired by iKonrad's original plugin.

Features

This plugin pulls all your Directus Collections and creates Gatsby nodes for them. It maps through all the Many-To-One and Many-To-Many -relationships between your Collections, and links them for extra comfy GraphQL querying.

For example, if you have a Posts collection, you'll get access to allDirectusPost and directusPost queries which return Items in the Collection. If your Posts have a relation to a Categories -collection with the same field name, the Category objects can be found right in the Post GraphQL object.

This plugin uses gatsby-source-filesystem to download all the files in Directus's uploads and link them to their respective Items's fields for simple usability. If you use lots of images in you project, take a look at also installing gatsby-plugin-sharp and gatsby-transformer-sharp.

This works really well with Gatsby's createPages function if you want to dynamically create content such as Blog posts from Directus, for instance.

Installation Guide

  • Install Gatsby
  • Install plugin by running npm npm i gatsby-source-directus7 -S
  • Configure the plugin in gatsby-config.js file:
module.exports = {
  siteMetadata: {
    title: 'A sample site using Directus API',
    subtitle: 'My sample site using Directus',
  },
  plugins: [
    {
      resolve: 'gatsby-source-directus7',
      options: {
        /**
         * The base URL of Directus.
         */
        url: 'https://directus.example.com',
        /**
         * Directus project to connect to, if empty defaults to '_' (Directus's default project name).
         */
        project: '_',
        /**
         * If your Directus installation needs authorization to access the required api,
         * you'll also need to supply the credentials here. In addition to your own
         * Collections, the Directus System Collections 'Collections', 'Files'
         * and 'Relations' should be readable either to the Public group
         * or the user account you provide.
         */
        email: '[email protected]',
        password: 'password123',
        /**
         * Optional - set the status of the items you want to receive. E.g. if you functionality
         * want to receive items with status 'published'.
         * `targetStatus` sets the status you want the items to have. `defaultStatus`
         * defines a fallback status that will also be accepted (e.g. you want
         * items with status 'draft', but 'published' is also acceptable)
         *
         */
        targetStatus: 'draft',
        defaultStatus: 'published'
      },
    },
  ],
};

Usage

For every Collection in Directus, the plugin will create a separate node with Directus prefix.

So, for your posts and categories Collections, the queries would be directusPost, allDirectusPost and directusCategory, allDirectusCategory.

This plugin is using Pluralize module to transform plural table names into singular node types to conform to the Gatsby naming convention. If for some reason, the generated name doesn't seem right, you can overwrite the node name using the nameExceptions object in the plugin config. (see example above)

Example with Gatsby's createPages

This example assumes that you have created a posts collection in Directus with title, author and content fields, and a barebones Gatsby app. Add the following files to your Gatsby project:

./gatsby-node.js

const path = require('path');

// Gatsby function that runs during build after generating GraphQL store
exports.createPages = async ({ actions, graphql }) => {
  const { createPage } = actions;
  try {
    const result = await graphql(`
      {
        allDirectusPost {
          edges {
            node {
              directusId
              title
            }
          }
        }
      }
    `);

    result.data.allDirectusPost.edges.map(edge => {
      try {
        const node = edge.node;
        const url = `post/${node.directusId}`;
        createPage({
          path: url,
          component: path.resolve('src/templates/post.jsx'),
          context: {
            // Used as a query argument in the component below
            id: node.directusId,
          },
        });
        console.log(`Generated post '${node.title}' to path '/${url}'`);
      } catch (error) {
        console.error(`Failed to generate post '${node.title}': ${error}`);
      }
    });
  } catch (error) {
    console.error(`GraphQL query returned error: ${error}`);
  }
};

./src/templates/post.jsx

import React from 'react';
import { graphql } from 'gatsby';

// Basic post component
export default ({ data }) => {
  const post = data.directusPost;
  return (
    <div>
      <h1>{post.title}</h1>
      <p>Posted by {post.author}</p>
      <div dangerouslySetInnerHTML={{ __html: post.content }} />
    </div>
  );
};

// Query to be ran on build, passes resulting JSON as 'data' prop
export const query = graphql`
  query($id: Int!) {
    directusPost(directusId: { eq: $id }) {
      title
      author
      content
    }
  }
`;

To do

  • Implement markdown processing with something like gatsby-transformer-remark
  • Consider adding in rest of the data provided by Directus (users and activity for example)

Contributions

Contributions are always welcome, if you come up with any feature requests, ideas or bugs just create an issue or a pull request!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].