Add support for cursor based pagination in query params

This commit is contained in:
Andrew Kingston 2022-01-05 17:28:57 +00:00
parent 4728b52780
commit b7d7923904
3 changed files with 20 additions and 7 deletions

View File

@ -35,7 +35,7 @@ export default class QueryFetch extends DataFetch {
// Add pagination to query if supported // Add pagination to query if supported
let queryPayload = { queryId: datasource?._id, parameters } let queryPayload = { queryId: datasource?._id, parameters }
if (supportsPagination) { if (supportsPagination) {
const requestCursor = type === "page" ? parseInt(cursor || 0) : cursor const requestCursor = type === "page" ? parseInt(cursor || 1) : cursor
queryPayload.pagination = { page: requestCursor, limit } queryPayload.pagination = { page: requestCursor, limit }
} }
@ -49,11 +49,12 @@ export default class QueryFetch extends DataFetch {
if (type === "page") { if (type === "page") {
// For "page number" pagination, increment the existing page number // For "page number" pagination, increment the existing page number
nextCursor = queryPayload.pagination.page + 1 nextCursor = queryPayload.pagination.page + 1
hasNextPage = data?.length === limit && limit > 0
} else { } else {
// For "cursor" pagination, the cursor should be in the response // For "cursor" pagination, the cursor should be in the response
nextCursor = pagination.cursor nextCursor = pagination?.cursor
hasNextPage = nextCursor != null
} }
hasNextPage = data?.length === limit && limit > 0
} }
return { return {

View File

@ -120,7 +120,7 @@ module RestModule {
this.config = config this.config = config
} }
async parseResponse(response: any) { async parseResponse(response: any, pagination: PaginationConfig | null) {
let data, raw, headers let data, raw, headers
const contentType = response.headers.get("content-type") || "" const contentType = response.headers.get("content-type") || ""
try { try {
@ -159,6 +159,13 @@ module RestModule {
for (let [key, value] of Object.entries(headers)) { for (let [key, value] of Object.entries(headers)) {
headers[key] = Array.isArray(value) ? value[0] : value headers[key] = Array.isArray(value) ? value[0] : value
} }
// Check if a pagination cursor exists in the response
let nextCursor = null
if (pagination?.responseParam) {
nextCursor = data?.[pagination.responseParam]
}
return { return {
data, data,
info: { info: {
@ -170,6 +177,9 @@ module RestModule {
raw, raw,
headers, headers,
}, },
pagination: {
cursor: nextCursor
}
} }
} }
@ -325,7 +335,7 @@ module RestModule {
this.startTimeMs = performance.now() this.startTimeMs = performance.now()
const url = this.getUrl(path, queryString, pagination, paginationValues) const url = this.getUrl(path, queryString, pagination, paginationValues)
const response = await fetch(url, input) const response = await fetch(url, input)
return await this.parseResponse(response) return await this.parseResponse(response, pagination)
} }
async create(opts: RestQuery) { async create(opts: RestQuery) {

View File

@ -47,11 +47,13 @@ class QueryRunner {
let output = threadUtils.formatResponse(await integration[queryVerb](query)) let output = threadUtils.formatResponse(await integration[queryVerb](query))
let rows = output, let rows = output,
info = undefined, info = undefined,
extra = undefined extra = undefined,
pagination = undefined
if (threadUtils.hasExtraData(output)) { if (threadUtils.hasExtraData(output)) {
rows = output.data rows = output.data
info = output.info info = output.info
extra = output.extra extra = output.extra
pagination = output.pagination
} }
// transform as required // transform as required
@ -90,7 +92,7 @@ class QueryRunner {
integration.end() integration.end()
} }
return { rows, keys, info, extra } return { rows, keys, info, extra, pagination }
} }
async runAnotherQuery(queryId, parameters) { async runAnotherQuery(queryId, parameters) {