feat: introducing gql polling

This commit is contained in:
Andrew Bastin
2021-11-01 22:26:30 +05:30
committed by liyasthomas
parent 94763dcb31
commit afd414fa3f
2 changed files with 26 additions and 0 deletions

View File

@@ -116,6 +116,7 @@ const myTeams = useGQLQuery<
MyTeamsQueryError MyTeamsQueryError
>({ >({
query: MyTeamsDocument, query: MyTeamsDocument,
pollDuration: 5000,
}) })
watchEffect(() => { watchEffect(() => {

View File

@@ -141,6 +141,7 @@ type UseQueryOptions<T = any, V = object> = {
updateSubs?: MaybeRef<GraphQLRequest<any, object>[]> updateSubs?: MaybeRef<GraphQLRequest<any, object>[]>
defer?: boolean defer?: boolean
pollDuration?: number | undefined
} }
/** /**
@@ -171,6 +172,8 @@ export const useGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
const isPaused: Ref<boolean> = ref(args.defer ?? false) const isPaused: Ref<boolean> = ref(args.defer ?? false)
const pollDuration: Ref<number | null> = ref(args.pollDuration ?? null)
const request: Ref<GraphQLRequest<DocType, DocVarType>> = ref( const request: Ref<GraphQLRequest<DocType, DocVarType>> = ref(
createRequest<DocType, DocVarType>( createRequest<DocType, DocVarType>(
args.query, args.query,
@@ -180,6 +183,25 @@ export const useGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
const source: Ref<Source<OperationResult> | undefined> = ref() const source: Ref<Source<OperationResult> | undefined> = ref()
// Toggles between true and false to cause the polling operation to tick
const pollerTick: Ref<boolean> = ref(true)
stops.push(
watchEffect(
(onInvalidate) => {
if (pollDuration.value !== null && !isPaused.value) {
const handle = setInterval(() => {
pollerTick.value = !pollerTick.value
}, pollDuration.value)
onInvalidate(() => {
clearInterval(handle)
})
}
}
)
)
stops.push( stops.push(
watchEffect( watchEffect(
() => { () => {
@@ -199,6 +221,9 @@ export const useGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
stops.push( stops.push(
watchEffect( watchEffect(
() => { () => {
// Just listen to the polling ticks
pollerTick.value
source.value = !isPaused.value source.value = !isPaused.value
? client.value.executeQuery<DocType, DocVarType>(request.value, { ? client.value.executeQuery<DocType, DocVarType>(request.value, {
requestPolicy: "cache-and-network", requestPolicy: "cache-and-network",