【GitHub】使用GraphQL注册问题
为了试验 GraphQL,我想使用 GitHub 提供的 GraphQL 来尝试一下。实际上我会使用 GraphQL 来进行问题的注册和在项目中移动问题。我会使用 playground 作为 GraphQL 的执行环境。
准备

创建项目后,只需创建任意数量的列(在这里创建“待办事项”、“进行中”和“已完成”列)。
在添加问题时,还需要设置自动化以将问题添加到“待办事项”列中。


如果将新注册的问题与此项目相关联,那么它将自动显示在“待办事项”中。
问题 注册
我将尝试在GraphQL中注册问题。要注册问题,首先需要获取存储库ID。另外,为了将问题与项目相关联,我还需要获取项目ID,并且稍后还会添加标签给问题,所以我也会一并获取标签ID。
使用GitHub的GraphQL需要在头部设置访问令牌。请在授予访问令牌后执行每个查询。
{
"Authorization": "Bearer access-token"
}
请设置标题后执行下一个查询,以确认是否能够获取结果。(在name字段中指定存储库名称,在owner字段中指定GitHub用户名。)
query getProjectLabels {
repository(name:"PersonalTaskManagement", owner:"kiyo27") {
id
name
projects(search:"todo", first:1) {
nodes {
id
name
columns(first:3) {
nodes {
id
name
cards {
nodes {
id
content {
__typename
... on Issue {
id
title
}
}
}
}
}
}
}
}
labels(first: 10) {
nodes {
id
name
}
}
}
}
达到的成果:
{
"data": {
"repository": {
"id": "repository-id-xxxxxxxxxxxxxxxxxxx",
"name": "PersonalTaskManagement",
"projects": {
"nodes": [
{
"id": "project-id-xxxxxxxxxxxx",
"name": "todo",
"columns": {
"nodes": [
{
"id": "column-id-xxxxxxxxxxxxxxxxxxx",
"name": "Backlog",
"cards": {
"nodes": []
}
},
{
"id": "column-id-xxxxxxxxxxxxxxxxxxx",
"name": "In Progress",
"cards": {
"nodes": []
}
},
{
"id": "column-id-xxxxxxxxxxxxxxxxxxx",
"name": "Done",
"cards": {
"nodes": []
}
}
]
}
}
]
},
"labels": {
"nodes": [
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "bug"
},
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "documentation"
},
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "duplicate"
},
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "enhancement"
},
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "help wanted"
},
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "good first issue"
},
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "invalid"
},
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "question"
},
{
"id": "label-id-xxxxxxxxxxxxxxxxxxxxx",
"name": "wontfix"
}
]
}
}
}
}
注册
使用获取到的仓库ID、项目ID和标签ID来注册问题。
mutation createIssue {
createIssue(input:{title: "Create issue from GraphQL", repositoryId :"repository-id-xxxxxxxxxxxxxxxxxxx", projectIds:"project-id-xxxx", labelIds: ["label-id-xxxxxxx"]}) {
issue {
id
}
}
}
执行结果:
{
"data": {
"createIssue": {
"issue": {
"id": "issue-id-xxxxxxxxxxxx"
}
}
}
}

问题变动
我打算使用GraphQL来更新已注册的问题。要更新问题,需要使用问题ID,因此需要指定先前执行查询返回的ID值。
mutation updateIssue{
updateIssue(input:{id: "issue-id-xxxxxxxxx", title:"Hello world, GraphQL"}) {
issue {
id
}
}
}
运行结果:
{
"data": {
"updateIssue": {
"issue": {
"id": "iisue-id-xxxxxx"
}
}
}
}

項目卡片移動
由于刚刚注册的问题位于“Backlog”栏中,现在我将尝试将其移动到“In Progress”栏中。为了移动问题,我们需要卡片ID和栏ID,可以通过执行以下查询来获取。
query getIssue {
search(query:"Hello world, GraphQL", type:ISSUE, first:1) {
nodes {
__typename
... on Issue {
title
projectCards {
nodes {
id
project {
name
columns(first:3) {
nodes {
id
name
}
}
}
}
}
}
}
}
}
执行结果:
{
"data": {
"search": {
"nodes": [
{
"__typename": "Issue",
"title": "Hello world, GraphQL",
"projectCards": {
"nodes": [
{
"id": "project-card-id-xxxxxxxxx",
"project": {
"name": "todo",
"columns": {
"nodes": [
{
"id": "column-backlog-id-xxxxxxxx",
"name": "Backlog"
},
{
"id": "column-in-progress-id-xxxxxxxxx",
"name": "In Progress"
},
{
"id": "column-done-id-xxxxxxxxxxxx",
"name": "Done"
}
]
}
}
}
]
}
}
]
}
}
}
使用project-card-id-xxxxxxxxx这个变量取得内容,并且使用column-in-progress-id-xxxxxxxxx这个变量确定issue的移动目标。让我们尝试移动issue。
mutation moveProjectCard {
moveProjectCard(input:{cardId:"PRC_lALOF65GMM4Ax90azgQCJLY", columnId:"project-card-id-xxxxx"}) {
cardEdge {
node {
url
}
}
}
}
运行结果:
{
"data": {
"moveProjectCard": {
"cardEdge": {
"node": {
"url": "https://github.com/kiyo27/PersonalTaskManagement/projects/1#card-xxxxx"
}
}
}
}
}

以上、我們介紹了如何使用GitHub的GraphQL進行簡單的問題和項目操作。
请在本地使用中国话将以下内容改写:只需要一个选项:
参考
请提供以下文档的中文原生释义:
文档
https://docs.github.com/ja/graphql