【问题标题】:How to consume the Github GraphQL API using Python?如何使用 Python 使用 Github GraphQL API?
【发布时间】:2018-02-08 00:18:26
【问题描述】:

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

我使用 Github GraphQl explorer 通过以下代码查找存储库列表:

viewer {
repositories(first: 30) {
  totalCount
  pageInfo {
    hasNextPage
    endCursor
  }
  edges {
    node {
      name
    }
  }
}

【问题讨论】:

    标签: python python-2.7 python-3.x graphql graphene-python


    【解决方案1】:

    对于 GitHub,有一个关于在 Python 3 中使用 Github GraphQL API 的示例

    https://gist.github.com/gbaman/b3137e18c739e0cf98539bf4ec4366ad

    (检查链接,因为它有很多 cmets,包括更好的身份验证代码)

    # An example to get the remaining rate limit using the Github GraphQL API.
    
    import requests
    
    headers = {"Authorization": "Bearer YOUR API KEY"}
    
    
    def run_query(query): # A simple function to use requests.post to make the API call. Note the json= section.
        request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers)
        if request.status_code == 200:
            return request.json()
        else:
            raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
    
            
    # The GraphQL query (with a few aditional bits included) itself defined as a multi-line string.       
    query = """
    {
      viewer {
        login
      }
      rateLimit {
        limit
        cost
        remaining
        resetAt
      }
    }
    """
    
    result = run_query(query) # Execute the query
    remaining_rate_limit = result["data"]["rateLimit"]["remaining"] # Drill down the dictionary
    print("Remaining rate limit - {}".format(remaining_rate_limit))
    

    还有很多 Python GraphQL 客户端库:

    官方名单在https://graphql.org/code/#python
    (只需向下滚动,客户端库在服务器库之后)

    【讨论】:

      【解决方案2】:

      正如前面的答案所提到的,调用 GraphQL 就像使用查询字符串发出 POST 请求一样简单。 但是,如果您在 Python3 上想要更高级的东西,它还将在构建期间验证您的查询并为您生成类型化的数据类响应类,请查看新的 GQL 库: https://github.com/ekampf/gql

      【讨论】:

        【解决方案3】:

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

        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)
        

        【讨论】:

        • 感谢您的回答!!我很怀疑我是否想通过添加一些带有默认参数的函数来自定义我的 json。所以我将在默认参数中添加first:10,如果我想编辑,我可以通过传递first:30 来覆盖。我知道这是一个愚蠢的问题,但你能帮我吗
        • @VaibhavSingh 我不确定我的措辞是否正确。介意再解释一遍。
        • 现在 json 由我们修改,我们将从 GraphQL 获得预期的结果,但如果我愿意,我可以自定义函数中的 json 购买传递参数,并可以根据用户更改查询。现在它被硬编码在程序中
        【解决方案4】:

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

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

        它是 Python 的 GraphQL 客户端。

        希望对您有所帮助。

        【讨论】:

        猜你喜欢
        • 2017-06-20
        • 2017-06-20
        • 2018-04-22
        • 2018-08-25
        • 2021-12-30
        • 1970-01-01
        • 2022-10-14
        • 2019-12-19
        • 2018-08-25
        相关资源
        最近更新 更多