晚上好,我是盖茨比和图形to的新手。我正在试验CMS系统,但我不知道这个错误意味着什么。任何帮助都将不胜感激。下面标记的是我一直在犯的错误。我在根文件夹中有一个文件夹graphcms-片段。我已经跟踪了已经存在的文档,但是它没有起作用。
Cannot query field "image" on type "Query" Graphiql也只显示对本地机器的查询。我不知道我错过了什么。
index.js
import * as React from "react"
import {useState} from "react";
import {useStaticQuery, graphql} from "gatsby"
const IndexPage = () => {
const [query, setQuery] = useState([{}])
const testQuery = graphql`
{
image {
images{
id
}
}
}
`
const test1 = useStaticQuery(testQuery);
console.log(test1);
return (
<main style={pageStyles}>
<title>Home Page</title>
<h1 style={headingStyles}>
Congratulations
<br />
<span style={headingAccentStyles}>— you just made a Gatsby site! </span>
<span role="img" aria-label="Party popper emojis">
</span>
</h1>
<p style={paragraphStyles}>
Edit <code style={codeStyles}>src/pages/index.js</code> to see this page
update in real-time.{" "}
<span role="img" aria-label="Sunglasses smiley emoji">
</span>
</p>
<ul style={listStyles}>
<li style={docLinkStyle}>
<a
style={linkStyle}
href={`${docLink.url}?utm_source=starter&utm_medium=start-page&utm_campaign=minimal-starter`}
>
{docLink.text}
</a>
</li>
{}
</ul>
<img
alt="Gatsby G Logo"
src="data:image/svg+xml,%3Csvg width='24' height='24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M12 2a10 10 0 110 20 10 10 0 010-20zm0 2c-3.73 0-6.86 2.55-7.75 6L14 19.75c3.45-.89 6-4.02 6-7.75h-5.25v1.5h3.45a6.37 6.37 0 01-3.89 4.44L6.06 9.69C7 7.31 9.3 5.63 12 5.63c2.13 0 4 1.04 5.18 2.65l1.23-1.06A7.959 7.959 0 0012 4zm-8 8a8 8 0 008 8c.04 0 .09 0-8-8z' fill='%23639'/%3E%3C/svg%3E"
/>
</main>
)
}
export default IndexPage
module.exports = {
siteMetadata: {
siteUrl: "https://www.yourdomain.tld",
title: "Playground",
},
plugins: [
"gatsby-plugin-sass",
"gatsby-plugin-image",
"gatsby-plugin-react-helmet",
"gatsby-plugin-sitemap",
"gatsby-plugin-sharp",
"gatsby-transformer-sharp",
{
resolve: "gatsby-source-filesystem",
options: {
name: "images",
path: "./src/images/",
},
__key: "images",
},
{
resolve: 'gatsby-source-graphcms',
options: {
typeName: 'GraphCMS',
fieldName: 'image',
endpoint: "URL endpoint,
}
}
],
};

发布于 2021-08-01 11:27:03
无法在“查询”类型上查询字段“图像”
这基本上意味着查询中没有image节点,因此不能使用GraphQL查询。
根据文档,使用图像的方式应该是如下所示的查询:
{
allGraphCmsAsset {
nodes {
gatsbyImageData(layout: FULL_WIDTH)
}
}
}在这种情况下,allGraphCmsAsset是由gatsby-source-graphcms插件创建的主要节点,您可以在其中存储图像。如果您在GraphiQL操场(localhost:8000/___graphql)上测试它,您将看到要查询的所有可用节点,因此尝试并在那里准备查询。
在您的示例中,由于您正在自定义一些节点,所以我认为您的查询应该如下所示:
{
allGraphCmsAsset {
nodes {
image {
gatsbyImageData(layout: FULL_WIDTH)
}
}
}
}但正如我所说,它将严格地与您的GraphCMS字段、自定义和实现相关。换句话说,要获取节点和数据,请使用GraphiQL操场。把它建在那里。
您的最终结构应该如下所示:
从"gatsby“导入{ graphql }的" React”
function IndexPage({ data }) {
return (
<ul>
{data.allGraphCmsAsset.nodes.map(image=> (
<li key={image.id}>
<GatsbyImage image={image.gatsbyImageData} alt="" />
</li>
))}
</ul>
)
}
export const pageQuery = graphql`
query IndexPageQuery {
allGraphCmsAsset {
nodes {
image {
id
gatsbyImageData(layout: FULL_WIDTH)
}
}
}
}
`
export default IndexPage检查pageQuery是如何在IndexPage组件之外的,不像您提供的片段。
有用的资源:
https://stackoverflow.com/questions/68607946
复制相似问题