如何在python-gql中使用变量?

Lea*_*lzi 0 graphql graphql-python

这个函数将返回所有用户而不是给定的用户名,我怎么能做对?还有更好的 Python GraphQL 客户端吗?gql 就这么简单,没有多少文件可以检查。

    def fetch_user(username):
        query = gql("""
                    query getUser($username: String) {
                    user(
                        where: { name: { _eq: $username } }
                    ) {
                        id
                        name
                    }
                    }
                """)
        result = client.execute(query)
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以在variable_values参数的帮助下在 python-gql 中使用变量。这是我在其中使用了变量的代码片段。

query = gql("""
mutation ($paramId: ID!, $paramTitle: String!){
    XputPost(id: $paramId, title: $paramTitle){
        id
        title
    }
}
""")

params = {
    "paramId": "1",
    "paramTitle": "New Post"
}
result = client.execute(query, variable_values=params)
Run Code Online (Sandbox Code Playgroud)

我希望这个答案对你有帮助。