首页 > 解决方案 > Gatsby:graphql 查询中的 gatsby-source-graphql 和 gatsby-plugin-sharp

问题描述

我正在使用 Gatsby v. 2.0.2。作为一个无头 CMS,我正在使用 Strapi。我正在尝试对从 Strapi 上传的图片使用 gatsby-image、gatsby-plugin-sharp(图像处理)。

我的文件夹结构是:

>>projectfolder
   >>api
     >>public
       >>uploads (here my images are located)
   >>frontend (gatsby stuff)

类似于 Gatsby-source-wordpress 我想像这样使用 graphql 查询图像:

query {
  api {
    projects {
      image {
        name
        url
        childImageSharp {
          resize(width: 180, height: 180) {
            src
          }
        }
      }
    }
  }
}

如果我使用exports.onCreateNode,我只会得到父节点“api”。如何获取图像 URL 以便可以使用 createRemoteFileNode?

我是否需要编写exports.createPages,graphql 查询api-node,然后使用createNode 或createNodeField 创建节点?


我曾尝试使用 Gatsby-source-strapi,但同样的问题无法到达 Gatsby-transformer-sharp。

query {
  allStrapiProject {
    edges {
      node {
        title
        image {
          name
        }
      }
    }
  }
}

标签: graphqlgatsbystrapi

解决方案


这已在最近的拉取请求中添加,并将在接下来的几天内发布在 npm 上:https ://github.com/strapi/gatsby-source-strapi/pull/24 。


推荐阅读