'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? gql just so simple that no much documents could check.

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


Solution 1:[1]

You can use variables in graphql-python/gql with the help of variable_values parameter. 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 leszek.hanusz