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
}
}
}
`
i ran into a similar sounding issue, following helped me:
schema.gql
file, GATSBY_UPDATE_SCHEMA_SNAPSHOT=y yarn develop
did it for meschema.gql
file, your changes with createTypes
will show upin 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)
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With