首页 > 解决方案 > 使用 GraphQL 和 gql 包 - 更新 GitHub 存储库分支保护规则设置

问题描述

我一直在尝试通过 Python 集成 GitHub 存储库设置,并且使用 GitHub 包一切顺利。不幸的是,该包不包含任何创建新的 repo 分支保护规则的功能,所以我不得不切换到 gql 包。我很难理解这个包裹周围的术语,经过数周的在线搜索,我没有发现任何关于这个主题的内容。

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

方法:


# 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(查询 = 查询,变量 = 变量))

类型错误:execute_async() 缺少 1 个必需的位置参数:'document'

标签: pythongithubgql

解决方案


execute_async需要等待,因为它是一个异步函数。如果您查看python-graphql-client 自述文件,则会有一个示例说明如何执行此操作。

# Asynchronous request
import asyncio

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

推荐阅读