如何用Python的gql库过滤GraphQL中documentLanguage为English的记录
解决GraphQL查询过滤语法错误及数据超时问题
错误原因分析
你写的变量定义完全不符合GraphQL语法规范:
- GraphQL变量声明必须遵循
$变量名: 类型的格式,变量后面只能跟类型名称(比如String、自定义输入类型),不能直接写嵌套对象结构。 - 你错误地把过滤条件直接写在了变量类型位置,导致语法解析失败。
正确的过滤查询写法
首先确认你的GraphQL服务器对technicalDocumentsEntries字段提供的过滤参数(通常是where或filter),以下是基于常见GraphQL API设计规范的正确写法:
query DocumentsAndSoftware($lang: String!) { technicalDocumentsEntries( limit: 3000 where: { technicalDocuments_some: { documentLanguage: { eq: $lang } } } ) { ... on technicalDocuments_document_Entry { id title technicalDocuments { ... on technicalDocuments_document_BlockType { fallbackFileUrl documentLanguage file { id url dateModified } } } } } }
代码说明
- 变量定义:
$lang: String!声明一个必填的字符串变量,用于传递要过滤的语言值 - 过滤参数:
- 在
technicalDocumentsEntries字段中添加where参数,指定过滤条件 technicalDocuments_some表示:只返回那些technicalDocuments数组中至少存在一个条目满足后续条件的记录documentLanguage: { eq: $lang }表示匹配documentLanguage等于变量$lang的值
- 在
- 执行查询时传入变量:
{ "lang": "english" }
关键注意事项
- 不同GraphQL服务器的过滤参数命名可能有差异:比如有些服务器用
filter替代where,或者嵌套条件的命名是technicalDocuments: { some: ... },具体要参考你的API schema文档(可通过GraphiQL/Playground的Docs面板查看) - 如果
limit:3000依然超时,建议即使过滤后也启用分页(比如用skip和limit组合),避免单次查询数据量过大
内容的提问来源于stack exchange,提问作者Brad Davis
相关产品推荐
相关产品推荐

