首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GraphQL查询限制标记数

GraphQL查询限制标记数
EN

Stack Overflow用户
提问于 2022-04-29 17:15:35
回答 1查看 236关注 0票数 1

这是来自盖茨比项目的GraphQL查询:

代码语言:javascript
运行
复制
const Data = useStaticQuery(graphql`
    query {
      allMarkdownRemark(
        sort: { fields: [frontmatter___date], order: DESC }
        limit: 5
      ) {
        edges {
          node {
            excerpt(pruneLength: 300)
            fields {
              slug
            }
            frontmatter {
              date(formatString: "DD [<span>] MMM [</span>]")
              title
              description
              tags
            }
          }
        }
        group(field: frontmatter___tags) {
          totalCount
          fieldValue
        }
      }
    }
  `);

  const Posts = Data.allMarkdownRemark.edges;
  const Tags = Data.allMarkdownRemark.group;

上面的代码用于显示最新的5个帖子和所有标签(包括每个标签的计数)。问题是在显示了5个最新的帖子之后,它只显示了标签数5。

如果按下面所示对组部分应用2000的限制,则在进行gatsby开发时会出现错误。

代码语言:javascript
运行
复制
group(field: frontmatter___tags, limit: 2000)

下面是查询的GraphQL IDE输出:

代码语言:javascript
运行
复制
{
  allMarkdownRemark(sort: {fields: id}) {
    group(field: frontmatter___tags) {
      totalCount
      fieldValue
    }
  }
}

产出:

代码语言:javascript
运行
复制
{
  "data": {
    "allMarkdownRemark": {
      "group": [
        {
          "totalCount": 27,
          "fieldValue": "android"
        },
        {
          "totalCount": 24,
          "fieldValue": "c"
        },
        {
          "totalCount": 42,
          "fieldValue": "chash"
        },
        {
          "totalCount": 31,
          "fieldValue": "cplus"
        },
        {
          "totalCount": 36,
          "fieldValue": "css"
        },
        {
          "totalCount": 22,
          "fieldValue": "html"
        },
        {
          "totalCount": 24,
          "fieldValue": "html5"
        },
        {
          "totalCount": 30,
          "fieldValue": "java"
        },
        {
          "totalCount": 11,
          "fieldValue": "jsp"
        },
        {
          "totalCount": 18,
          "fieldValue": "mysql"
        },
        {
          "totalCount": 37,
          "fieldValue": "networking"
        },
        {
          "totalCount": 33,
          "fieldValue": "php"
        },
        {
          "totalCount": 8,
          "fieldValue": "xml"
        }
      ]
    }
  },
  "extensions": {}
}

不知何故,最新帖子的5的限制被应用到要显示的标签的计数上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-01 15:22:10

在这种情况下,通过为每个查询提供一个别名来运行多个查询。

下面是一个示例:

代码语言:javascript
运行
复制
const Data = useStaticQuery(graphql`
    query {
      posts: allMarkdownRemark(
        sort: { fields: [frontmatter___date], order: DESC }
        limit: 5
      ) {
        edges {
          node {
            excerpt(pruneLength: 300)
            fields {
              slug
            }
            frontmatter {
              date(formatString: "DD [<span>] MMM [</span>]")
              title
              description
              tags
            }
          }
        }
      }
      tags: allMarkdownRemark(
        sort: { fields: [frontmatter___date], order: DESC }
        limit: 1000
      ) {
        group(field: frontmatter___tags) {
          totalCount
          fieldValue
        }
      }
    }
  `);

  const Posts = Data.posts.edges;
  const Tags = Data.tags.group;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72061540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档