简体   繁体   English

使用 Gatsby 如何从单个 GraphQL 查询中获取标签的总数?

[英]With Gatsby how to get a tag's total count from a single GraphQL query?

Reading querying data in Gatsby I've created a component that I'm trying to get all counts for each tag, example:在 Gatsby 中读取查询数据我创建了一个组件,我试图获取每个标签的所有计数,例如:

count.js:计数.js:

import React from 'react'

import { useStaticQuery, graphql, Link } from 'gatsby'

const Count = () => {
  const data = useStaticQuery(graphql`
    query MyQuery {
      markdownRemark {
        frontmatter {
          subject
        }
      }
    }
  `)

  const subjects = data.markdownRemark.frontmatter.subject

  return (
    <>
      <ul>
        {subjects.map(subject => (
          <li key={subject}>
            <Link to={subject}>{subject}</Link>
          </li>
        ))}
      </ul>
    </>
  )
}

export default Count

markdown frontmatter: markdown 前端:

title: FooBar
date: 2021-02-01
subject: ['mon', 'tues']
featimg: foobar.png

when I examine GraphiQL I'm only able to see one area and it's under markdownRemark that will tell me the total tag list:当我检查 GraphiQL 时,我只能看到一个区域,它位于markdownRemark下方,它将告诉我总标签列表:

{
  "data": {
    "markdownRemark": {
      "frontmatter": {
        "subject": [
          "mon",
          "tues",
          "wed"
        ]
      }
    }
  },
}

Research研究

For the record I am not trying to do this for WordPress I'm using markdown.作为记录,我不想为 WordPress 执行此操作,我使用的是 markdown。 Is there a way in GraphQL to get a list of all tags and how many markdown files have each tag in a single query that I do not have to loop and iterate through? GraphQL 中是否有办法获取所有标签的列表以及在单个查询中有多少 markdown 文件具有每个标签,我不必循环和迭代? Is this possible in Gatsby and GraphQL?这在盖茨比和 GraphQL 中是否可行?

Reading querying data in Gatsby I've created a component that I'm trying to get all counts for each tag, example:在 Gatsby 中读取查询数据我创建了一个组件,我试图获取每个标签的所有计数,例如:

count.js:计数.js:

import React from 'react'

import { useStaticQuery, graphql, Link } from 'gatsby'

const Count = () => {
  const data = useStaticQuery(graphql`
    query MyQuery {
      markdownRemark {
        frontmatter {
          subject
        }
      }
    }
  `)

  const subjects = data.markdownRemark.frontmatter.subject

  return (
    <>
      <ul>
        {subjects.map(subject => (
          <li key={subject}>
            <Link to={subject}>{subject}</Link>
          </li>
        ))}
      </ul>
    </>
  )
}

export default Count

markdown frontmatter: markdown 前端:

title: FooBar
date: 2021-02-01
subject: ['mon', 'tues']
featimg: foobar.png

when I examine GraphiQL I'm only able to see one area and it's under markdownRemark that will tell me the total tag list:当我检查 GraphiQL 时,我只能看到一个区域,它位于markdownRemark下方,它将告诉我总标签列表:

{
  "data": {
    "markdownRemark": {
      "frontmatter": {
        "subject": [
          "mon",
          "tues",
          "wed"
        ]
      }
    }
  },
}

Research研究

For the record I am not trying to do this for WordPress I'm using markdown.作为记录,我不想为 WordPress 执行此操作,我使用的是 markdown。 Is there a way in GraphQL to get a list of all tags and how many markdown files have each tag in a single query that I do not have to loop and iterate through? GraphQL 中是否有办法获取所有标签的列表以及在单个查询中有多少 markdown 文件具有每个标签,我不必循环和迭代? Is this possible in Gatsby and GraphQL?这在盖茨比和 GraphQL 中是否可行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM