feat: fix gqlclient race condition

This commit is contained in:
Andrew Bastin
2021-11-02 19:11:16 +05:30
committed by liyasthomas
parent 874b846e60
commit b0dd6b0bd6

View File

@@ -7,6 +7,7 @@ import {
watchSyncEffect, watchSyncEffect,
WatchStopHandle, WatchStopHandle,
set, set,
isRef,
} from "@nuxtjs/composition-api" } from "@nuxtjs/composition-api"
import { import {
createClient, createClient,
@@ -120,8 +121,8 @@ const createHoppClient = () =>
}), }),
fetchExchange, fetchExchange,
subscriptionExchange({ subscriptionExchange({
// @ts-expect-error: An issue with the Urql typing
forwardSubscription: (operation) => forwardSubscription: (operation) =>
// @ts-expect-error: An issue with the Urql typing
subscriptionClient.request(operation), subscriptionClient.request(operation),
}), }),
], ],
@@ -259,39 +260,42 @@ export const useGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
onEnd(() => { onEnd(() => {
loading.value = false loading.value = false
isStale.value = false isStale.value = false
console.log("end")
}), }),
subscribe((res) => { subscribe((res) => {
data.value = pipe( if (res.operation.key === request.value.key) {
// The target data.value = pipe(
res.data as DocType | undefined, // The target
// Define what happens if data does not exist (it is an error) res.data as DocType | undefined,
E.fromNullable( // Define what happens if data does not exist (it is an error)
pipe( E.fromNullable(
// Take the network error value pipe(
res.error?.networkError, // Take the network error value
// If it null, set the left to the generic error name res.error?.networkError,
E.fromNullable(res.error?.message), // If it null, set the left to the generic error name
E.match( E.fromNullable(res.error?.message),
// The left case (network error was null) E.match(
(gqlErr) => // The left case (network error was null)
<GQLError<DocErrorType>>{ (gqlErr) =>
type: "gql_error", <GQLError<DocErrorType>>{
error: parseGQLErrorString( type: "gql_error",
gqlErr ?? "" error: parseGQLErrorString(
) as DocErrorType, gqlErr ?? ""
}, ) as DocErrorType,
// The right case (it was a GraphQL Error) },
(networkErr) => // The right case (it was a GraphQL Error)
<GQLError<DocErrorType>>{ (networkErr) =>
type: "network_error", <GQLError<DocErrorType>>{
error: networkErr, type: "network_error",
} error: networkErr,
}
)
) )
) )
) )
)
loading.value = false loading.value = false
}
}) })
).unsubscribe ).unsubscribe
) )
@@ -302,8 +306,11 @@ export const useGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
const execute = (updatedVars?: DocVarType) => { const execute = (updatedVars?: DocVarType) => {
if (updatedVars) { if (updatedVars) {
set(args, "variables", updatedVars) if (isRef(args.variables)) {
// args.variables = updatedVars as any args.variables.value = updatedVars
} else {
set(args, "variables", updatedVars)
}
} }
isPaused.value = false isPaused.value = false