【问题标题】:How to use variables in python-gql?如何在 python-gql 中使用变量?
【发布时间】:2020-04-27 05:40:16
【问题描述】:

这个函数将返回所有用户而不是给出用户名,我怎样才能使它正确?还有更好的 Python GraphQL 客户端吗? gql 太简单了,没有多少文件可以检查。

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

【问题讨论】:

    标签: graphql graphql-python


    【解决方案1】:

    您可以借助variable_values 参数在python-gql 中使用变量。 这是我在其中使用了变量的代码 sn-p。

    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)
    

    希望这个回答对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 2015-04-19
      • 2023-03-27
      • 2021-05-23
      • 2013-02-26
      • 1970-01-01
      相关资源
      最近更新 更多