简体   繁体   English

Gatsby GraphQL 无法在 Strapi 的“文件”类型上查询字段“url”

[英]Gatsby GraphQL cannot query field “url” on type “File” of Strapi

I'm making a blog with Gatsby front-end, Strapi back-end.我正在用 Gatsby 前端和 Strapi 后端制作博客。 I made a query in component with StaticQuery我使用 StaticQuery 在组件中进行了查询

query={graphql`
        query {
          allStrapiArticle {
            edges {
              node {
                strapiId
                title
                category {
                  name
                }
                image {
                  url
                }
              }
            }
          }
        }
      `}

All of field is work fine without image{url} .没有image{url} ,所有字段都可以正常工作。 I got error: error Cannot query field "url" on type "File" graphql/template-strings .我收到错误: error Cannot query field "url" on type "File" graphql/template-strings How can I fix it?我该如何解决? Thanks!谢谢!

I have faced this problem too.我也遇到过这个问题。 Tutorial on Strapi suggest to query with 'url' but it's wrong. Strapi教程建议使用 'url' 进行查询,但这是错误的。

The right way to query is to do:正确的查询方法是:

      allStrapiArticle {
        edges {
          node {
            strapiId
            title
            category {
              name
            }
            image {
              publicURL
            }
          }
        }
      }

In order to display image, don't forget to swap url into publicURL like that:为了显示图像,不要忘记像这样将url换成publicURL

  <img
    src={article.node.image.publicURL}
    alt={article.node.image.publicURL}
    height="100"
  />

Despite you've created an image object with an url field in Strapi, Strapi + Gatsby downloads it and you must change a bit your query.尽管您在 Strapi 中创建了带有url字段的图像 object,但 Strapi + Gatsby 会下载它,您必须对查询进行一些更改。

At this point, all your images belong to gatsby-source-filesystem so they must be queried in a different way.此时,您的所有图像都属于gatsby-source-filesystem ,因此必须以不同的方式查询它们。 Of course, you can get the url but your data structure may differ from the one you've created in Strapi's back office.当然,您可以获得url ,但您的数据结构可能与您在 Strapi 后台创建的数据结构不同。 In other words, the field you are looking for is inside another object, in this case, the publicURL will contain your desired url value.换句话说,您要查找的字段位于另一个 object 中,在这种情况下, publicURL将包含您想要的url值。 Here's a sample of how to get one or multiple images:以下是如何获取一张或多张图片的示例:

    singleImage {
     publicURL
    }
    multipleImages {
      localFile {
        publicURL
      }
    }

Reference: https://github.com/strapi/gatsby-source-strapi参考: https://github.com/strapi/gatsby-source-strapi

Take a look at the autocompletion when you run a gatsby develop under localhost:8000/___graphql playground to test your GraphQL query, it may help you to get the fields you need.看看当你在localhost:8000/___graphql playground 下运行 gatsby develop 来测试你的 GraphQL 查询时的自动补全,它可能会帮助你获得你需要的字段。

This tutorial also points out some interesting stuff. 本教程还指出了一些有趣的东西。

If you want to use a gatsby-image -based image, you can use:如果要使用基于gatsby-image的图像,可以使用:

  image {
    childImageSharp {
      fluid(maxWidth: 960) {
        ...GatsbyImageSharpFluid
      }
    }
  }

Then it should be used in a loop something like (with gatsby-image usage):然后它应该在类似的循环中使用(使用gatsby-image ):

<Img fluid={data.allStrapiArticle.edges[position].index.image.childImageSharp.fluid} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Graphql 从 Strapi 查询 Gatsby 中的多个图像 - How to query multiple images in Gatsby from Strapi using Graphql 无法在类型“用户”graphql 上查询字段“密码” - Cannot query field 'password' on type 'User' graphql Gatsby 和 WPGraphQL - 构建错误“无法在类型“查询”上查询字段“页面”” - Gatsby and WPGraphQL - Build Error "Cannot query field "pages" on type "Query"" Gatsby.js 的 GraphQL 无法检索 Strapi 创建的帖子类别。 但是,它显示在 Gatsby.js 的 GraphiQL 中 - Category of Post created by Strapi cannot be retrieved by GraphQL of Gatsby.js. However, it is displayed in GraphiQL of Gatsby.js Gatsby GraphQL 中用于自定义查询的未知字段错误 - Unknow field error in Gatsby GraphQL for custom query graphQL + gatsby:查询一个字段是Image还是mp4 - graphQL + gatsby: query a field that is Image or mp4 无法在 gatsby starter 博客中查询类型“MarkdownRemark”的字段“frontmatter” - Cannot query field “frontmatter” on type “MarkdownRemark” in gatsby starter blog Yaml 文件在 Gatsby Graphql 查询中返回 null - Yaml file returning null in Gatsby Graphql query Gatsby 和 GraphQL:使用查询 pdf 文件<StaticQuery/> - Gatsby and GraphQL: query pdf file using <StaticQuery/> Gatsby/Strapi 与 GraphQl 关系数据不会填充到页面上 - Gatsby/Strapi with GraphQl Relational data will not populate on page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM