You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google App Script中GraphQL请求添加日期筛选与分页失败求助

解决Google App Script中GraphQL请求添加日期筛选与分页的问题

你的基础GraphQL请求能正常运行,但添加筛选和分页变量的版本失败,大概率是变量格式不符合API要求或参数名称不匹配API schema导致的。以下是排查和修正方案:

第一步:先排查API返回的具体错误

你启用了muteHttpExceptions:true,但没有打印完整的错误信息,建议先添加日志看API的反馈:

// 在解析响应前添加这两行
Logger.log("响应状态码: " + response.getResponseCode());
Logger.log("原始响应内容: " + response.getContentText());

通过这些日志,你能直接看到API返回的错误提示(比如变量类型错误、参数名称错误等),这是最快定位问题的方式。

常见问题与修正方案

1. 日期格式不符合API要求

大部分GraphQL API的日期时间字段要求使用ISO 8601格式(带时区),而不是纯日期字符串。你可以修改createdAtRange的日期值:

filters: {
  createdAtRange: {
    start: "2023-06-01T00:00:00Z",
    end: "2023-06-01T23:59:59Z"
  }
}

2. 分页参数名称不匹配API定义

不同API的分页参数命名可能不同,比如有些API用page/size而非currentPage/perPage。你需要对照该API的GraphQL schema文档,确认PaginationInput的字段名称。

3. 变量定义的语法细节

确保你的GraphQL查询中,变量的类型声明与API schema完全一致(比如LeadFiltersInput和PaginationInput是否是API定义的正确类型名)。

修正后的完整代码示例

function make_query_with_filters() {
  
  const url = "https://open-api.eprocorpo.com.br/graphql";
  const token = "mytoken";

  const response = UrlFetchApp.fetch(url, {
        method: 'POST',
        headers: { 
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + token
        },
        payload: JSON.stringify({
          query: `query ($filters: LeadFiltersInput, $pagination: PaginationInput) {
              fetchLeads(filters: $filters, pagination: $pagination) {
                  data {
                    createdAt
                    id
                    source {
                      title
                    }
                    status {
                      label
                    }
                    store {
                      name
                    }
                  }
                  // 新增分页元数据,方便验证分页是否生效
                  pagination {
                    currentPage
                    perPage
                    totalPages
                    totalCount
                  }
                }`,
            variables: {
              filters: {
                    createdAtRange: {
                        start: "2023-06-01T00:00:00Z",
                        end: "2023-06-01T23:59:59Z"
                    }
                },
              pagination: {
                  currentPage: 1,
                  perPage: 10
              }
          }   
        }),
        muteHttpExceptions: true
      });

  // 打印关键调试信息
  Logger.log("响应状态码: " + response.getResponseCode());
  Logger.log("原始响应内容: " + response.getContentText());

  const resposta = JSON.parse(response.getContentText());
  Logger.log("解析后的数据: " + JSON.stringify(resposta, null, 2));
}

额外建议

如果还是无法解决,直接查看该API的GraphQL playground或文档:

  • 确认fetchLeads接口支持的参数格式
  • 验证LeadFiltersInput的结构(比如是否是createdAtRange还是分开的startDate/endDate)
  • 确认分页参数的正确命名

内容的提问来源于stack exchange,提问作者user3347814

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:57:39