[英]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.