【问题标题】:Gatsby Graphql Reading imageGatsby Graphql 阅读图片
【发布时间】:2020-01-18 18:00:20
【问题描述】:

我想从 YAML 读取图像文件的路径,并使用 gatsby-image 创建响应式图像,但它不能让我做我想做的事。

data.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
        }
      }
    }
  }
}


第一个查询的好处是我可以将个人资料图像放在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/': '',
        },
      })
    )
  },
}

【问题讨论】:

    标签: graphql gatsby


    【解决方案1】:

    这很可能发生,因为 Gatsby 将您的 profile.image 字段推断为字符串而不是文件。如果在引导 Gatsby 时提供的一个或多个路径字符串未解析为文件,则可能会发生这种情况。请注意,Gatsby 在启动后不会重新运行现有字段的类型推断,因此您需要重新启动开发服务器以获取这些更改。

    【讨论】:

    • 我又仔细检查了一遍,还是不行。我是gatsby-config.js粘贴的。
    • 如果您的 data.yml 位于 src/data 中并且您的图像位于 src/img 中,那么您的 profile.image 字段应类似于 ../img/image.jpg,而不是 image.jpg
    • 谢谢,这行得通,但是不必指定相对于 yml 位置的路径。我设置了gatsby-source-filesystem 插件来指定图像文件夹,所以我不需要这样做。这里缺少什么?
    • 另外,如果我使用gatsby-transformer-rewark 查询图像,我不需要在相对于 Markdown 文件位置的static 文件夹中查询图像。所以,在这种情况下,我需要一个相对于文件位置的路径,这很奇怪..
    • 我想我明白了。我认为它适用于gatsby-transformer-remark,因为这个插件gatsby-remark-relative-images。谢谢。
    猜你喜欢
    • 2019-07-11
    • 1970-01-01
    • 2018-06-16
    • 2020-08-28
    • 2019-02-19
    • 2020-08-15
    • 2021-06-20
    • 2021-06-19
    • 2021-05-29
    相关资源
    最近更新 更多