Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Gatsby / Contentful Schema

Tags:

graphql

gatsby

I'm trying to add a createSchemaCustomization for a Contentful source where that source might not have the field I'm interested in, and so Gatsby can't infer the type.

I've created the customization such that my template queries do not complain, but if there is an entry, it's also not being picked up. I'm sure I've probably not set up the customization correctly. Here's what I have (the @infer flag doesn't appear to do much either way):

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions;
  const typeDefs = `
    type ContentfulUniversalProduct implements Node @infer {
      datesAndPricesSnippets: [DatesAndPricesSnippet]
    }
    type DatesAndPricesSnippet implements Node @infer {
      id: String
      title: String
      icon: String
      iconColor: String
      body: WithChildMarkdownRemark
    }
    type WithChildMarkdownRemark implements Node @infer {
      childMarkdownRemark: MarkdownRemark
    }
  `;
  createTypes(typeDefs);
};

My query.

This query works if there are datesAndPricesSnippets in Contentful, AND I don't have the above schemaCustomization in place.

If I do have the customization in place, then the output for this query is null - regardless of whether the content is in Contentful or not.

export const query = graphql`
  query($slug: String!, $id: String!) {
    contentfulUniversalProduct {
      datesAndPricesSnippets {
        id
        title
        body {
          childMarkdownRemark {
            html
          }
        }
        icon
        iconColor
      }
    }
  }
`
like image 504
Guy Bowden Avatar asked Sep 20 '25 04:09

Guy Bowden


2 Answers

i ran into a similar sounding issue, following helped me:

  1. install gatsby-plugin-schema-snapshot
  2. generate the schema.gql file, GATSBY_UPDATE_SCHEMA_SNAPSHOT=y yarn develop did it for me
  3. observe the schema.gql file, your changes with createTypes will show up

in my case adding a @link call helped return nodes instead of null:

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions
  const typeDefs = `
    type ContentfulThing implements Node {
      fieldWithModules: [SomeBigUnion] @link(by: "id", from: "fieldWithModules___NODE")
    }
  `
  createTypes(typeDefs)
}
like image 138
schpet Avatar answered Sep 23 '25 12:09

schpet


Using gatsby-plugin-schema-snapshot (from another answer) actually didn't work for me when trying to handle cases where markdown content from Contentful was missing.

There is a method accessible via actions, however, that generates a type definitions file that appears to be more accurate. Then if you directly copy the relevant types into the typeDefs defined in createSchemaCustomization, it seems to work as expected -- whether or not markdown content exists.

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions;
 
  // This is what generates the type defs
  actions.printTypeDefinitions({path: './typeDefs.txt'});

  const typeDefs = `
    // Your type defs from the generated file here
    // (just the ones you've been having issues with)
  `;
  createTypes(typeDefs)
};

Hope this helps!

like image 25
Ksan8 Avatar answered Sep 23 '25 10:09

Ksan8