NiceLeeのBlog 用爱发电 bilibili~

关于 Github graphQL

2021-02-06
nIceLee

阅读:


笔记向。

前言

一般情况下Github REST API足够用了。相关doc链接
但它有时候返回的信息不全,或者过多,会没法较好地实现目的,或者造成不必要的开销。
这时候使用graphQL是一种较方便的替代方法。

内容

相关链接

实例

我们常用的有RepositoryUserOrganization等类型。
举例来说,可以进行User, Repository查询:

query {
  __type(name: "User") {
    name
    kind
    description
    fields {
      name
    }
  }
  __type(name: "Repository") {
    name
    kind
    description
    fields {
      name
    }
  }
}

得到结果进行参照,再配合Query 语法,(或者直接参考此处
我们可以轻松的写出查询某用户所有项目的名称、fork数目、star数目的query

query {
  user(login: "此处填用户名") {
    repositories(first: 100, after: endCursor) {//初始endCursor = "",无需after,后续分页查询加上即可
      pageInfo {
        endCursor
        hasNextPage
      }
      nodes {
        name
        stargazerCount
        forkCount
      }
    }
  }
}

或者需要更复杂的操作时,可以通过edge获取具体节点的cursor信息。参考分页

query {
  user(login: "nICEnnnnnnnLee") {
    repositories(first: 100) { // 同上
      pageInfo {
        endCursor
        hasNextPage
      }
      edges {
        node {
          name
          stargazerCount
          forkCount
          name
        }
        cursor
      }
    }
  }
}

比如,再进行更高级一点的操作,查询某人star数目TOP 3的项目。

query {
  user(login: "nICEnnnnnnnLee") {
    repositories(first: 3, orderBy: {field: STARGAZERS, direction: DESC}) {
      pageInfo {
        endCursor
        hasNextPage
      }
      nodes {
        name
        stargazerCount
        forkCount
      }
    }
  }
}

参考type: User

{
	"data": {
		"__type": {
			"name": "User",
			"kind": "OBJECT",
			"description": "A user is an individual's account on GitHub that owns repositories and can make new content.",
			"fields": [{
				"name": "anyPinnableItems"
			}, {
				"name": "avatarUrl"
			}, {
				"name": "bio"
			}, {
				"name": "bioHTML"
			}, {
				"name": "commitComments"
			}, {
				"name": "company"
			}, {
				"name": "companyHTML"
			}, {
				"name": "contributionsCollection"
			}, {
				"name": "createdAt"
			}, {
				"name": "databaseId"
			}, {
				"name": "email"
			}, {
				"name": "followers"
			}, {
				"name": "following"
			}, {
				"name": "gist"
			}, {
				"name": "gistComments"
			}, {
				"name": "gists"
			}, {
				"name": "hasSponsorsListing"
			}, {
				"name": "hovercard"
			}, {
				"name": "id"
			}, {
				"name": "interactionAbility"
			}, {
				"name": "isBountyHunter"
			}, {
				"name": "isCampusExpert"
			}, {
				"name": "isDeveloperProgramMember"
			}, {
				"name": "isEmployee"
			}, {
				"name": "isHireable"
			}, {
				"name": "isSiteAdmin"
			}, {
				"name": "isSponsoredBy"
			}, {
				"name": "isSponsoringViewer"
			}, {
				"name": "isViewer"
			}, {
				"name": "issueComments"
			}, {
				"name": "issues"
			}, {
				"name": "itemShowcase"
			}, {
				"name": "location"
			}, {
				"name": "login"
			}, {
				"name": "name"
			}, {
				"name": "organization"
			}, {
				"name": "organizationVerifiedDomainEmails"
			}, {
				"name": "organizations"
			}, {
				"name": "packages"
			}, {
				"name": "pinnableItems"
			}, {
				"name": "pinnedItems"
			}, {
				"name": "pinnedItemsRemaining"
			}, {
				"name": "project"
			}, {
				"name": "projects"
			}, {
				"name": "projectsResourcePath"
			}, {
				"name": "projectsUrl"
			}, {
				"name": "publicKeys"
			}, {
				"name": "pullRequests"
			}, {
				"name": "repositories"
			}, {
				"name": "repositoriesContributedTo"
			}, {
				"name": "repository"
			}, {
				"name": "resourcePath"
			}, {
				"name": "savedReplies"
			}, {
				"name": "sponsorsListing"
			}, {
				"name": "sponsorshipForViewerAsSponsor"
			}, {
				"name": "sponsorshipsAsMaintainer"
			}, {
				"name": "sponsorshipsAsSponsor"
			}, {
				"name": "starredRepositories"
			}, {
				"name": "status"
			}, {
				"name": "topRepositories"
			}, {
				"name": "twitterUsername"
			}, {
				"name": "updatedAt"
			}, {
				"name": "url"
			}, {
				"name": "viewerCanChangePinnedItems"
			}, {
				"name": "viewerCanCreateProjects"
			}, {
				"name": "viewerCanFollow"
			}, {
				"name": "viewerCanSponsor"
			}, {
				"name": "viewerIsFollowing"
			}, {
				"name": "viewerIsSponsoring"
			}, {
				"name": "watching"
			}, {
				"name": "websiteUrl"
			}]
		}
	}
}

参考type: repository

{
	"data": {
		"__type": {
			"name": "Repository",
			"kind": "OBJECT",
			"description": "A repository contains the content for a project.",
			"fields": [{
				"name": "assignableUsers"
			}, {
				"name": "branchProtectionRules"
			}, {
				"name": "codeOfConduct"
			}, {
				"name": "collaborators"
			}, {
				"name": "commitComments"
			}, {
				"name": "contactLinks"
			}, {
				"name": "createdAt"
			}, {
				"name": "databaseId"
			}, {
				"name": "defaultBranchRef"
			}, {
				"name": "deleteBranchOnMerge"
			}, {
				"name": "deployKeys"
			}, {
				"name": "deployments"
			}, {
				"name": "description"
			}, {
				"name": "descriptionHTML"
			}, {
				"name": "diskUsage"
			}, {
				"name": "forkCount"
			}, {
				"name": "forks"
			}, {
				"name": "fundingLinks"
			}, {
				"name": "hasIssuesEnabled"
			}, {
				"name": "hasProjectsEnabled"
			}, {
				"name": "hasWikiEnabled"
			}, {
				"name": "homepageUrl"
			}, {
				"name": "id"
			}, {
				"name": "interactionAbility"
			}, {
				"name": "isArchived"
			}, {
				"name": "isBlankIssuesEnabled"
			}, {
				"name": "isDisabled"
			}, {
				"name": "isEmpty"
			}, {
				"name": "isFork"
			}, {
				"name": "isInOrganization"
			}, {
				"name": "isLocked"
			}, {
				"name": "isMirror"
			}, {
				"name": "isPrivate"
			}, {
				"name": "isSecurityPolicyEnabled"
			}, {
				"name": "isTemplate"
			}, {
				"name": "isUserConfigurationRepository"
			}, {
				"name": "issue"
			}, {
				"name": "issueOrPullRequest"
			}, {
				"name": "issueTemplates"
			}, {
				"name": "issues"
			}, {
				"name": "label"
			}, {
				"name": "labels"
			}, {
				"name": "languages"
			}, {
				"name": "latestRelease"
			}, {
				"name": "licenseInfo"
			}, {
				"name": "lockReason"
			}, {
				"name": "mentionableUsers"
			}, {
				"name": "mergeCommitAllowed"
			}, {
				"name": "milestone"
			}, {
				"name": "milestones"
			}, {
				"name": "mirrorUrl"
			}, {
				"name": "name"
			}, {
				"name": "nameWithOwner"
			}, {
				"name": "object"
			}, {
				"name": "openGraphImageUrl"
			}, {
				"name": "owner"
			}, {
				"name": "packages"
			}, {
				"name": "parent"
			}, {
				"name": "primaryLanguage"
			}, {
				"name": "project"
			}, {
				"name": "projects"
			}, {
				"name": "projectsResourcePath"
			}, {
				"name": "projectsUrl"
			}, {
				"name": "pullRequest"
			}, {
				"name": "pullRequests"
			}, {
				"name": "pushedAt"
			}, {
				"name": "rebaseMergeAllowed"
			}, {
				"name": "ref"
			}, {
				"name": "refs"
			}, {
				"name": "release"
			}, {
				"name": "releases"
			}, {
				"name": "repositoryTopics"
			}, {
				"name": "resourcePath"
			}, {
				"name": "securityPolicyUrl"
			}, {
				"name": "shortDescriptionHTML"
			}, {
				"name": "squashMergeAllowed"
			}, {
				"name": "sshUrl"
			}, {
				"name": "stargazerCount"
			}, {
				"name": "stargazers"
			}, {
				"name": "submodules"
			}, {
				"name": "tempCloneToken"
			}, {
				"name": "templateRepository"
			}, {
				"name": "updatedAt"
			}, {
				"name": "url"
			}, {
				"name": "usesCustomOpenGraphImage"
			}, {
				"name": "viewerCanAdminister"
			}, {
				"name": "viewerCanCreateProjects"
			}, {
				"name": "viewerCanSubscribe"
			}, {
				"name": "viewerCanUpdateTopics"
			}, {
				"name": "viewerDefaultCommitEmail"
			}, {
				"name": "viewerDefaultMergeMethod"
			}, {
				"name": "viewerHasStarred"
			}, {
				"name": "viewerPermission"
			}, {
				"name": "viewerPossibleCommitEmails"
			}, {
				"name": "viewerSubscription"
			}, {
				"name": "vulnerabilityAlerts"
			}, {
				"name": "watchers"
			}]
		}
	}
}

内容
隐藏