graphql - 如何在 python-gql 中使用变量?
问题描述
此函数将返回所有用户而不是提供用户名,我怎样才能使它正确?还有更好的 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-python/gqlvariable_values
中的变量。这是我使用该变量的代码片段。
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)
我希望这个答案对你有所帮助。
推荐阅读
- python - 如何在python中将矩阵的直径转换为零?
- python - 'list' 对象不能解释为整数 - 有没有办法将列表转换为整数?
- perl - Perl 哈希数组正在添加来自其他数组的元素
- javascript - 在页面上显示 api 数据时遇到问题?
- scheme - 可以在 Scheme Dr Racket 中返回 let* 函数吗?
- ansible - 如何混合标签和布尔变量来执行/不执行任务?
- git - Git从存储库中删除文件?
- gfortran - 如果变量不等于某些值,则将填充值分配给变量
- mobile - 移动设备上的文本超链接太大
- javascript - Zoom out camera while keeping text locked - Phaser 3