Search code examples
reactjsgraphqlgatsbygraphiql

Where to define variables in GraphQL


Excuse this newbie here. I want to list some articles based on their categories, now I have categories page where you click a category, a list of articles under that specific category should open. The question is, where to define the variable for the slug [which is equal to the article's category]. Where to define $slug variable which is equal to the category, given that I come from categories page, this must be a post request which sends the clicked category, I should put it somewhere on that page, can someone guide me if I make any sense here?! Thanks in advance.

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

Here is my gatsby-node.js file

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,
      },
    })
  })

Here is the [parent] education page which I want to take the slug from

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

Solution

  • I somehow could reolve the issue, which was editing the Graphql query in a slightly different way, given that I had gatsby-node.js set up correctly.

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