首页 > 解决方案 > 带有过滤/条件渲染的 Strapi Graphql 查询

问题描述

在我的 Strapi 项目中,我正在尝试编写一个通过标签数组过滤帖子的 Graphql 查询。帖子必须属于给定数组的所有标签。例如,如果给定数组是 [1,2,3],则下面的帖子将不满足条件,但如果数组是 [3,5],则将通过条件。

任何帮助将非常感激

示例帖子:

 {
      "id": "1",
      "title": "Lorem Iopsum",       
      "created_at": "2021-02-19T22:53:19.204Z",
      "tags": [
        {
          "id": "3",
          "tag": "Porte De Gentily"
        },
        {
          "id": "5",
          "tag": "Bridges"
        },  
        {
          "id": "6",
          "tag": "Towers"
        }
      ]
    }

我正在尝试类似的东西:

query {
 posts( where: {tags: {id_contains: [3,1]}}) {
    title
    tags{id},
    created_at
  }
}

标签: graphqlstrapi

解决方案


你可以这样做

query($ids:[ID]) {
 posts( where: {tags: {id:$ids}}) {
    title
    tags{id},
    created_at
  }
}

并将您的 id 作为查询变量传递:

{
  "ids": [1,5] 
}

推荐阅读