【发布时间】:2019-10-03 00:33:18
【问题描述】:
我正在使用 Gatsby 构建一个博客站点,并且我正在使用 Netlify CMS 来管理我的内容。每篇博文都有一个在 frontmatter 中指定的特色图片。
但是,我在显示这些图像时遇到了一些问题。我不断收到此错误:
GraphQL Error Field "image" must not have a selection since type "String" has no subfields.
我尝试添加 gatsby-remark-relative-images 以尝试解决此问题,但这不起作用 - 我仍然遇到相同的错误。
关于如何解决这个问题的任何想法?
目录结构
content
- articles
public
src
static
- admin
- images
发布前言
---
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,
},
})
})
})
}
【问题讨论】:
-
嘿@taylor018,由于您使用的是netlifyCMS,您是否使用图像小部件来选择和上传图像?如果是这样,您会在
config.yml中分享相关部分吗?
标签: reactjs graphql gatsby netlify-cms