使用GraphQL和gql软件包-更新GitHub Repository分支保护规则设置

问题描述

我一直在尝试通过Python集成GitHub存储库设置,并且使用GitHub软件包可以使一切顺利进行。不幸的是,该软件包不包含任何创建新的回购分支保护规则的功能,因此我不得不切换到gql软件包。我很难理解有关该软件包的术语,经过数周的在线搜索后,我对该主题一无所获。

目标是选择到我的仓库的运输工具,创建客户端,设置查询并运行客户端。我相信前两个设置正确,这是我认为我可能搞砸了的查询。我一直在使用这两个链接来指导我:https://github.community/t/rest-api-v3-wildcard-branch-protection/13593/8https://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)

方法


# 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)

这将导致以下错误

数据= asyncio.run(client.execute_async(查询=查询,变量=变量))

TypeError:execute_async()缺少1个必需的位置参数:“ document”

解决方法

execute_async由于它是一个异步函数,因此需要等待。如果您选中python-graphql-client readme,则有一个示例。

# Asynchronous request
import asyncio

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