I'm building a blog site with Gatsby and I'm using Netlify CMS to manage my content. Each blog post has a featured image specified in the frontmatter.
But, I'm having some issues with getting those images to display. I keep getting this error:
GraphQL Error Field "image" must not have a selection since type "String" has no subfields.
I tried adding gatsby-remark-relative-images
to try to fix this but that's not working - i still get the same error.
Any ideas on how to fix this?
Directory structure
content
- articles
public
src
static
- admin
- images
Post Frontmatter
---
title: Post 1
image: img.png
---
gatsby-config.js
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/static/images`,
}
},
{
resolve: `gatsby-source-filesystem`,
options: {
name: `content`,
path: `${__dirname}/content`,
}
},
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-relative-images`,
},
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 590,
},
},
],
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
`gatsby-plugin-netlify-cms`,
`gatsby-plugin-sitemap`
]
gatsby-node.js
const path = require('path');
const { createFilePath } = require(`gatsby-source-filesystem`)
const { fmImagesToRelative } = require('gatsby-remark-relative-images');
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
fmImagesToRelative(node)
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
createNodeField({
node,
name: `slug`,
value: slug,
})
}
}
exports.createPages = ({actions, graphql}) => {
const {createPage} = actions;
return graphql(`{
allMarkdownRemark {
edges {
node {
html
id
frontmatter {
title
date
templateKey
}
fields {
slug
}
}
}
}
}`)
.then(res => {
if (res.errors) {
return Promise.reject(res.errors);
}
res.data.allMarkdownRemark.edges.forEach(({node}) => {
createPage({
path: node.fields.slug,
component: path.resolve(
`src/templates/${String(node.frontmatter.templateKey)}.js`
),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.fields.slug,
},
})
})
})
}