James
James

Reputation: 1485

Gatsby Query createPage context options from page template

I'm trying to query, either through props or graphql, a key-value pair I am testing inside the context option from within Gatsby's createPage function. See code:

 createPage({
        path: node.frontmatter.path,
        component: blogPostTemplate,
        context: {
          mycontext: "My Context James",
        }, // additional data can be passed via context
      })

My page query is as follows:

export const pageQuery = graphql`
  query($path: String!) {
    markdownRemark(frontmatter: { path: { eq: $path } }) {
      html
      frontmatter {
        date(formatString: "MMMM DD, YYYY")
        path
        title
      }
    }
  }
`

How do I query for the context as well as thehtml and frontmatter nodes?

Many thanks

Upvotes: 0

Views: 746

Answers (1)

ksav
ksav

Reputation: 20830

Pass context to pages. When in doubt, check the docs.

import React from 'react'
import { graphql } from 'gatsby'

const Page = ({ data, pageContext }) => {
  return (
    <>
      <p>Context: { pageContext.mycontext }<p>
      <p>Title: { data.markdownRemark.frontmatter.title }<p>
    </>
  )
}

export default Page

export const pageQuery = graphql`
  query($path: String!) {
    markdownRemark(frontmatter: { path: { eq: $path } }) {
      html
      frontmatter {
        date(formatString: "MMMM DD, YYYY")
        path
        title
      }
    }
  }
`

Upvotes: 2

Related Questions