【发布时间】:2018-07-09 23:32:13
【问题描述】:
我在一个新闻网站上工作,该网站将有文章,每篇文章都有一个或多个各自的作者。如果您单击作者的姓名,它将带您进入一个页面,显示他们的信息和他们贡献的文章列表。
所以每篇文章都有一个authors 属性,而该属性又是一个author 对象数组,其属性为:全名、slug(全名的小写版本,空格替换为破折号)等。
在定义查询时是否可以按特定作者的 slug 过滤文章?
query authorQuery($slug: String!) {
allContentfulArticle(filter: { //not sure what to do here }) {
edges {
node {
title
slug
authors {
name
slug
}
}
}
}
}
我的另一个选择是加载所有文章,然后在组件中设置一个过滤器,如下所示:
const articles = data.allContentfulArticle.edges.filter(({ node }) => {
return node.authors.some((author) => author.slug === data.contentfulAuthor.slug);
});
这不会是世界末日,但它违反了 GraphQL 只加载您需要的数据的原则。
【问题讨论】:
标签: filter graphql graphql-js contentful gatsby