I am almost able to create Hero banner images with different layouts depending on what fields are filled out in a headless CMS/Contentful. I am able to get data from Contentful endpoint and have optional fields for header titles(sting values) when defining types for them in advance in the gatsby-node.js file if/when Gatsby does not find them:
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type ContentfulHeroBannerSet implements Node {
headerFirstLeft: String
headerFirstCenter: String
headerFirstRight: String
}
`;
createTypes(typeDefs);
};
And then checking their existence in React components renders them if they exist:
{item.node.headerRight && (
<>
<Container
item
xs={6}
spacing={3}
className={classes.topContainer}
>
Now I have added another optional field with rich text and I can not find out how to set the type definition for it in advance and Gatsby does not allow to build project before it finds automatically or in advance a type for subHeaderLeft. Heres the query in Index.js:
export const pageQuery = graphql`
query HomeQuery {
site {
siteMetadata {
title
}
}
allContentfulHeroBannerSet(limit: 2) {
edges {
node {
headerLeft
headerCenter
headerRight //So far so good
subHeaderLeft { // Here comes the problem
childMarkdownRemark {
html
}
}
ERROR: There was an error in your GraphQL query: Cannot query field "childMarkdownRemark" on type.. It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "contentfulHeroBannerSetSubHeaderSecondLeftRichTextNode": https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions.
Tried also to read through that docs page but still stuck. Dummy content would not suit as the idea is to have optional fields for content editors. Any ideas?