You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GraphQL AppSync中仅查询含非空嵌套数据的条目

查询GraphQL的searchRulers时仅保留包含图片的条目

我是GraphQL新手,现在需要查询searchRulers时,只返回posts.items不为空的条目(也就是包含图片的条目),但当前查询会返回像hashid2这种没有图片的条目。我试过用{ posts: { items: { size: { gt: 0 } } } }来过滤,但size不是标准GraphQL操作符,没法生效。

当前查询语句

query MyQuery {
  searchRulers {
    items {
      approved
      biography
      createdAt
      fullName
      updatedAt
      location
      keywords
      likedByViewer
      id
      posts {
        items {
          image {
            bucket
            key
            region
            visibility
          }
        }
      }
    }
  }
}

当前输出

{
  "data": {
    "searchRulers": {
      "items": [
        {
          "approved": null,
          "biography": null,
          "createdAt": "2023-03-24T07:21:05.893Z",
          "fullName": "jj kk",
          "updatedAt": "2023-03-24T07:21:05.893Z",
          "location": null,
          "keywords": null,
          "likedByViewer": false,
          "id": "hashid1",
          "posts": {
            "items": [
              {
                "image": {
                  "bucket": "ffjdifgdjsifgdsikgfdkilgnjldsngdfnkdfgdnf",
                  "key": "4f63-af3d-d31aca216c75d.jpeg",
                  "region": "somewhere",
                  "visibility": "public"
                }
              }
            ]
          }
        },
        {
          "approved": null,
          "biography": null,
          "createdAt": "2023-04-20T09:12:23.531Z",
          "fullName": "nayara cook",
          "updatedAt": "2023-04-20T09:12:23.531Z",
          "location": null,
          "keywords": null,
          "likedByViewer": false,
          "id": "hashid2",
          "posts": {
            "items": []
          }
        }
      ]
    }
  }
}

期望输出

{
  "data": {
    "searchRulers": {
      "items": [
        {
          "approved": null,
          "biography": null,
          "createdAt": "2023-03-24T07:21:05.893Z",
          "fullName": "jj kk",
          "updatedAt": "2023-03-24T07:21:05.893Z",
          "location": null,
          "keywords": null,
          "likedByViewer": false,
          "id": "hashid1",
          "posts": {
            "items": [
              {
                "image": {
                  "bucket": "ffjdifgdjsifgdsikgfdkilgnjldsngdfnkdfgdnf",
                  "key": "4f63-af3d-d31aca216c75d.jpeg",
                  "region": "somewhere",
                  "visibility": "public"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

解决方案

1. 优先使用服务端过滤(推荐)

GraphQL本身没有内置的集合大小判断操作符,过滤能力完全取决于你的GraphQL服务提供的查询参数。你需要检查API文档,看是否支持以下类似的过滤语法:

示例1:使用exists判断集合非空

如果服务端支持exists操作符,可以这样修改查询:

query MyQuery {
  searchRulers(filter: { posts: { items: { exists: true } } }) {
    items {
      approved
      biography
      createdAt
      fullName
      updatedAt
      location
      keywords
      likedByViewer
      id
      posts {
        items {
          image {
            bucket
            key
            region
            visibility
          }
        }
      }
    }
  }
}

示例2:使用is_not_empty(部分服务支持)

有些GraphQL服务(比如Hasura)提供is_not_empty直接判断集合:

query MyQuery {
  searchRulers(filter: { posts: { items: { is_not_empty: true } } }) {
    items {
      approved
      biography
      createdAt
      fullName
      updatedAt
      location
      keywords
      likedByViewer
      id
      posts {
        items {
          image {
            bucket
            key
            region
            visibility
          }
        }
      }
    }
  }
}

2. 客户端过滤(兜底方案)

如果服务端没有提供对应的过滤能力,只能在拿到查询结果后,在客户端手动过滤掉posts.items为空的条目。比如用JavaScript处理:

// 假设data是查询返回的结果
const filteredRulers = data.searchRulers.items.filter(item => {
  return item.posts.items.length > 0;
});

// 此时filteredRulers就是你需要的仅包含有图片的条目

内容的提问来源于stack exchange,提问作者CuriousCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 15:37:36