-
-
Notifications
You must be signed in to change notification settings - Fork 544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[gatsby-theme-minimal-blog] Additional Post schema fields returning null in allPost query #438
Comments
Hi, thanks for the issue! As the error suggests you currently can't override nodes from other plugins/themes. So this is a limitation of Gatsby itself. You can solve this problem by using a custom field extension/directive for your fields. This is the only code you need in gatsby-node.js: const addFrontmatterField = (name) => async (source, args, context) => {
const mdxNode = context.nodeModel.getNodeById({
id: source.parent,
})
return mdxNode.frontmatter[name]
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes, createFieldExtension } = actions
createFieldExtension({
name: `addFrontmatterField`,
args: {
name: `String!`,
},
extend({ name }) {
return {
resolve: addFrontmatterField(name),
}
},
})
createTypes(`
interface Post @nodeInterface {
subtitle: String
featuredImage: File @fileByRelativePath
}
type MdxPost implements Node & Post {
subtitle: String @addFrontmatterField(name: "subtitle")
featuredImage: File @addFrontmatterField(name: "featuredImage") @fileByRelativePath
}
`)
} Since const mdxNode = context.nodeModel.getNodeById({
id: source.parent,
}) And as Gatsby automatically infers fields, this node contains the new fields from the frontmatter which in turn you then can access. Important bit for images here: Order matters! You first need to apply the In a previous issue I've shown accessing the parent in the query (#387), today I came up with this better idea :) |
@LekoArts Works perfectly, thanks! But in order to add these additional fields to the generated posts/pages (in
Do you know if it's possible to use the existing |
You don’t need the |
Hi! I am trying to add 2 additional fields (
subtitle
andfeaturedImage
) to myPost
andPage
types. I am able to add these new field types in my customgatsby-node.js
file:/content/posts/introduction-to-defence-against-the-dark-arts/index.mdx
:I can confirm these fields are then accessible via graphiQL (but they return null since we haven't generated the new nodes yet):
However, when I try to generate the nodes:
I get the following error:
I can eliminate this error by creating a new type name (i.e.,
MdxPost2
):But the fields still return
null
in theallPost
query:However, I see that a
allMdx2Post
query was added, which does return the additional data:How can I update the
allPost
query to see this data? Thanks!The text was updated successfully, but these errors were encountered: