简体   繁体   English

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

[英]How to use variables in python-gql?

This function will return all users instead of the giving username, how could I make it right?这个函数将返回所有用户而不是给定的用户名,我怎么能做对? And any better GraphQL client for Python?还有更好的 Python GraphQL 客户端吗? gql just so simple that no much documents could check. gql 只是简单到没有多少文件可以检查。

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

You can use variables in python-gql with the help of variable_values parameter.您可以借助 variable_values 参数在 python-gql 中使用variable_values Here is a code snippet in which I have used the variable.这是我在其中使用了变量的代码片段。

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)

I hope this answer helps you.我希望这个答案对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM