forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request galaxyproject#16914 from jmchilton/infinite_scrolling
Fix GQL infinite scrolling in tool shed.
- Loading branch information
Showing
5 changed files
with
103 additions
and
79 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
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
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,63 @@ | ||
import { ref, type Ref } from "vue" | ||
import type { OperationVariables } from "@apollo/client/core/index.js" | ||
import { useLazyQuery } from "@vue/apollo-composable" | ||
import { DocumentParameter } from "@vue/apollo-composable/dist/useQuery" | ||
import type { Maybe, PageInfo } from "@/gql/graphql" | ||
|
||
export interface InterfaceHasNode<NodeType> { | ||
node?: Maybe<NodeType> | ||
} | ||
|
||
export interface RelayQuery<NodeType> { | ||
pageInfo: PageInfo | ||
edges: Array<Maybe<InterfaceHasNode<NodeType>>> | ||
} | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export function useRelayInfiniteScrollQuery<ResultType, NodeType>( | ||
query: DocumentParameter<any, OperationVariables>, | ||
variables: OperationVariables, | ||
toResult: (arg: ResultType) => RelayQuery<NodeType> | ||
) { | ||
const records = ref<NodeType[]>([]) as Ref<Array<NodeType>> | ||
const lastResult = ref<ResultType | null>(null) as Ref<ResultType | null> | ||
const hasNextPage = ref(true) | ||
|
||
const { load, error, loading, fetchMore } = useLazyQuery(query, variables, { | ||
fetchPolicy: "network-only", | ||
nextFetchPolicy: "network-only", | ||
}) | ||
|
||
function handleResult(result: ResultType) { | ||
lastResult.value = result | ||
const asResult: RelayQuery<NodeType> = toResult(result) | ||
hasNextPage.value = asResult.pageInfo.hasNextPage | ||
const nodes: NodeType[] = asResult.edges.flatMap((x) => (x && x.node ? [x.node] : [])) | ||
records.value.push(...nodes) | ||
} | ||
|
||
async function onScroll() { | ||
if (hasNextPage.value && lastResult.value != null && fetchMore != null) { | ||
const asResult: RelayQuery<NodeType> = toResult(lastResult.value) | ||
const endCursor = asResult.pageInfo.endCursor | ||
const fetchMoreVariables = { ...variables, cursor: endCursor } | ||
const fetchMorePromise = fetchMore({ | ||
variables: fetchMoreVariables, | ||
/* | ||
fetchPolicy: "network-only", | ||
nextFetchPolicy: "network-only", | ||
*/ | ||
}) | ||
if (fetchMorePromise) { | ||
fetchMorePromise.then((result) => handleResult(result.data as ResultType)) | ||
} | ||
} | ||
} | ||
|
||
const loadPromise = load() | ||
if (loadPromise) { | ||
loadPromise.then(handleResult) | ||
} | ||
|
||
return { records, error, loading, onScroll } | ||
} |