Ryan
Ryan

Reputation: 1232

Referencing GraphQL variable used in query in Gatsby template?

I am aggregating all posts that have a certain tag in it's tag list:


export const Search = graphql`
  query SearchResults($tag: String = "") {
    allMarkdownRemark(filter: { frontmatter: { tags: { in: [$tag] } } }) {
      nodes {
        frontmatter {
          title
          date
        }
      }
    }
  }
`

The query works great, but I want to also be able to dynamically show the tag that is being queried for. How can I pass this information through?

For example: Searching results for tag: Java which would be the value inside $tag in the graphql query.

I tried to pull from the URL, but it's rendered in node, so I don't have access to the window object and it felt a bit hacky anyway.

Upvotes: 0

Views: 65

Answers (1)

Ryan
Ryan

Reputation: 1232

I got it. Passing in props.pageContext gives you access to the contextual information passed in via gatsby-node.

Upvotes: 1

Related Questions