如何使用 GitHub GraphQL API

如何使用 GitHub GraphQL API

如何调用

1. 到 Settings / Developer Settings / Personal Access Toknes 页面申请 token

2. 调用:

Post https://api.github.com/graphql
Headers:
Authorization: "token ${github_token}"
Body:
{query: "${query}"}

GraphQL Query

1.获取基本信息

query {
  viewer {
    id
    name
    bio
    login
    email
    avatarUrl
    location
    followers {
      totalCount
    }
    following {
      totalCount
    }
  }
}

2.获取组织列表

query {
  viewer {
    organizations(first: 100) {
      nodes {
        id,
        name,
        avatarUrl,
        url
      }
      pageInfo {
        hasNextPage
        hasPreviousPage
        startCursor
        endCursor
      }
    }
  }
}

3.获取项目列表

query {
  viewer {
    repositories(first: ${ totalCount }, orderBy: {field: ${ sortBy }, direction: DESC}, affiliations: [${ affiliations }]) {
      totalCount
      nodes {
        id
        name
        nameWithOwner
        description
        url
        stargazers {
          totalCount
        }
        forkCount
        updatedAt
        languages(first:1, orderBy: {field: SIZE, direction: DESC}) {
          nodes {
            name
            color
          }
        }
      }
    }
  }
}

参考

文档:https://developer.github.com/v4/

在线调试:https://developer.github.com/v4/explorer/

miaowing
A Javascript & Typescript developer
Posted on 2019-11-17

Share this post

Scan to open in WeChat

Stay tuned

  • Add me on WeChat

  • Subscribe to my newsletter