Reputation: 2214
I'd like to delete a github branch using a GraphQL mutation, but I haven't found enough information regarding the deleteRef
command. Using the GraphQL explorer I came up with this nonsense:
mutation {
deleteRef(input: {refId: "my-branch"}) {
__typename
}
}
I don't know yet how to add repository information for the mutation to have any meaning, and the only reason I included __typename
was because the deleteRef
block couldn't be left empty. How do I fix this mutation?
Upvotes: 4
Views: 776
Reputation: 45372
You can't return nothing in that mutation since it's not of type Void
. You need to define a mutation like the following :
mutation DeleteBranch($branchRef: ID!) {
deleteRef(input: {refId: $branchRef}) {
__typename
clientMutationId
}
}
In the explorer you can define queries and mutations and choose which one to execute when you hit the button. A full example in the explorer would be :
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
}
}
with variables:
{
"branchRef": "MDM6UmVmMjQ5MDU0NzQ0Om1hc3Rlcg=="
}
You can do the following in the explorer :
GetBranchID
querybranchRef
variableDeleteBranch
Without the explorer, here's how it would look using 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())
Upvotes: 5