无法查询类型“y”上的字段“x”

2024-01-22

我需要 Graphql 查询方面的帮助:)。 我正在尝试将投资组合网站部署到 Netlify,但在部署过程中出现此错误。 我的代码可以在 localhost 上运行,但不能在 Netlify 上运行。 我需要从 Contentful 中提取数据并用它填充图像库。

预期结果: Netlify 部署我的代码

我得到的结果: 构建错误。

There was an error in your GraphQL query:
2:51:25 AM: Cannot query field "tags" on type "ContentfulArt".
2:51:25 AM: If you don't expect "tags" to exist on the type "ContentfulArt" it is most likely a typo.
However, if you expect "tags" to exist there are a couple of solutions to common problems:
2:51:25 AM: - If you added a new data source and/or changed something inside gatsby-node.js/gatsby-config.js, please try a restart of your development server
2:51:25 AM: - The field might be accessible in another subfield, please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have
2:51:25 AM: - You want to optionally use your field "tags" and right now it is not used anywhere. Therefore Gatsby can't infer the type and add it to the GraphQL schema. A quick fix is to add at least one entry with that field ("dummy content")
2:51:25 AM: It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "ContentfulArt":
https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions

Gatsby-node.js 文件

enter code here
 const path = require('path')

module.exports.createPages = async ({graphql, actions}) => {
  const { createPage } = actions
  const portfolioTemplate = path.resolve('./src/templates/gallerylist.js')
  const { data }  = await graphql(
    `
  query{
    allContentfulArt{
    edges{
      node{
        title
        tags
        publishedDate(formatString:"MMMM Do, YYYY")
        image{
          file{
              url
              }
          }
      }
    }
    }
}
`
)
//error handling


//amount of images per page = 8
  const imgPerPage = 8
  const numPages = Math.ceil(data.allContentfulArt.edges.length / imgPerPage)
//we calculate math ceiling from the total amount of objects in our query by edges.length then divide it by 8.

//we create page for each index in Numpages, ignoring the callback with "_,", path = path where we will create pages
//component - template that contain the endpoints for our create page code
//context - information to pass down into the created page
  Array.from({length:numPages}).forEach((_, i) => {
    actions.createPage({
      path: i === 0 ? `/portfolio/${1}` : `/portfolio/${i + 1}`,
      component:portfolioTemplate,
      context: {
        limit: imgPerPage,
        skip: i * imgPerPage,
        numPages,
        currentPage: i + 1
      }
    })
  })

}

我的模板文件

import React from 'react'
import {graphql} from 'gatsby'

import Zoom from 'react-medium-image-zoom'
import Footer from '../components/footer'
import Header from '../components/header'
import Head from '../components/head'
import 'react-medium-image-zoom/dist/styles.css'
import Pagination from '../components/pagination'

import portfolioStyles from '../pages/portfolio.module.scss'


const GalleryList = ({ pageContext, data}) => {
    const {currentPage, numPages} = pageContext
    const isFirst = currentPage === 1
    const isLast = currentPage === numPages
    /*if prev page equals to currentpage -1 we return return to the first page, otherwise we go down 1 page*/ 
    const prevPage = currentPage - 1 === 1 ? `/portfolio/${1}` : `/portfolio/${currentPage - 1}`
    /*next page = currentpage + 1*/ 
    const nextPage = `/portfolio/${currentPage + 1}`


/* this will return a div container that conain gallery page 
we are passing data via different predefined constants and variables
*/
    return (
        <div className={portfolioStyles.maincontainergallery}>
            <Head title="Gallery" />

            <div className={portfolioStyles.headercomponent}>
            <Header />
            {/* Header container that includes menu and a pagination */}
            <div className={portfolioStyles.portfoliopagepaginationcontainer}>
                {/* This is pagination component with passed down props to manage the pagination */}
        <Pagination 
        isFirst={isFirst}
        isLast={isLast}
        prevPage={prevPage}
        nextPage={nextPage}/>
            </div>
            </div>

            {/* This is a gallery div container that receives data from Graphql query*/}
        <div className={portfolioStyles.portfoliopagegallerycontainer}>
        <div className={portfolioStyles.portfoliopagegallery}>
            {
            data.allContentfulArt.edges.map((edge) => {
                return (
                //we grabbing data from Contenful API using Graphql query 
                //Zoom allows us to click-zoom the images passed down from Contenftul API
                <figure  className={portfolioStyles.portfoliopagegalleryfigure} >
                
                <h3 >
                <figcaption>
                {edge.node.title}
                </figcaption>
                </h3>

                <p>{edge.node.tags}</p>

                <p>{edge.node.publishedDate}</p>
                <Zoom>
                <img src={edge.node.image.file.url} width="250" alt={edge.node.title}/>
                </Zoom>
                
                </figure>
                
                    
                )
            })
        }
        </div>
        

        
        </div>

        
        {/* this is a footer container with a footer component */}
        <div className={portfolioStyles.portfoliopagefootercontainer}>
            <Footer />
            </div>
        </div>
    )
}
/* This is graphql query to grab the code from a Contentful API */
export const pageQuery = graphql`
query($skip: Int!, $limit: Int!){
    allContentfulArt(skip: $skip, limit: $limit){
        edges{
        node{
            title
            tags
            publishedDate(formatString:"MMMM Do, YYYY")
            image{
                file{
                    url
                    }
                }
            }  
        }
        }
}`

export default GalleryList

这是因为您的 Contentful 架构allContentfulArt定义了一个tags字段作为字符串,默认情况下不能为空。它总是期望得到一个结果,至少是一个contentfulArt.

理想的解决方案是通过添加类型定义来自定义您的架构 https://www.gatsbyjs.com/docs/schema-customization/#creating-type-definitions

为了暂时绕过这个问题,您可以做的一件简单的事情就是为您的产品增加价值tags字段或添加空白值并在获取数据后修剪它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法查询类型“y”上的字段“x” 的相关文章

随机推荐

  • 如何在 php 中反转数组而不使用数组反转方法

    我想知道如何在不使用 array reverse 方法的情况下反转数组 我有一个名为反向数组的数组 这是我想要反转的数组 我的代码如下 有人可以指出我做错了什么 因为我在其他地方找不到任何以这种方式反转数组的示例 我的代码如下
  • C 中的字符串分割器 - 它是如何工作的?

    我继承了一个庞大的代码库 并且有一个实用函数可以分割字符串 字符 我了解大约 80 的运作方式 但我不明白 token 0 line 任何指点都将受到高度赞赏 include
  • tomcat websocket servlet监听端口

    我正在尝试编写一个使用的 servletorg apache catalina websocket WebSocketServlet 我找到了一个 websocket 聊天的示例 但我不知道如何指定 websocket 服务器的侦听端口 在
  • 如何隐藏 TVirtualStringTree 节点?

    如果我没记错的话 在一个节点中不可能有不可见的节点 TVirtualStringTree 树视图 因为没有启用 可见或其他 属性来这样做 我的说法正确吗 如果是 我如何设法拥有不可见的节点 我的树的结构 One Node Another n
  • Android:如何获取当前主题的资源ID?

    在 Android 中 您可以获取 Activity 的当前主题Resource Theme对象来自getTheme 另外 您可以通过其他主题的资源 ID 将主题设置为不同的主题 如下所示setTheme R style Theme MyT
  • 无法从tensorflow.keras.metrics导入指标

    我想编写一个我正在关注的自定义指标评估器这个链接 https www tensorflow org beta guide keras training and evaluation specifying a loss metrics and
  • Django runserver 在开发中不提供静态文件

    我正在使用 Djangorunserver为了我的发展 当我部署到生产服务器时 我可以看到所有静态文件 但看不到本地计算机上的文件 I did collectstatic我已经设置了DEBUG True 我在网上找到了很多不同的意见 其中最
  • mpi 中的 darray 和 subarray 有什么区别?

    我有一个用于并行编程类的并行 I O 项目 并且我必须实现派生数据类型 我不太清楚darray和subarray之间的区别 darray 是否可以从动态分配的数组派生 主要区别是什么 子数组可让您描述较大多维数组的单个块 切片 如果每个 M
  • 使用“Microsoft Print to PDF”和 Java 将文档转换为 PDF

    我目前正在 Microsoft Windows 主机上测试将 RTF DOC 文档转换为 PDF 我有一段使用 Microsoft Word API 的工作和平代码 但由于许可证成本 我想摆脱它 我的想法是 只需将 RTF 发送 到 Mic
  • 如何删除git中未暂存的更改[换行符差异]?

    这很令人沮丧 我根本找不到如何处理这个问题的正确答案 我正在执行变基操作 但这只是发生此问题的众多场景之一 并且我有大量 已更改但未更新 的文件 除了换行符之外没有任何区别 git diff b 什么也没有返回 现在我只想删除更改并将文件保
  • 异步瀑布相当于 Q

    我有一个页面 它是帐户设置页面 在其中 我允许我的用户更新他们的头像 如果他们附加了图像 更改他们的电子邮件 如果已更改为原始电子邮件 以及更改他们的名称和密码 现在 我正在使用异步waterfall方法 但我将 async 替换为 Q 因
  • 在 SciTE 中重新格式化 80 列中的文本(或者更好的是 LaTeX)

    我最近在 Lix 这样的所见即所得编辑器的帮助下开始研究 LaTeX 现在我开始在 Sci TE 中编写 tex 文件 它已经具有语法高亮显示 并且我调整了 tex properties 文件以在 Windows 中工作 显示 Go 上的预
  • Swift 类不可构造

    我正在按照 iBook 进行快速编程 但是当我尝试使用 var 构造一个类时出现错误 这是一个结构和一个类 struct Resolution var width 0 var height 0 class VideoMode var res
  • 将宽图的同一等级上的节点分布到不同的线上

    我有一个图表 组织图 digraph G nodesep 0 3 ranksep 0 2 margin 0 1 node shape rectangle edge arrowsize 0 8 1 gt 2 1 gt 3 1 gt 4 1 g
  • 检测到“时间戳”类型的对象与预期实例不匹配

    我想知道为什么 Timestamp 对象没有按我的预期工作 它在测试环境中工作 我使用Mocha 但在部署时抛出错误 index ts import Timestamp QuerySnapshot from google cloud fir
  • 为什么我们可以使用“this”作为实例方法参数?

    什么是接收器参数在爪哇 Java 8 语言规范谈论this JLS 给出a hint http docs oracle com javase specs jls se8 html jls 8 html jls 8 4 1 220 无论哪种方
  • 垃圾收集和毕加索的问题

    我正在尝试在 Google 地图标记的 InfoWindow 中设置 ImageView 并复制了代码这个答案 https stackoverflow com a 22043781非常准确 除了我的 InfoWindowAdapter 不是
  • 如何获取OSM节点或路径的城市和国家?

    我正在解析 OSM 数据的国家 地区摘录 我想要的是获取该点或方式所属的城市和国家的名称 这可能吗 我怎样才能获取这些信息 要检索此信息 您需要地理编码器 除非该节点具有相关的地址标签 https wiki openstreetmap or
  • R:如何对pairs()图中的对角线面板进行着色?

    以下代码将面板背景着色pairs情节在R 如何为对角线面板 打印变量名称的地方 着色 正如您所看到的 我尝试了它 但变量名称未正确对齐 无论出于何种原因 count lt 0 mypanel lt function x y count lt
  • 无法查询类型“y”上的字段“x”

    我需要 Graphql 查询方面的帮助 我正在尝试将投资组合网站部署到 Netlify 但在部署过程中出现此错误 我的代码可以在 localhost 上运行 但不能在 Netlify 上运行 我需要从 Contentful 中提取数据并用它