使用 GraphQL (v4 API) 在 GitHub 中搜索代码 [英] Search for code in GitHub using GraphQL (v4 API)

查看:54
本文介绍了使用 GraphQL (v4 API) 在 GitHub 中搜索代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 GitHub 的 GraphQL API 来搜索包含特定单词的文件/代码.一个简单的(人为的)搜索示例,在这种情况下,是在位于recipes"(repo)的someuser"(repo 的所有者)的文件中查找术语beef",如下所示:

I am using the GitHub's GraphQL API to search for files/code containing a particular word. A simple (contrived) example of a search which in this case is to find the term "beef" in files located in "recipes" (the repo) for "someuser" (the owner for the repo) is shown below:

{
  search(query: "beef repo:someuser/recipes", type: REPOSITORY, first: 10) {
    repositoryCount
    edges {
      node {
        ... on Repository {
          name
        }
      }
    }
  }
}

我在 GitHub 的 GraphQL Explorer (https://developer.github.com/v4/explorer/) 并从搜索中收到零结果,这是不正确的,因为我可以确认单词(上面示例中的beef")在 repo 的文件中:

I tried this in GitHub's GraphQL Explorer (https://developer.github.com/v4/explorer/) and receive zero results from the search which is incorrect as I can confirm that the word ("beef" in the example above) is in the files in the repo:

{
  "data": {
    "search": {
      "repositoryCount": 0,
      "edges": []
    }
  }
}

当我通过 curl 使用 GitHub 的 REST API (v3) 尝试此操作时,我肯定会得到结果:

When I try this using GitHub's REST API (v3) via curl, I definitely get results:

curl --header 'Accept: application/vnd.github.v3.raw' https://api.github.com/search/code?q=beef+repo:someuser/recipes

...所以我知道查询 (REST v3 API) 是有效的,我的理解是 GraphQL (v4) API 中的查询字符串与 REST (v3) API 中的查询字符串相同.

... So I know that the query (REST v3 API) is valid, and my understanding is that the query string in the GraphQL (v4) API is identical to that for the REST (v3) API.

我的问题是:

  1. 我是否错误地使用了 GitHub GraphQL (v4) API,或者我是否错误地指定了查询字符串,或者我是否正在尝试使用尚不支持的功能?
  2. 是否有某人可以提供(或链接到)的示例来说明如何搜索特定词的代码?

推荐答案

Type: CODE 尚不支持.您现在无法使用 graphql 搜索代码.

Type: CODE is not supported yet. There is no way you can search the code using graphql right now.

你的理解是对的.只是你少了一件.您正在执行的搜索是针对 type: REPOSITORY 进行的.如果您将搜索替换为

Your understanding is right. Just that you are missing one piece. The search you are doing is happening against the type: REPOSITORY. if you replace your search with

search(query: "beef", type: REPOSITORY, first: 10) {

你会得到所有以他们的名字命名的 repos.

you will get all the repos having beef in their name.

这篇关于使用 GraphQL (v4 API) 在 GitHub 中搜索代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆