在 GraphQL 中定义变量的位置

Where to define variables in GraphQL

在此打扰这位新手。我想根据类别列出一些文章,现在我有类别页面,您可以在其中单击一个类别,应该打开该特定类别下的文章列表。问题是,在哪里定义 slug 的变量 [等于文章的类别]。 在哪里定义等于类别的 $slug 变量,假设我来自类别页面,这必须是一个 post 发送点击类别的请求,我应该把它放在那个页面的某个地方,有人可以指导我吗如果我在这里有任何意义?! 提前致谢。

    const EduCatTemp = ({data}) => {
        const {allStrapiEducations:{nodes:educations}} = data
        return (
            <Layout>
                {
                educations.map((education)=> {
                    return (
                       <p>{education.title}</p>
                    )
                })
    }
            </Layout>
        )
    }
    
    export default EduCatTemp
    
    export const query = graphql`
      {
        allStrapiEducations(filter: {education_category: {slug: {eq: $slug}}}) {
          nodes {
            title
          }
        }
      }

这是我的 gatsby-node.js 文件

educations: allStrapiEducations {
          nodes {
              slug
          }
      }
education_categories: allStrapiEducationCategories {
        nodes {
            slug
        }
    }
result.data.educations.nodes.forEach(education => {
    createPage({
      path: `/education/${education.slug}`,
      component: path.resolve(`src/templates/education-template.js`),
      context: {
        slug: education.slug,
      },
    })
  })
  result.data.education_categories.nodes.forEach(educat => {
    createPage({
      path: `/education/${educat.slug}`,
      component: path.resolve(`src/templates/education-category-template.js`),
      context: {
        slug: educat.slug,
      },
    })
  })

这里是[家长]教育页面,我想从中拿走鼻涕虫

import React from 'react'
import Layout from '../components/layout'
import EducationCard from '../components/EducationComponent/EducationCard'
import Category from '../components/utilities/Category'

const Education = ({data}) => {
    const {allStrapiEducationCategories:{nodes:educations}} = data
    return (
        <Layout>
          <div className="p-5">
            <h1 className="sm:text-3xl text-2xl font-medium title-font text-gray-900 headline py-10">Beekeeping Programs</h1>
            <input type="search" id="gsearch" placeholder="Search..." name="gsearch" className="my-5 py-2 search-button lg:w-1/3" />
            <div className="flex flex-wrap mx-auto">
            {educations.map((education)=> {
                    return <EducationCard headline={education.name} image={education.picture.childImageSharp.fixed} slug={`/education/${education.slug}`} />
                })}
            </div>
            </div>
        </Layout>
    )
}

export default Education

export const query = graphql`
  {
    allStrapiEducationCategories {
      nodes {
        slug
        picture {
          childImageSharp {
            fixed(width: 400
              height: 200) {
              ...GatsbyImageSharpFixed
            }
          }
        }
        name
      }
    }
  }
`

我以某种方式解决了这个问题,即以稍微不同的方式编辑 Graphql 查询,前提是我 gatsby-node.js 设置正确。

export const query = graphql`
query getSingleNewsCategory($slug: String!)
  {
    strapiNewsCategories(slug: { eq: $slug }) {
      name
      }
      allStrapiIndustries(
        filter: {news_category: {slug: {eq: $slug}}}
        ) {
        nodes {
          report_photo {
            childImageSharp {
              fluid {
                src
              }
            }
          }
          quote
          content
          title
          slug
          minutes_read
          date(formatString: "MM")
            article_author {
            name
          }
        }
    }
    allStrapiNewsCategories {
      nodes {
        slug
        name
      }
    }
  }
`