-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Platform]: batch loading in variant page (#550)
- Loading branch information
Showing
9 changed files
with
164 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/sections/src/variant/GWASCredibleSets/GWASCredibleSetsQuery.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/sections/src/variant/QTLCredibleSets/QTLCredibleSetsQuery.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import _ from "lodash"; | ||
|
||
import defaultClient from "../client"; | ||
import { tableChunkSize } from "../constants"; | ||
import { DocumentNode } from "@apollo/client"; | ||
|
||
type useBatchQueryProps = { | ||
query: DocumentNode; | ||
variables: Record<string, unknown>; | ||
dataPath: string; | ||
size?: number; | ||
client?: any; | ||
rowField?: string; | ||
countField?: string; | ||
}; | ||
/** | ||
* Provides a function to asynchronously batch-download a whole dataset from | ||
* the backend. | ||
* | ||
* The function uses the parameter downloaderChunkSize from the configuration.js | ||
* file to determine the size of the chunks to fetch. | ||
* | ||
* @param {import('graphql').DocumentNode} query Query to run to fetch the data. | ||
* @param {import('apollo-client').QueryOptions} variables Variables object for the query. | ||
* @param {string} dataPath Path where the data array, row count and cursor are inside the query's result. | ||
* @param {string} [rowField=rows] field in dataPath containing the rows. Default: 'rows'. | ||
* @param {string} [countField=count] field in dataPath containing the row count. Default: 'count'. | ||
* | ||
* @returns {Function} Function that will fetch the whole dataset. | ||
*/ | ||
function useBatchQuery({ | ||
query, | ||
variables, | ||
dataPath, | ||
client = defaultClient, | ||
size = tableChunkSize, | ||
rowField = "rows", | ||
countField = "count", | ||
}: useBatchQueryProps): () => Promise<Record<string, unknown>[]> { | ||
const rowPath = `${dataPath}.${rowField}`; | ||
const countPath = `${dataPath}.${countField}`; | ||
|
||
const getDataChunk = async (index: number) => | ||
client.query({ | ||
query, | ||
variables: { ...variables, index, size }, | ||
}); | ||
|
||
return async function getWholeDataset() { | ||
const chunkPromises = []; | ||
let data: Array<Record<string, unknown>> = []; | ||
let index = 0; | ||
|
||
const firstChunk = await getDataChunk(index); | ||
|
||
data = [...getRows(firstChunk, rowPath)]; | ||
index += 1; | ||
|
||
const count = Math.ceil(_.get(firstChunk, countPath) / size); | ||
|
||
while (index < count) { | ||
chunkPromises.push(getDataChunk(index)); | ||
index += 1; | ||
} | ||
|
||
const remainingChunks = await Promise.all(chunkPromises); | ||
|
||
remainingChunks.forEach(chunk => { | ||
data = [...data, ...getRows(chunk, rowPath)]; | ||
}); | ||
|
||
const wholeData = setRows(firstChunk, rowPath, data); | ||
|
||
return wholeData; | ||
}; | ||
} | ||
|
||
function getRows(data: Record<string, unknown>, dataPath: string) { | ||
return _.get(data, dataPath, []); | ||
} | ||
|
||
function setRows( | ||
res: Promise<Record<string, unknown>>, | ||
dataPath: string, | ||
rows: Record<string, unknown>[] | ||
): Promise<Record<string, unknown>> | null { | ||
if (!res || !rows) return null; | ||
const wholeRes = structuredClone(res); | ||
let obj = wholeRes; | ||
const dataPathSplit: string[] = dataPath.split("."); | ||
for (let i = 0, len = dataPathSplit.length; i < len; i++) { | ||
obj = obj[dataPathSplit[i]]; | ||
} | ||
Object.assign(obj, rows); | ||
return wholeRes; | ||
} | ||
|
||
export default useBatchQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type responseType = { | ||
data: Record<string, unknown> | null; | ||
error: Record<string, unknown> | null; | ||
loading: boolean; | ||
}; |