All Projects → GraphCMS → Gatsby Source Graphcms

GraphCMS / Gatsby Source Graphcms

The official Gatsby source plugin for GraphCMS projects

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Gatsby Source Graphcms

Feathericons.com
The Feather website
Stars: ✭ 104 (-14.05%)
Mutual labels:  gatsby
Gray Matter
Contributing Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Stars: ✭ 2,105 (+1639.67%)
Mutual labels:  gatsby
Gatsby Source Figma
🍭 Gatsby plugin for using Figma documents as a data source.
Stars: ✭ 118 (-2.48%)
Mutual labels:  gatsby
Nodejs.dev
A new Node.js resource built using Gatsby.js with React.js, TypeScript, and Remark.
Stars: ✭ 1,794 (+1382.64%)
Mutual labels:  gatsby
Developer Community Stats
🚀 A repository to encourage beginners to contribute to open source and for all contributors to view their Github stats
Stars: ✭ 116 (-4.13%)
Mutual labels:  gatsby
Create Ueno App
The easiest and fastest way to create new web projects with next, gatsby, create-react-app and mobile projects with react-native.
Stars: ✭ 116 (-4.13%)
Mutual labels:  gatsby
Jamstack Ecommerce
A starter project for building performant ECommerce applications with Next.js and React
Stars: ✭ 1,384 (+1043.8%)
Mutual labels:  gatsby
Gatsby Advanced Blog
Gatsby starter for advanced blog
Stars: ✭ 121 (+0%)
Mutual labels:  gatsby
Gatsby Starter Elemental
Gatsby starter for portfolio sites
Stars: ✭ 115 (-4.96%)
Mutual labels:  gatsby
Gatsby Docs Kit
📃Easy to Maintain Markdown/React Documentation Static Websites
Stars: ✭ 117 (-3.31%)
Mutual labels:  gatsby
Covid 19 Apis
Postman COVID-19 API Resource Center—API collections to help in the COVID-19 fight.
Stars: ✭ 111 (-8.26%)
Mutual labels:  gatsby
Gatsby Source Datocms
Official GatsbyJS source plugin to pull content from DatoCMS
Stars: ✭ 113 (-6.61%)
Mutual labels:  gatsby
Gatsby Themes
Gatsby themes that we use to build websites at Rocketseat ⚡️🔥
Stars: ✭ 116 (-4.13%)
Mutual labels:  gatsby
Gatsbytutorials.com
A community-updated list of video, audio and written tutorials to help you learn GatsbyJS. 👩‍💻
Stars: ✭ 109 (-9.92%)
Mutual labels:  gatsby
Blog
my blog!
Stars: ✭ 119 (-1.65%)
Mutual labels:  gatsby
Gatsby Starter Saas Marketing
☁️ A simple one page marketing site starter for SaaS companies and indie hackers
Stars: ✭ 103 (-14.88%)
Mutual labels:  gatsby
Mdx Deck
♠️ React MDX-based presentation decks
Stars: ✭ 10,487 (+8566.94%)
Mutual labels:  gatsby
Undefined.fm
The engineering podcast with a 2 drink minimum. Hosted by Jared Palmer and Ken Wheeler.
Stars: ✭ 122 (+0.83%)
Mutual labels:  gatsby
Mdx Embed
Embed 3rd party media content in MDX - no import required 🧽
Stars: ✭ 119 (-1.65%)
Mutual labels:  gatsby
Gatsby Plugin Material Ui
Gatsby plugin for Material-UI with built-in server-side rendering support
Stars: ✭ 118 (-2.48%)
Mutual labels:  gatsby

gatsby-source-graphcms

The official Gatsby source plugin for GraphCMS projects • Demogatsby-starter-graphcms-blog

Installation

yarn add gatsby-source-graphcms gatsby-plugin-image

Configuration

We recommend using environment variables with your GraphCMS token and endpoint values. You can learn more about using environment variables with Gatsby here.

Basic

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-graphcms',
      options: {
        endpoint: process.env.GRAPHCMS_ENDPOINT,
      },
    },
  ],
}

Authorization

You can also provide an auth token using the token configuration key. This is necessary if your GraphCMS project is not publicly available, or you want to scope access to a specific content stage (i.e. draft content).

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-graphcms',
      options: {
        endpoint: process.env.GRAPHCMS_ENDPOINT,
        token: process.env.GRAPHCMS_TOKEN,
      },
    },
  ],
}

Options

Key Type Description
endpoint String (required) The endpoint URL for the GraphCMS project. This can be found in the project settings UI.
token String If your GraphCMS project is not publicly accessible, you will need to provide a Permanent Auth Token to correctly authorize with the API. You can learn more about creating and managing API tokens here.
typePrefix String (Default: GraphCMS_) The string by which every generated type name is prefixed with. For example, a type of Post in GraphCMS would become GraphCMS_Post by default. If using multiple instances of the source plugin, you must provide a value here to prevent type conflicts.
downloadLocalImages Boolean (Default: false) Download and cache GraphCMS image assets in your Gatsby project. Learn more.
buildMarkdownNodes Boolean (Default: false) Build markdown nodes for all RichText fields in your GraphCMS schema. Learn more.
fragmentsPath String (Default: graphcms-fragments) The local project path where generated query fragments are saved. This is relative to your current working directory. If using multiple instances of the source plugin, you must provide a value here to prevent type and/or fragment conflicts.
locales String (Default: ['en']) An array of locale key strings from your GraphCMS project. Learn more. You can read more about working with localisation in GraphCMS here.
stages String (Default: ['PUBLISHED']) An array of Content Stages from your GraphCMS project. Learn more. You can read more about using Content Stages here.

Features

Querying localised nodes

If using GraphCMS localisation, this plugin provides support to build nodes for all provided locales.

Update your plugin configuration to include the locales key.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-graphcms',
      options: {
        endpoint: process.env.GRAPHCMS_ENDPOINT,
        locales: ['en', 'de'],
      },
    },
  ],
}

To query for nodes for a specific locale, use the filter query argument.

{
  enProducts: allGraphCmsProduct(filter: { locale: { eq: en } }) {
    nodes {
      name
    }
  }
}

Check out the demo source for an example of a localisation implementation.

Querying from content stages

This plugin provides support to build nodes for entries from multiple Content Stages.

The provided Content Stages must be accessible according to the configuration of your project's API access. If providing a token, then that Permanent Auth Token must have permission to query data from all provided Content Stages.

The example below assumes that both the DRAFT and PUBLISHED stages are publicly accessible.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-graphcms',
      options: {
        endpoint: process.env.GRAPHCMS_ENDPOINT,
        stages: ['DRAFT', 'PUBLISHED'],
      },
    },
  ],
}

To query for nodes from a specific Content Stage, use the filter query argument.

{
  allGraphCmsProduct(filter: { stage: { eq: DRAFT } }) {
    nodes {
      name
    }
  }
}

Usage with gatsby-plugin-image

Requires gatsby-plugin-image as a project dependency.

This source plugin supports gatsby-plugin-image for responsive, high performance GraphCMS images direct from our CDN.

Use the gatsbyImageData resolver on your GraphCMS_Asset nodes.

{
  allGraphCmsAsset {
    nodes {
      gatsbyImageData(layout: FULL_WIDTH)
    }
  }
}

gatsbyImageData resolver arguments

Key Type Description
aspectRatio Float Force a specific ratio between the image’s width and height.
backgroundColor String Background color applied to the wrapper.
breakpoints [Int] Output widths to generate for full width images. Default is to generate widths for common device resolutions. It will never generate an image larger than the source image. The browser will automatically choose the most appropriate.
height Int Change the size of the image.
layout GatsbyImageLayout (CONSTRAINED/FIXED/FULL_WIDTH) Determines the size of the image and its resizing behavior.
outputPixelDensities [Float] A list of image pixel densities to generate. It will never generate images larger than the source, and will always include a 1x image. The value is multiplied by the image width, to give the generated sizes. For example, a 400 px wide constrained image would generate 100, 200, 400 and 800 px wide images by default. Ignored for full width layout images, which use breakpoints instead.
quality Int The default image quality generated. This is overridden by any format-specific options.
sizes String The <img> sizes attribute, passed to the img tag. This describes the display size of the image, and does not affect generated images. You are only likely to need to change this if your are using full width images that do not span the full width of the screen.
width Int Change the size of the image.

For more information on using gatsby-plugin-image, please see the documentation.

Downloading local image assets

If you prefer, the source plugin also provides the option to download and cache GraphCMS assets in your Gatsby project.

To enable this, add downloadLocalImages: true to your plugin configuration.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-graphcms',
      options: {
        endpoint: process.env.GRAPHCMS_ENDPOINT,
        downloadLocalImages: true,
      },
    },
  ],
}

This adds a localFile field to the GraphCMS_Asset type which resolves to the file node generated at build by gatsby-source-filesystem.

{
  allGraphCmsAsset {
    nodes {
      localFile {
        childImageSharp {
          gatsbyImageData(layout: FULL_WIDTH)
        }
      }
    }
  }
}

Using markdown nodes

This source plugin provides the option to build markdown nodes for all RichText fields in your GraphCMS schema, which in turn can be used with MDX.

To enable this, add buildMarkdownNodes: true to your plugin configuration.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-graphcms',
      options: {
        endpoint: process.env.GRAPHCMS_ENDPOINT,
        buildMarkdownNodes: true,
      },
    },
  ],
}

Enabling this option adds a markdownNode nested field to all RichText fields on the generated Gatsby schema.

Usage with gatsby-plugin-mdx

These newly built nodes can be used with gatsby-plugin-mdx to render markdown from GraphCMS.

Once installed, you will be able to query for MDX fields using a query similar to the one below.

{
  allGraphCmsPost {
    nodes {
      id
      content {
        markdownNode {
          childMdx {
            body
          }
        }
      }
    }
  }
}

Check out the demo source for an example of a full MDX implementation.

Working with query fragments

The source plugin will generate and save GraphQL query fragments for every node type. By default, they will be saved in a graphcms-fragments directory at the root of your Gatsby project. This can be configured:

If using multiple instances of the source plugin, you must provide a value to prevent type and/or fragment conflicts.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-source-graphcms',
      options: {
        endpoint: process.env.GRAPHCMS_ENDPOINT,
        fragmentsPath: 'my-query-fragments',
      },
    },
  ],
}

The generated fragments are then read from the project for subsequent builds. It is recommended that they are checked in to version control for your project.

Should you make any changes or additions to your GraphCMS schema, you will need to update the query fragments accrdingly. Alternatively they will be regnerated on a subsequent build after removing the directory from your project.

Modifying query fragments

In some instances, you may need modify query fragments on a per type basis. This may involve:

  • Removing unrequired fields
  • Adding new fields with arguments as an aliased field

For example, adding a featuredCaseStudy field:

fragment Industry on Industry {
  featuredCaseStudy: caseStudies(where: { featured: true }, first: 1)
}

Field arguments cannot be read by Gatsby from the GraphCMS schema. Instead we must alias any required usages as aliased fields. In this example, the featuredCaseStudy field would then be available in our Gatsby queries:

{
  allGraphCmsIndustry {
    nodes {
      featuredCaseStudy {
        ...
      }
    }
  }
}
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].