I wanted to filter posts based on a draft status (a la https://github.com/KyleAMathews/blog/blob/4c3f83cdcc7d158cbcb8236e4b826a8a4b3abd0a/src/pages/index.js#L78).
This works great if I have any posts with a draft key in the Frontmatter, but if not, it errors:
GraphQL Error Argument "filter" has invalid value {frontmatter: {draft: {ne: true}}}.
In field "frontmatter": In field "draft": Unknown field.
file: /src/pages/index.js
1 |
2 | query Home {
3 | allMarkdownRemark(
> 4 | filter: { frontmatter: { draft: { ne: true } } },
| ^
5 | limit: 10,
6 | sort: { fields: [frontmatter___order], order: ASC },
7 | ) {
8 | edges {
9 | node {
10 | id
11 | frontmatter {
12 | draft
13 | order
14 | path
If possible, it'd be nice for this to work regardless of existence.
A quick workaround was to toss a temp Markdown file in src/pages/temp.md that only had Frontmatter defined:
---
draft: true
path: '/temp'
---
We've discussed before adding default "markdown" frontmatter fields in the past but nothing ever went too far.
If you'd like to lead a discussion around this and create a PR, that'd be great. I can see adding default fields to markdown frontmatter being really useful.
Seems like https://github.com/gatsbyjs/gatsby/issues/2186 is semi-related, but if you have a minute and could point me to some of the previous convo, and any pointers for getting started (don't really have any idea), I'm all ears.
Going to close to help reduce clutter & because this is very easy to work around鈥攐bviously please re-open or otherwise handle as you wish.
could you filter in the map function like below?
{posts.map(post =>
{(post.node.frontmatter.draft !== 'true')
?
<li key={post.node.fields.slug}>
<Link to={post.node.fields.slug}>
{post.node.frontmatter.title}
</Link>
</li>
:
<span></span>
)}
@dustinhorton another way would be to loop all nodes in gatsby-node.js and create a node field 'draft' setting it to the value of frontmatter.draft, than you'd query for fields.draft instead of frontmatter.draft
that way you don't need a temp file
Most helpful comment
We've discussed before adding default "markdown" frontmatter fields in the past but nothing ever went too far.
If you'd like to lead a discussion around this and create a PR, that'd be great. I can see adding default fields to markdown frontmatter being really useful.