npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gatsby-plugin-contentful-optional-fields

v2.2.2

Published

A plugin to define optional contentful fields and resolve them to NULL if they're not set.

Downloads

5,854

Readme

gatsby-plugin-contentful-optional-fields

This package resolves an issue with gatsby-source-contentful and optional fields. When an optional field is defined on a content type in Contentful, at least one instance of that content type MUST contain an entry in that optional field. Otherwise, gatsby-source-contentful will not send that optional field in the return data, and suddenly your GraphQL queries are all borked. Uh-oh!

This package lets you manually define optional fields on each content type. When defined this way, your GraphQL queries will recieve NULL for unpopulated optional fields, instead of no value at all.

Installation

npm i gatsby-contentful-optional-fields

Quickstart

Include gatsby-plugin-contentful-optional-fields in your gatsby-config.js, with a map of optional fields and their primitive types in the plugin options.

{
  resolve: 'gatsby-plugin-contentful-optional-fields',
  options: {
    optionalFields: {
      ParentNode: {
        optionalFieldName: 'String'
      }
    }
  }
}

Setting up your config file

First, include gatsby-plugin-contentful-optional-fields in your gatsby-config.js.

{
  resolve: 'gatsby-plugin-contentful-optional-fields',
  options: {
    optionalFields: {}
  }
}

Next, you must provide a config object which defines all of your optional fields. The format for this object is as follows:

{
  ParentNode: {
    optionalFieldName: 'Optional field type'
  }
}

For instance, if your ContentfulPages have an "optionalHeader" field, which is a relational field that links to a ContentfulHeader node, your config would look like this:

{
  ContentfulPage: {
    optionalHeader: 'Node'
  }
}

The optional field type in this case is a Node (an object with child properties). Typically, your fields will either be of type 'Node', 'String' or 'Int'.

Your final plugin object should look something like this:

{
  resolve: 'gatsby-plugin-contentful-optional-fields',
  options: {
    optionalFields: {
      ContentfulPage: {
        optionalCustomHeader: 'Node'
      }
    }
  }
}

Usage in GraphQL queries

Now that your config file is set up, you can begin using optional fields in your queries. For optional fields of type Node, you will have to change your queries to use the spread operator (... on ContentType), since child properties will not be guaranteed to exist if the optional field is not set, and therefore NULL is sent.

For instance, an optional query for a ContentfulHeader might look something like this (note the spread operator):

query {
  contentfulPage {
    optionalCustomHeader {
      ... on ContentfulHeader {
        id
        logo
        links
      }
    }
  }
}

Advanced example

{
  resolve: 'gatsby-plugin-contentful-optional-fields',
  options: {
    optionalFields: {
      ContentfulPage: {
        optionalCustomHeader: 'Node',
        optionalCustomTitle: 'String',
      },
      ContentfulBlogPost {
        optionalAuthor: 'Node',
        optionalPublishDate: 'Int'
      }
    }
  }
}
query {
  contentfulPage {
    optionalCustomHeader {
      ... on ContentfulHeader {
        id
        logo
        links
      }
    }
    optionalCustomTitle
  }
  contentfulBlogPost {
    optionalAuthor {
      ... on ContentfulAuthor {
        id
        firstName
        lastName
      }
    }
    optionalPublishDate
  }
}