这是来自盖茨比项目的GraphQL查询:
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开发时会出现错误。
group(field: frontmatter___tags, limit: 2000)下面是查询的GraphQL IDE输出:
{
allMarkdownRemark(sort: {fields: id}) {
group(field: frontmatter___tags) {
totalCount
fieldValue
}
}
}产出:
{
"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的限制被应用到要显示的标签的计数上。
发布于 2022-05-01 15:22:10
在这种情况下,通过为每个查询提供一个别名来运行多个查询。
下面是一个示例:
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;https://stackoverflow.com/questions/72061540
复制相似问题