【问题标题】:Where to define variables in GraphQL在 GraphQL 中定义变量的位置
【发布时间】:2021-01-03 21:37:31
【问题描述】:

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

    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
      }
    }
  }
`

【问题讨论】:

标签: reactjs graphql gatsby graphiql


【解决方案1】:

鉴于我正确设置了 gatsby-node.js,我可以通过某种方式解决以稍微不同的方式编辑 Graphql 查询的问题。

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
      }
    }
  }
`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-21
    • 2019-06-15
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    相关资源
    最近更新 更多