如何使用Python使用Github GraphQL API?

Vai*_*ngh 9 python python-2.7 python-3.x graphql graphene-python

我想使用Github GraphQl v4 API从Github访问详细信息.我找到了Graphene库,但我不确定如何使用Python中的个人访问令牌进行身份验证.
我试图在Google上搜索,但找不到任何示例.它是可以创建图形模式的Python库,而不是用于消费它们,我尝试使用`requests'但是失败了.我如何进行身份验证并找到存储库列表?

我使用Github GraphQl资源管理器通过以下代码查找存储库列表:

viewer {
repositories(first: 30) {
  totalCount
  pageInfo {
    hasNextPage
    endCursor
  }
  edges {
    node {
      name
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

sre*_*vas 14

与rest不同,graphql只有一个终点.您只需POST要将查询作为json对象执行.你应该提供你api_tokengithub获得的标题的一部分.

import requests

url = 'https://api.github.com/graphql'
json = { 'query' : '{ viewer { repositories(first: 30) { totalCount pageInfo { hasNextPage endCursor } edges { node { name } } } } }' }
api_token = "your api token here..."
headers = {'Authorization': 'token %s' % api_token}

r = requests.post(url=url, json=json, headers=headers)
print (r.text)
Run Code Online (Sandbox Code Playgroud)


Yas*_*sss 9

Graphene 用于构建 GraphQL API,而不是用于使用它们。

你看到了吗:https : //github.com/graphql-python/gql

它是 Python 的 GraphQL 客户端。

希望这有帮助。

  • @VaibhavSingh 按照 GitHub 文档 (https://developer.github.com/v4/guides/forming-calls/#authenticating-with-graphql),您需要生成一个像 https://help.github 这样的访问令牌。 com/articles/creating-a-personal-access-token-for-the-command-line/。然后,您必须像 https://developer.github.com/v4/guides/forming-calls/#communicating-with-graphql 一样将您的令牌传递到 `Authorization` 标头中。干杯, (2认同)
  • @VaibhavSingh 您还可以使用图形客户端 GraphiQL https://github.com/graphql/graphiql 来探索 GraphQL API 并在用 python 编写查询之前尝试查询。祝你好运 ! (2认同)