基于游标的分页 graphql
Cursor based pagination graphql
我正在通过 koa 服务器从 graphql 检索数据。为此,我写了一个函数 requestData(token, queryName, cursor)
来调用 graphql。
我想重复请求,直到游标为 null 或未定义。
我总是需要使用最后一个光标:
lastCursor = edges[edges.length - 1].cursor;
在终端中我得到以下输出:
null
eyJsYXN0X2lkIjo2MTQzNTk3ODcxMjc0LCJsYXN0X3ZhbHVlIjoiNjE0MzU5Nzg3MTI3NCJ9
即使我有更多数据,我仍会收到此输出。有人可以帮我弄清楚我的代码有什么问题吗?
server.js部分
router.post('/getAllProducts', bodyParser(), async (ctx, next) => {
let data;
let edges;
let lastCursor = null;
const { accessToken } = ctx.session;
ctx.response.status = 200;
requestData(accessToken, GET_ALL_PRODUCTS, lastCursor).then((res) => {
data = res.data.data;
edges = data.products.edges;
lastCursor = edges[edges.length - 1].cursor;
setTimeout(() => {
requestData(accessToken, GET_ALL_PRODUCTS, lastCursor);
}, 1000);
});
});
function requestData(token, queryName, cursor) {
const variables = { after: cursor };
console.log(cursor);
const res = axios({
headers: {
'X-Shopify-Access-Token': token,
},
method: 'post',
data: {
query: queryName,
variables: variables,
},
url: url,
});
return res;
}
看来您需要对您的代码实现一些递归 - 像这样:
router.post('/getAllProducts', bodyParser(), async (ctx, next) => {
let allProducts;
const { accessToken } = ctx.session;
ctx.response.status = 200;
function loadData(cursor) {
requestData(accessToken, GET_ALL_PRODUCTS, cursor).then((res) => {
const data = res.data.data;
// process data here, eg: allProducts = [...allProducts, ...data];
const edges = data.products.edges;
const nextCursor = edges.length ? edges[edges.length - 1].cursor : null;
if (nextCursor) {
setTimeout(() => {
loadData(nextCursor);
}, 1000);
} else {
// now we have all data
sendToClient(allProducts);
}
});
}
loadData(null);
});
function requestData(token, queryName, cursor) {
const variables = { after: cursor };
console.log(cursor);
const res = axios({
headers: {
'X-Shopify-Access-Token': token,
},
method: 'post',
data: {
query: queryName,
variables: variables,
},
url: url,
});
return res;
}
我正在通过 koa 服务器从 graphql 检索数据。为此,我写了一个函数 requestData(token, queryName, cursor)
来调用 graphql。
我想重复请求,直到游标为 null 或未定义。
我总是需要使用最后一个光标:
lastCursor = edges[edges.length - 1].cursor;
在终端中我得到以下输出:
null
eyJsYXN0X2lkIjo2MTQzNTk3ODcxMjc0LCJsYXN0X3ZhbHVlIjoiNjE0MzU5Nzg3MTI3NCJ9
即使我有更多数据,我仍会收到此输出。有人可以帮我弄清楚我的代码有什么问题吗?
server.js部分
router.post('/getAllProducts', bodyParser(), async (ctx, next) => {
let data;
let edges;
let lastCursor = null;
const { accessToken } = ctx.session;
ctx.response.status = 200;
requestData(accessToken, GET_ALL_PRODUCTS, lastCursor).then((res) => {
data = res.data.data;
edges = data.products.edges;
lastCursor = edges[edges.length - 1].cursor;
setTimeout(() => {
requestData(accessToken, GET_ALL_PRODUCTS, lastCursor);
}, 1000);
});
});
function requestData(token, queryName, cursor) {
const variables = { after: cursor };
console.log(cursor);
const res = axios({
headers: {
'X-Shopify-Access-Token': token,
},
method: 'post',
data: {
query: queryName,
variables: variables,
},
url: url,
});
return res;
}
看来您需要对您的代码实现一些递归 - 像这样:
router.post('/getAllProducts', bodyParser(), async (ctx, next) => {
let allProducts;
const { accessToken } = ctx.session;
ctx.response.status = 200;
function loadData(cursor) {
requestData(accessToken, GET_ALL_PRODUCTS, cursor).then((res) => {
const data = res.data.data;
// process data here, eg: allProducts = [...allProducts, ...data];
const edges = data.products.edges;
const nextCursor = edges.length ? edges[edges.length - 1].cursor : null;
if (nextCursor) {
setTimeout(() => {
loadData(nextCursor);
}, 1000);
} else {
// now we have all data
sendToClient(allProducts);
}
});
}
loadData(null);
});
function requestData(token, queryName, cursor) {
const variables = { after: cursor };
console.log(cursor);
const res = axios({
headers: {
'X-Shopify-Access-Token': token,
},
method: 'post',
data: {
query: queryName,
variables: variables,
},
url: url,
});
return res;
}