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

GitHub GraphQL API是否支持按名称等过滤仓库文件/树?求实现语法

问题描述

我正在使用GitHub GraphQL API检索仓库目录与文件,只需要(category)->(item)->(config.ini)这个结构的内容,但当前查询会返回.gitignore、.github目录以及maybe_some_junk这类无关内容。

仓库结构如下:

- / #root
- /.gitignore
- /.github/...
- /categoryOne
- /categoryOne/itemOne
- /categoryOne/itemOne/config.ini
- /categoryTwo
- /categoryTwo/itemOne
- /categoryTwo/itemOne/config.ini
- /categoryTwo/itemOne/maybe_some_junk/...
...

当前使用的查询代码:

query Repository {
    repository(name: "my-repository", owner: "example") {
        object(expression: "HEAD:") {
            ... on Tree {
                categories: entries {
                    mode
                    name
                    type
                    object {
                        ... on Tree {
                            items: entries {
                                name
                                type
                                configFile: object {
                                    ... on Blob {
                                        text
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }   
    }
}

尝试添加(where: {...})子句时提示语法错误,想知道GitHub GraphQL API是否支持按名称过滤文件/树,以及如何实现。

解决方案

1. GitHub GraphQL API不支持查询阶段直接过滤Tree条目

GitHub的GraphQL API中,Tree类型的entries字段没有提供过滤参数(比如where子句),所以无法在查询时直接排除无关的文件或目录,这也是你添加where子句报语法错误的原因。

2. 可行的两种处理方式

方式一:客户端本地过滤

查询获取全量数据后,在客户端(或服务端)手动过滤掉不需要的内容:

  • 排除根目录下以.开头的文件/目录(比如.gitignore、.github)
  • 排除item目录下的无关子目录(比如maybe_some_junk)
  • 只保留item目录下名为config.ini的Blob对象

举个简单的JavaScript过滤示例(伪代码):

// 假设data是API返回的结果
const validCategories = data.repository.object.categories.filter(category => {
    // 过滤根目录下的隐藏目录/文件
    return !category.name.startsWith('.') && category.type === 'tree';
}).map(category => {
    // 过滤item目录下的无关内容,只保留config.ini
    const validItems = category.object.items.filter(item => {
        return item.type === 'tree' && item.configFile;
    });
    return { ...category, object: { ...category.object, items: validItems } };
});

方式二:精准指定路径查询

如果提前知道需要查询的category和item名称,可以直接通过指定路径的方式查询,避免返回无关数据。比如同时查询categoryOne/itemOne和categoryTwo/itemOne的config.ini:

query Repository {
    repo: repository(name: "my-repository", owner: "example") {
        categoryOneItemOne: object(expression: "HEAD:categoryOne/itemOne") {
            ... on Tree {
                configFile: entries(name: "config.ini") {
                    ... on Blob {
                        text
                    }
                }
            }
        }
        categoryTwoItemOne: object(expression: "HEAD:categoryTwo/itemOne") {
            ... on Tree {
                configFile: entries(name: "config.ini") {
                    ... on Blob {
                        text
                    }
                }
            }
        }
    }
}

这种方式直接定位到目标路径,查询效率更高,也不需要后续过滤。

如果category和item的数量较多且不确定,可以先查询根目录下的合法category列表(过滤隐藏目录),再逐个查询每个category下的item目录,最后获取对应的config.ini。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 19:52:42