我在GraphQL中有一个API,并尝试调用它。第一次调用获取前50条记录工作正常,我的查询是,
var firstQuery = "{\"query\": \"{products(first: 100) {totalCount edges {node {id,name,brand{name},prices{quantity,price},tags{name},barcodes{code},lastCost,cost,caseQuantity,image} cursor} pageInfo { endCursor hasNextPage }}}\"}";
var task = flurlAutUrl
.WithHeader("Content-Type", "application/json")
.WithHeader("Accept", "application/json")
.PostStringAsync(firstQuery)
.ReceiveJson();
task.Wait();
当尝试获取下一个50条记录时,在task.wait()处得到错误400,错误请求,我查询第二页记录,
var afterQueryLeft = "{\"query\": \"{products(first: 100 after:";
var afterQueryRight = ") {totalCount edges {node {id,name,brand{name},prices{quantity,price},tags{name},barcodes{code},lastCost,cost,caseQuantity,image} cursor} pageInfo { hasNextPage, endCursor }}}\"}";
while (hasNextPage)
{
//var afterQuery = "{\"query\": \"{products(first: 100 after:"\"lastCursor) {totalCount edges {node {id,name,brand{name},prices{quantity,price},tags{name},barcodes{code},lastCost,cost,caseQuantity,image} cursor} pageInfo { endCursor hasNextPage }}}\"}";
var nextPageQuery = afterQueryLeft + "\"" + lastCursor + "\"" + afterQueryRight;
var nextPageTask = flurlAutUrl
.WithHeader("Content-Type", "application/json")
.WithHeader("Accept", "application/json")
.PostStringAsync(nextPageQuery)
.ReceiveJson();
nextPageTask.Wait();
}
我的查询中有什么错误,这个查询在postman中工作正常(200 ok)
1条答案
按热度按时间wf82jlnq1#
对原问题的评论是正确的,因为应该使用GraphQL变量。此外,它应该是使用async/await的异步代码。
但是,无论如何,即使使用Flurl,检索和处理分页的GraphQL结果仍然是一件真实的的痛苦的事情...
因此,我发布了FlurlGraphQL库(Flurl的扩展),它大大简化了GraphQL和Flurl的使用。
使用FlurlGraphQL,原始问题可以实现为:
但如果您只想枚举页面并获取数据,这实际上更容易: