我想使用 GraphQL 突变删除一个 github 分支,但我还没有找到关于 deleteRef
的足够信息命令。使用 GraphQL explorer我想出了这个废话:
mutation {
deleteRef(input: {refId: "my-branch"}) {
__typename
}
}
我还不知道如何添加存储库信息以使突变具有任何意义,我包含 __typename
的唯一原因是因为 deleteRef
block 不能'不要留空。我该如何修复这种突变?
最佳答案
您不能在该突变中返回任何内容,因为它不是 Void
类型。您需要像下面这样定义一个突变:
mutation DeleteBranch($branchRef: ID!) {
deleteRef(input: {refId: $branchRef}) {
__typename
clientMutationId
}
}
在explorer您可以定义查询和突变,并选择在您按下按钮时执行哪一个。资源管理器中的完整示例是:
query GetBranchID {
repository(name: "test-repo", owner: "bertrandmartel") {
refs(refPrefix: "refs/heads/", first: 100) {
nodes {
id
name
}
}
}
}
mutation DeleteBranch($branchRef: ID!) {
deleteRef(input: {refId: $branchRef}) {
__typename
clientMutationId
}
}
带有变量:
{
"branchRef": "MDM6UmVmMjQ5MDU0NzQ0Om1hc3Rlcg=="
}
您可以在资源管理器中执行以下操作:
- 执行
GetBranchID
查询 - 将引用复制粘贴到
branchRef
变量中 - 执行
DeleteBranch
如果没有资源管理器,这是使用 python 时的样子:
import requests
repo_name = "YOUR_REPO"
repo_owner = "YOUR_USERNAME"
token = "YOUR_TOKEN"
query = """
query {
repository(name: \"""" + repo_name + """\", owner: \"""" + repo_owner + """\") {
refs(refPrefix: "refs/heads/", first: 100) {
nodes {
id
name
}
}
}
}
"""
resp = requests.post('https://api.github.com/graphql',
json={ "query": query},
headers= {"Authorization": f"Token {token}"}
)
body = resp.json()
for i in body["data"]["repository"]["refs"]["nodes"]:
print(i["name"], i["id"])
chosenBranchId = input("Enter a branch ID: ")
query = """
mutation {
deleteRef(input: {refId: \"""" + chosenBranchId + """\"}) {
__typename
clientMutationId
}
}
"""
resp = requests.post('https://api.github.com/graphql',
json={ "query": query},
headers= {"Authorization": f"Token {token}"}
)
print(resp.json())
关于github - 使用 GraphQL 突变删除 github 中的一个分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60655699/