GraphQL现有Schema添加多条件过滤及Postman查询报错排查
问题分析与修正方案
核心问题
AudienceFilterInput类型定义错误:其and字段当前被限定为[Population],仅支持人口过滤条件,无法容纳国家过滤条件,与你需要同时组合两种过滤的需求不符。- 字段不匹配:你的查询中使用了
male_percentage和us_percentage,但当前Schema的Population和Country类型中并未定义这些字段,导致报错。
修正方案分两种场景
场景1:使用通用percentage字段(无需新增特定字段)
适用于仅需对人口/国家的整体百分比进行过滤的情况。
修正后的Schema
scalar JSON schema { query: Query } input PercentageInput { gte: Int } input Population { percentage: PercentageInput } input Country { percentage: PercentageInput } input And { population: [Population] country: [Country] } # 修复and字段类型为[And],支持同时组合人口和国家过滤 input AudienceFilterInput { market: String and: [And] } input TalentMetadataFilterInput { citizen_of: ListFilterInput gender: [String] original_country: [String] audience: AudienceFilterInput }
修正后的查询语句
query MyQuery { talentMetadata( filter: { audience: { and: [ { population: [{ percentage: { gte: 80 } }], country: [{ percentage: { gte: 30 } }] } ] } }, size: 100 ) { # 在此添加你需要返回的字段 id gender original_country } }
场景2:新增特定人口分组与灵活国家过滤
适用于需要按特定人口分组(如男性)和指定国家(如美国)进行精准过滤的情况。
修正后的Schema
scalar JSON schema { query: Query } input PercentageInput { gte: Int } # 新增特定人口分组的百分比字段 input Population { male_percentage: PercentageInput female_percentage: PercentageInput # 可根据需求添加其他分组字段,如non_binary_percentage } # 改为支持按国家代码+百分比的灵活过滤 input Country { code: String # ISO国家代码,如"US"、"CA"、"GB" percentage: PercentageInput } input And { population: [Population] country: [Country] } # 修复and字段类型为[And] input AudienceFilterInput { market: String and: [And] } input TalentMetadataFilterInput { citizen_of: ListFilterInput gender: [String] original_country: [String] audience: AudienceFilterInput }
修正后的查询语句
query MyQuery { talentMetadata( filter: { audience: { and: [ { population: [{ male_percentage: { gte: 80 } }], country: [{ code: "US", percentage: { gte: 30 } }] } ] } }, size: 100 ) { # 在此添加你需要返回的字段 id gender original_country } }
内容的提问来源于stack exchange,提问作者Pavanraotk
相关产品推荐
相关产品推荐

