Python GraphQL API 调用组合 [英] Python GraphQL API call composition

查看:26
本文介绍了Python GraphQL API 调用组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习如何使用 python,但在调用 graphQL api 时遇到了一些问题.

I've recently started learning how to use python and i'm having some trouble with a graphQL api call.

我正在尝试建立一个循环以使用分页获取所有信息,并且我的第一个请求工作正常.

I'm trying to set up a loop to grab all the information using pagination, and my first request is working just fine.

values = """
      {"query" : "{organizations(ids:) {pipes {id name phases {id name cards_count cards(first:30){pageInfo{endCursor hasNextPage} edges {node {id title current_phase{name} assignees {name} due_date createdAt finished_at fields{name value filled_at updated_at} } } } } }}}"}
    """

但是使用结束光标作为变量的第二次调用对我不起作用.我认为这是因为我不明白如何正确转义变量的字符串.但对于我的生活,我无法理解应该如何做.

but the second call using the end cursor as a variable isn't working for me. I assume that it's because i'm not understanding how to properly escape the string of the variable. But for the life of me I'm unable to understand how it should be done.

这是我到目前为止所得到的......

Here's what I've got for it so far...

values = """
      {"query" : "{phase(id: """ + phaseID+ """ ){id name cards_count cards(first:30, after:"""" + pointer + """"){pageInfo{endCursor hasNextPage} edges {node {id title assignees {name} due_date createdAt finished_at fields{name value datetime_value updated_at phase_field { id label }  } } } } } }"}
        """ 

第二个在循环时只返回一个 400 错误的请求.

the second one as it loops just returns a 400 bad request.

任何帮助将不胜感激.

推荐答案

作为一般规则,您应该避免使用像这样的字符串操作来构建查询.

As a general rule you should avoid building up queries using string manipulation like this.

在 GraphQL 查询本身中,GraphQL 允许 变量 可以作为查询中的占位符您稍后将插入的值.您需要在查询的顶部声明变量,然后可以在查询内的任何位置引用它们.没有 JSON 包装器的查询本身看起来像

In the GraphQL query itself, GraphQL allows variables that can be placeholders in the query for values you will plug in later. You need to declare the variables at the top of the query, and then can reference them anywhere inside the query. The query itself, without the JSON wrapper, would look something like

query = """
  query MoreCards($phase: ID!, $cursor: String) {
    phase(id: $phase) {
      id, name, cards_count
      cards(first: 30, after: $cursor) {
        ... CardConnectionData
      }
    }
  }
"""

为了实际提供变量值,它们作为普通字典传递

To actually supply the variable values, they get passed as an ordinary dictionary

variables = {
  "phase": phaseID,
  "cursor": pointer
}

实际的请求正文是一个简单的 JSON 结构.您也可以将其构建为字典:

The actual request body is a straightforward JSON structure. You can construct this as a dictionary too:

body = {
  "query": query,
  "variables": variables
}

现在您可以使用标准的json模块将其格式化为字符串

Now you can use the standard json module to format it to a string

print(json.dumps(body))

或将其传递给诸如 requests 可以直接接受对象并为您编码的包.

or pass it along to something like the requests package that can directly accept the object and encode it for you.

这篇关于Python GraphQL API 调用组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆