janosh/blog

Unknown field 'fields' on type 'File!'.

marqpdx opened this issue · 7 comments

hi,

i just downloaded your site after reading your article about Modal in React.

i followed the instructions, and when i run it as a fresh install i get:

error GraphQL Error Encountered 1 error(s):
- Unknown field 'fields' on type 'File!'.

      file: /usr/local/projects/aa_working/Active/janosh.io/src/pages/nature.js

certainly not a big deal on this end, but if you think of what i might be, let me know.

Thanks,
m

Same for me. Having the same error on a lot of non static queries just lately. Sure wish I could figure it out.

  • Unknown field 'fields' on type 'File!'.

That's interesting. Thanks for letting me know. I can't reproduce that error on my end. One thing I can think of that might be causing this issue is that exports.onCreateNode in gatsby-node.js should really be an asynchronous function awaiting the fs.readFile call, which might only cause problems on machines with slower file system access.

Could you try to replace exports.onCreateNode with the following code snippet and let me know if that has any effect?

exports.onCreateNode = async ({ node, actions }) => {
  if (node.dir && node.dir.includes(`content/photos`) && node.ext === `.jpg`) {
    await fs.readFile(node.absolutePath, async (err, data) => {
      if (err) throw err
      const tags = ExifReader.load(data.buffer)
      const meta = {
        lat: tags.GPSLatitude.description,
        lng: tags.GPSLongitude.description,
        caption: tags.Headline.description,
      }
      await actions.createNodeField({
        node,
        name: `meta`,
        value: meta,
      })
    })
  }
}

Ignore what I wrote above. After running gatsby clean followed by gatsby develop I was able to reproduce the problem. So maybe that error only shows up when running gatsby develop with a clean cache, since on the second try it simply fetches the results of the async calls to fs.readFile from the first run where they didn't finish in time.

In any case, I was able to fiddle with the problem locally now and awaiting fs.readFile seems insufficient. However, refactoring to fs.readFileSync as follows solves the problem.

exports.onCreateNode = ({ node, actions }) => {
  if (node.dir && node.dir.includes(`content/photos`) && node.ext === `.jpg`) {
    const buffer = fs.readFileSync(node.absolutePath)
    const tags = ExifReader.load(buffer)
    const meta = {
      lat: tags.GPSLatitude.description,
      lng: tags.GPSLongitude.description,
      caption: tags.Headline.description,
    }
    actions.createNodeField({
      node,
      name: `meta`,
      value: meta,
    })
  }
}

Either replace exports.onCreateNode in gatsby-node.js with the above snippet or do a git pull in the coming days as I'll be pushing this fix soon.

I'll close this issue but feel free to reopen if questions/problems remain.

Thank You
Very Excellent Work!
Will wait for the Update.
Donald Boulton

I just pushed the fix. Merged into master by #55.

Tested the changes after adding fs-extra as fs will not install properly on a Windows machine. You get
"fs": "^0.0.1-security",
Which you want in your package.json to reference fs to fs-extra, as

"fs": "^0.0.1-security",
"fs-extra": "^7.0.1",
I installed fs-extra and then changing gatsby-node.js to
const fs = require(fs-extra)

By adding fs-extra, all references to fs are somehow taken over by fs-extra.
After fixing the fs issue it built correctly.

@donaldboulton Glad to hear it!