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

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

问题描述

我正在使用GitHub的GraphQL API搜索包含特定单词的文件/代码.一个简单的(人为)搜索示例,在这种情况下,是在"someuser"(存储库的所有者)的配方"(存储库)中的文件中找到牛肉"一词:

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 /),并从搜索中收到零个结果,这是不正确的,因为我可以确认单词(在上面的示例中为牛肉")在存储库的文件中:

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) {

您将获得所有以牛肉为名的仓库.

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

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

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