graphql - Gatsby Graphql 阅读图片
问题描述
我想从 YAML 读取图像文件的路径,并使用它gatsby-image
来创建响应式图像,但它不能让我做我想做的事。
数据.yaml
fileKey: data
profile:
- name: Foo
image: image.jpg
- name: Bar
image: image2.jpg
我的查询看起来像:
query DataQuery {
pagesYaml(fileKey: {eq: "data"}) {
profile {
name
image {
childImageSharp {
fluid(maxWidth: 2048, quality: 100) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
这给了我这个错误Field "image" must not have a selection since type "String" has no subfields
。
但是,这在下面有效。
query DataQuery {
pagesYaml(fileKey: {eq: "data"}) {
profile {
name
}
profileImage: file(relativePath: { eq: "image.jpg" }) {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
profileImage2: file(relativePath: { eq: "image2.jpg" }) {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
}
第一个查询的好处是我可以将个人资料图像放在 .js 文件中profile
,因此更容易在 JavaScript 中管理数据。是否可以将图像放在profile
查询中的对象内?
这是我的gatsby-config.js
。图像存储在src/img/
.
const proxy = require('http-proxy-middleware');
const yaml = require('./src/yaml');
module.exports = {
siteMetadata: {
title: 'Gatsby + Netlify CMS Starter',
description:
'This repo contains an example business website that is built with Gatsby, and Netlify CMS.It follows the JAMstack architecture by using Git as a single source of truth, and Netlify for continuous deployment, and CDN distribution.',
},
plugins: [
'gatsby-plugin-react-helmet',
{
resolve: 'gatsby-plugin-robots-txt',
options: {
policy: [{ userAgent: '*', disallow: '/' }]
}
},
{
resolve: `gatsby-plugin-sitemap`,
options: {
exclude: [`/admin`]
}
},
'gatsby-plugin-sass',
'gatsby-transformer-yaml',
{
// keep as first gatsby-source-filesystem plugin for gatsby image support
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/static/img`,
name: 'uploads',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/src/pages`,
name: 'pages',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/src/img`,
name: 'images',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/src/data`,
name: 'data',
},
},
'gatsby-plugin-sharp',
'gatsby-transformer-sharp',
{
resolve: 'gatsby-transformer-remark',
options: {
engines: { yaml },
plugins: [
{
resolve: 'gatsby-remark-relative-images',
options: {
name: 'uploads',
},
},
{
resolve: 'gatsby-remark-images',
options: {
// It's important to specify the maxWidth (in pixels) of
// the content container as this plugin uses this as the
// base for generating different widths of each image.
maxWidth: 2048,
},
},
{
resolve: 'gatsby-remark-copy-linked-files',
options: {
destinationDir: 'static',
},
},
],
},
},
{
resolve: 'gatsby-plugin-web-font-loader',
options: {
custom: {
families: ['Appli Mincho']
}
}
},
{
resolve: 'gatsby-plugin-netlify-cms',
options: {
modulePath: `${__dirname}/src/cms/cms.js`,
},
},
{
resolve: 'gatsby-plugin-purgecss', // purges all unused/unreferenced css rules
options: {
develop: true, // Activates purging in npm run develop
purgeOnly: ['/all.sass'], // applies purging only on the bulma css file
},
}, // must be after other CSS plugins
'gatsby-plugin-netlify', // make sure to keep it last in the array
],
// for avoiding CORS while developing Netlify Functions locally
// read more: https://www.gatsbyjs.org/docs/api-proxy/#advanced-proxying
developMiddleware: app => {
app.use(
'/.netlify/functions/',
proxy({
target: 'http://localhost:9000',
pathRewrite: {
'/.netlify/functions/': '',
},
})
)
},
}
解决方案
这很可能发生,因为 Gatsby 将您的profile.image
字段推断为字符串而不是文件。如果在引导 Gatsby 时提供的一个或多个路径字符串未解析为文件,则可能会发生这种情况。请注意,Gatsby 在启动后不会重新运行现有字段的类型推断,因此您需要重新启动开发服务器以获取这些更改。
推荐阅读
- python - 如何在 python 3 中制作矩形选择工具?
- php - dockerizing phpunit 测试时的最佳实践
- python - Django 事务不适用于原始 SQL 查询
- maven - 如何在 Maven 项目中制作包?
- java - 从 ArrayList 获取最新的唯一参数
- sql - 获取最高工资SQL的部门名称
- mysql - 删除没有root权限或mysql用户的MySQL8 SELECT ... INTO FILE文件
- react-native - 需要 React Native 的 Keyboard 的 removeListener 函数吗?
- legend - 我们可以在 Altair 的交互式折线图中添加交互式图例吗?
- aws-kms - 在 AWS 外部使用 KMS 公钥进行非对称加密