Gatsby: Getting error 'Cannot query field "draft" on type "Frontmatter".'

Created on 11 Nov 2016  Â·  7Comments  Â·  Source: gatsbyjs/gatsby

I cloned Bricolage.io site with Gatsby, develop was working great, except hot reloading wasn't working at all, not big deal I think, it is alpha version overall.

Then I deleted everything except favicon.ico, index.js, robots.txt, tags.js in pages folder to write my own blog posts, wrote couple of them and when I use "gatsby build" in command line, getting this error: 'Cannot query field "draft" on type "Frontmatter".'

Is it possible to workaround this problem? If not, what is the appropriate way to populate Kyle Mathews style blog with my own posts? Or is it simply not possible and I should get things done from scratch using one of Gatsby simple starters?

Update: for someone getting the same error, you should remove draft field from GraphQL queries in following files:

  • page-templates/blog-post.js
  • page-templates/tag-page.js
  • pages/index.js
  • pages/tags.js

Most helpful comment

@cupojoe not yet possible - schema is constructed just from existing data. There is defenitely plan to add way to declare schema after Gatsby v2 is released

All 7 comments

I'm getting the same error. Removed all the blog posts, put in my own and was greeted with this. Then I restored all the previous posts and the error continued. I suspect it might be some cache problem.

I think I got finally got around by adding draft: false to my own posts.

This is on _1.0.0-alpha9_.

There hasn't been any time spent on improving the errors in the new stuff so that error is a bit ambiguous. What's happening is in my blog — I add draft: true to frontmatter of draft blog posts. Then Gatsby, with its new GraphQL data layer, constructs a graphql schema based on my blog posts — which because of my draft: true adds draft as a field in the markdown frontmatter type.

The error comes from the fact that the blog index page queries for the draft field (see https://github.com/KyleAMathews/blog/blob/90e9c2979b54f08b76ddd7e9f7101b6ba93d6353/pages/index.js#L79). Gatsby extracts the graphql queries from pages and runs them and then injects their results as props into the component. This lets you query for exactly the data you need for a page. But since you removed the markdown files with draft: true, the schema now doesn't include this as a field which means that the query then fails because the field draft no longer exists.

So the solution would be to modify the queries to remove all mentions of the draft field.

Thanks for checking out the 1.0 stuff! I salute your bravery :-) I'll be starting to work on upgrade + conceptual/API docs soon as I finish fleshing out the final pieces.

@iiroj @KyleAMathews thanks for answers, closing issue.

Offtop: Kyle, did you drop your idea about SimpleGTD? I highly resonate with your concerns you've introduced in your blogpost. I would like to discuss that and compare your ideas with mine :-)

@hyphen-wrex yeah — 1) didn't think building a TODO app was worth my time and 2) I decided there are better ways to think about productivity e.g. this book and this one

@KyleAMathews is there a way to manually add a field to the schema, in case you want to keep this functionality but don't have any draft pages at the moment (ie. could be added in the future?)

Edit: solved it by adding the property with a default false to pages. Still wonder if there is a way to declare a schema.

@cupojoe not yet possible - schema is constructed just from existing data. There is defenitely plan to add way to declare schema after Gatsby v2 is released

you can achieve this by using createNode with a default value

createNodeField({
  node,
  name: 'draft',
  value: draft || false,
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hobochild picture hobochild  Â·  3Comments

brandonmp picture brandonmp  Â·  3Comments

totsteps picture totsteps  Â·  3Comments

kalinchernev picture kalinchernev  Â·  3Comments

dustinhorton picture dustinhorton  Â·  3Comments