无法使用 Gatsbyjs 查询类型 "Query" GraphCMS 上的字段 "image"

Cannot query field "image" on type "Query" GraphCMS with Gatsbyjs

晚上好, 我是 Gatsby 和 Graphql 的新手。我正在试验 CMS 系统,但我不知道这个错误是什么意思。任何帮助,将不胜感激。下面标记的是我遇到的错误。我的根文件夹中确实有文件夹 graphcms-fragmetns。 我已经按照那里的文档进行操作,但没有用。

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,
      }
    }
  ],
};

Cannot query field "image" on type "Query"

这基本上意味着您的查询中没有 image 节点,因此您不能使用 GraphQL 查询。

根据documentation,使用图像的方式应该是这样的查询:

{
  allGraphCmsAsset {
    nodes {
      gatsbyImageData(layout: FULL_WIDTH)
    }
  }
}

在这种情况下,allGraphCmsAsset 是由 gatsby-source-graphcms 插件创建的主节点,您可以在其中存储图像。如果您在 GraphiQL playground (localhost:8000/___graphql) 中对其进行测试,您将看到所有可用的查询节点,因此请尝试并在那里准备您的查询。

在您的情况下,由于您对节点进行了一些自定义,我认为您的查询应如下所示:

{
  allGraphCmsAsset {
    nodes {
      image {
         gatsbyImageData(layout: FULL_WIDTH)
      }
    }
  }
}

但正如我所说,这将与您的 GraphCMS 字段、定制和实施严格相关。换句话说,要获取节点和数据,请使用 GraphiQL playground。在那里建造它。

您的最终结构应如下所示:

从“反应”导入反应 从“gatsby”导入{graphql}

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 组件之外,这与您提供的代码段不同。

有用的资源: