从Gatsby Graphql查询中的数组访问数据

时间:2020-02-08 03:56:31

标签: reactjs graphql gatsby

我在这里很困惑。我有一个Gatsby网站,我正在设置netlify cms。我已经设置好配置文件,可以从netlify admin中创建页面和小部件。当我运行graphql查询时,我可以提取页面信息,但是无法从小部件访问数据。

admin / config.yml

  name: git-gateway
  branch: master # Branch to update (optional; defaults to master)

media_folder: "src/images" # Media files will be stored in the repo under static/images/uploads

collections:
  - name: "pages"
    label: "Page"
    folder: "src/markdown-pages"
    create: true
    fields:
      - {label: "Title", name: "title"}
      - {label: "Content", name: "content"}
      - {label: "Slug", name: "slug"}
      - {label: "Path", name: "path"}
      - label: "Page Sections"
        name: "sections"
        widget: "list"
        types:
          - label: "Call To Action"
            name: "calltoaction"
            widget: object
            fields:
              - {label: "Title", name: "title", widget: "string"}
              - {label: "Content", name: "content", widget: "markdown", required: false}
              - {label: "Call to Action Link", name: "link", widget: "string", required: false, hint: "Call to Action Link"}
              - {label: "Call to Action Text", name: "buttontext", widget: "string", required: false, hint: "Call to Action Text"}
          - label: "Services"
            name: "services"
            widget: object
            fields:
              - {label: "Title", name: "title", widget: "string"}
              - {label: "Content", name: "content", widget: "markdown", required: false}

gatsby-node.js


exports.createPages = ({actions, graphql}) => {
  const {createPage} = actions;

  const pageTemplate = path.resolve('src/templates/page.js');
  return graphql(`{
    allMarkdownRemark {
      edges {
        node {

          id
          frontmatter {
            title
            path
            sections {
              buttontext
              content
              link
              title
            }
          }
        }
      }
    }
  }`)
  .then(result => {
    if (result.errors) {
      result.errors.forEach(e => console.error(e.toString()))
      return Promise.reject(result.errors)
    }

    result.data.allMarkdownRemark.edges.forEach(({node}) => {
      createPage({
        path: node.frontmatter.path,
        component: pageTemplate,
        context: {
          id: node.id,
        }
      })
    })
  })
}

pagetemplate.js

import Helmet from 'react-helmet'
import Layout from "../components/layout"
//import CTA from '../components/sections/CTA-section'

export default function Template({ data }) {
  const {markdownRemark: page} = data

return (
  <Layout>
    <h1>{page.frontmatter.title}</h1>
    <p>{page.frontmatter.sections.title}</p>
    // <CTA />
  </Layout> 
)
}

export const pageQuery = graphql`
  query pageByPath($id: String!) {
    markdownRemark(id: { eq: $id } ) {
      id
      frontmatter {
        path
        title
        sections {
          title
          buttontext
          content
          link
        }
      }
    }
  }
`

GraphiQL屏幕截图 graphiql screenshot

奖金问题:我还需要弄清楚如何制作这些小部件的组件。从pagetemplate.js中注释掉的行中可以看到,我想为每个小部件制作单独的组件。由于数据是动态的,因此我认为我将无法使用StaticQuery,并且由于将导入相同的数据,因此无法对它进行另一个页面查询。

0 个答案:

没有答案