简体   繁体   English

盖茨比没有找到graphql片段

[英]Gatsby not finding graphql fragment

I'm trying to use GraphQL fragments on a Gatsby site, I tried to follow guides to create this fragment but Gatsby throws an error stating that my fragment does not exist我正在尝试在 Gatsby 站点上使用 GraphQL 片段,我尝试按照指南创建此片段,但 Gatsby 抛出一个错误,指出我的片段不存在

The fragment "PageFragment" does not exist.

I located my fragment in a folder called "graphql-fragments" located in the root of the Gatsby project, this folder contains one file called "pageFragment.js" which is my fragment pageFragment.js我将片段放在 Gatsby 项目根目录下名为“graphql-fragments”的文件夹中,该文件夹包含一个名为“pageFragment.js”的文件,它是我的片段 pageFragment.js

import { graphql } from "gatsby";

export const PageFragment = graphql`
  fragment PageFragment on SanityPage {
    slug {
      current
    }
    _rawContent(resolveReferences: { maxDepth: 20 })
    _id
    title
  }
`;

I use this fragment on my index page where I also get my error我在我的索引页面上使用了这个片段,我也得到了我的错误

import React from "react"
import { graphql } from "gatsby";

export const query = graphql`
  query FrontpageQuery {
    page: sanityPage(_id: { regex: "/(drafts.|)frontpage/" }) {
      ...PageFragment
    }
  }
`;


const Home = () => {
  return (
    <div>
      Hello world
    </div>
  )
}


export default Home

Running the graphQL query without the fragment works fine, so this works and I am getting the data I expect在没有片段的情况下运行 graphQL 查询工作正常,所以这有效,我得到了我期望的数据

export const query = graphql`
  query FrontpageQuery {
    page: sanityPage(_id: { regex: "/(drafts.|)frontpage/" }) {
      slug {
        current
      }
      _rawContent(resolveReferences: { maxDepth: 20 })
      _id
      title
    }
  }
`;

It seems like Gatsby is not finding my fragment, any clues to what I'm doing wrong?盖茨比似乎没有找到我的片段,任何关于我做错了什么的线索? Running on Gatsby version 2.24.66在 Gatsby 版本 2.24.66 上运行

Here is a quote from Gatsby docs:这是Gatsby 文档中的一段话

To create a fragment, define it in a query and export it as a named export from any file Gatsby is aware of .要创建片段,请在查询中定义它并将其导出为Gatsby 知道的任何文件的命名导出。 A fragment is available for use in any other GraphQL query, regardless of its location in the project.片段可用于任何其他 GraphQL 查询,无论其在项目中的位置如何。 Fragments are globally defined in a Gatsby project, so names have to be unique.片段在 Gatsby 项目中是全局定义的,因此名称必须是唯一的。

The file that export the fragment can not be stored in the root folder of the project where gatsby-config.js etc is located.导出fragment的文件不能存放在gatsby-config.js等所在项目的根文件夹中。 If the Gatsby source files are in a folder called src then the file that export the fragment has to be in the src folder or one of its subfolders.如果 Gatsby 源文件位于名为src的文件夹中,则导出片段的文件必须位于src文件夹或其子文件夹之一中。

As a side note: one thing I sometimes forget is to add import { graphql } from "gatsby" , but that's not the case here.附带说明:我有时会忘记的一件事是添加import { graphql } from "gatsby" ,但这里并非如此。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM