1

New to both Gatsby and Netlify. What I've managed to achieve so far is:

  1. Setting up a basic Gatsby project
  2. Synced it to Netlify
  3. Produced a GraphiQL query

I have an admin > config.yml file that populates the fields i can use in netlify

backend:
  name: git-gateway
  branch: master

media_folder: static/img
public_folder: /img

collections:
  - name: "pages"
    label: "Pages"
    files:
      - file: "src/pages/subscription/index.md"
        label: "Subscription Page"
        name: "Subscription Page"
        fields:
          - {label: "Template Key", name: "templateKey", widget: "hidden", default: "subscription-page"}
          - { label: 'Features Header Title', name: 'featuresHeaderTitle', widget: 'string' }
          - { label: 'Features List', name: 'featuresList', widget: 'list', 
              fields: [ 
                {label: Feature Title, name: featureTitle, widget: text},
                {label: Feature Description, name: featureDescription, widget: markdown}
                {label: Feature Published, name: featurePublished, widget: boolean}
              ] 
            }
          - { label: 'Seconday Features Header Title', name: 'secondaryFeaturesHeaderTitle', widget: 'string' }
          - { label: 'Secondary Features List', name: 'secondaryFeaturesList', widget: 'list', 
              fields: [ 
                {label: Secondary Feature Title, name: secondaryFeatureTitle, widget: text},
                {label: Secondary Feature Description, name: secondaryFeatureDescription, widget: markdown}
                {label: Secondary Feature Published, name: secondaryFeaturePublished, widget: boolean}
              ] 
            }  

Once I publish a page in Netlify and it syncs to my GitHub account, it produces an md file with the following:

---
title: Subscription
featuresHeaderTitle: 24 June - Features Header Title Again
featuresList:
  - featureTitle: Feature Title 1
    featureDescription: Feature Description 1
  - featureTitle: Feature Title 2
    featureDescription: Feature Description 2
secondaryFeaturesHeaderTitle: 24 June - Subscription Secondary Features Header Title
secondaryFeaturesList:
  - secondaryFeatureTitle: Secondary Feature Title
    secondaryFeatureDescription: Secondary Feature Description
  - secondaryFeatureTitle: Secondary feature title
    secondaryFeatureDescription: Secondary feature description
  - secondaryFeatureTitle: Secondary feature title 3
    secondaryFeatureDescription: Secondary feature description 3
---

In GraphiQL I have a query which I believe to be correct that produces the following in the Code Exporter:

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

const ComponentName = ({ data }) => <pre>{JSON.stringify(data, null, 4)}</pre>

export const query = graphql`
  {
    markdownRemark(frontmatter: {title: {eq: "Subscription"}}) {
      frontmatter {
        featuresHeaderTitle
        featuresList {
          featureDescription
          featureTitle
        }
        secondaryFeaturesHeaderTitle
        secondaryFeaturesList {
          secondaryFeatureDescription
          secondaryFeatureTitle
        }
      }
    }
  }
`

export default ComponentName

My question is, how do I display the above query in a template with HTML? I'd like to display the content as follows:

  • Features Header Title

  • List (ul/li) of the items in the feature list

  • Secondary Features Header Title

  • List (ul/li) of the items in the secondary feature list

component.js

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


const ComponentName = ({data}) => {
  return (
    <div>
      {data.markdownRemark.frontmatter.featuresHeaderTitle}
    </div>
  )
}

export const query = graphql`
  {
    markdownRemark(frontmatter: {title: {eq: "Subscription"}}) {
      frontmatter {
        featuresHeaderTitle
        featuresList {
          featureDescription
          featureTitle
        }
        secondaryFeaturesHeaderTitle
        secondaryFeaturesList {
          secondaryFeatureDescription
          secondaryFeatureTitle
        }
      }
    }
  }
`


export default ComponentName```
Haven
  • 87
  • 1
  • 9
  • The above should work if you place it as a js file in your `src/pages` folder. The docs are super helpful. Check https://www.gatsbyjs.org/docs/page-query/ and https://www.gatsbyjs.org/docs/static-query/ – ksav Jun 24 '20 at 11:10
  • 1
    Thanks! - I"ve updated my question to include my attempted HTML (missed that). The error I get is `TypeError: Cannot read property 'markdownRemark' of undefined` – Haven Jun 24 '20 at 11:25
  • 1
    Actually, when trying it as a static query (following the docs you provided)...i could get the titles to display! - Interesting. Probably need to look into the differences between page query and static query. Just need to figure out how to display a ul / li list for the features list! – Haven Jun 24 '20 at 11:28
  • 1
    Got it all working. `

    {data.markdownRemark.frontmatter.featuresHeaderTitle}

    ` - Thanks for your time :)
    – Haven Jun 24 '20 at 11:32

0 Answers0