Using GraphQL and the gql package - updating GitHub Repository Branch Protection Rule Setting

598 Views Asked by At

I have been trying to integrate GitHub repo settings via Python and everything was going smoothly using the GitHub package. Unfortunately, the package does not contain any functions on creating new repo branch protection rules, so I had to switch to the gql package. I'm having a difficult time understanding the lingo surrounding this package and after weeks of online searches I found nothing on the subject.

The goal is to select a transport to my repo, create a client, set up a query, and run the client. I believe the first two are setup appropriately, it's the query I think I may have messed up. I have been using these two links to guide me: https://github.community/t/rest-api-v3-wildcard-branch-protection/13593/8 and https://docs.github.com/en/free-pro-team@latest/graphql/reference/input-objects#createbranchprotectionruleinput

# Code:

# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url=myurl,
                              headers={'Authorization': 'mytoken'}) # I use my url and token

# Create a GraphQL client using the defined transport
client = Client(transport = transport, fetch_schema_from_transport = True)

Method:


# Provide a GraphQL query
def test(client):
    query = gql("""
            mutation($input: CreateBranchProtectionRuleInput!) {
                createBranchProtectionRule(input: $variables) {
                    branchProtectionRule {
                      id
                    } 
                }
            }
        """
        )
 
    variables = {"repositoryId": "123456", "pattern": "release/*"}
    
    # Execute the query on the transport
    data = asyncio.run(client.execute_async(query,variables))
    print(data)

test(client)

This results in the following error:

data = asyncio.run(client.execute_async(query = query,variables = variables))

TypeError: execute_async() missing 1 required positional argument: 'document'

1

There are 1 best solutions below

1
On

execute_async needs to be awaited because it's an asynchronous function. If you check the python-graphql-client readme there's an example of how to do it.

# Asynchronous request
import asyncio

data = asyncio.run(client.execute_async(query=query, variables=variables))
print(data)  # => {'data': {'country': {'code': 'CA', 'name': 'Canada'}}}