我可以将这个GraphQL API请求转换成Python可用的json格式吗?

2024-04-19 10:17:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我是这方面的无赖,需要帮助。你知道吗

我使用githubv4api执行了GraphQL查询,使用以下代码过滤搜索存储库。但是我对输出格式不满意。你知道吗

query {
  search(
    type:REPOSITORY, 
    query: """
      stars:<1000
      forks:>10
      size:>2000
      pushed:>=2019-08-01
      created:>=2018-08-01

    """,
    last: 100
  ) {
    repos: edges {
      repo: node {
        ... on Repository {
          name
          description
          url
          owner{__typename}
          licenseInfo{name}
          watchers{watchers: totalCount}
          releases {releases: totalCount}
          forks: forkCount
          pullRequests {pullRequests: totalCount}
          stargazers {stars: totalCount}
          openIssues: issues(states:OPEN){totalCount}
          totalIssues: issues {totalIssues: totalCount}
          primaryLanguage {primaryLanguage: name}
          languages(first: 3) { nodes {name} }
          repositoryTopics(first: 3) {nodes {topic {name}}}
          createdAt
          pushedAt
          updatedAt

       }
      }
    }
  }
}

我想做的是将查询作为.json导入,以便在Python中使用它。你知道吗

我在python中尝试了以下方法,但我正在努力使用json格式

import requests

url = 'https://api.github.com/graphql'
json = {'query': '{stars:<1000} {forks:>10} {size:>2000 {pushed:>=2019-08-01} {created:>=2018-08-01}}'}
api_token = "abc.."
headers = {'Authorization': 'token %s' % api_token}

r = requests.post(url=url, json=json, headers=headers)
print (r.text)

Tags: nametokenapijsonurlsize格式query