apollo.ts (hoppscotch-2.0.0) | : | apollo.ts (hoppscotch-2.1.0) | ||
---|---|---|---|---|
import { | import { | |||
ApolloClient, | ApolloClient, | |||
HttpLink, | HttpLink, | |||
InMemoryCache, | InMemoryCache, | |||
QueryOptions, | ||||
OperationVariables, | ||||
split, | split, | |||
ApolloError, | ||||
isApolloError as _isApolloError, | ||||
} from "@apollo/client/core" | } from "@apollo/client/core" | |||
import { WebSocketLink } from "@apollo/client/link/ws" | import { WebSocketLink } from "@apollo/client/link/ws" | |||
import { setContext } from "@apollo/client/link/context" | import { setContext } from "@apollo/client/link/context" | |||
import { getMainDefinition } from "@apollo/client/utilities" | import { getMainDefinition } from "@apollo/client/utilities" | |||
import { ref, onMounted, onBeforeUnmount, Ref } from "@nuxtjs/composition-api" | ||||
import { authIdToken$ } from "./fb/auth" | import { authIdToken$ } from "./fb/auth" | |||
let authToken: String | null = null | let authToken: String | null = null | |||
export function registerApolloAuthUpdate() { | export function registerApolloAuthUpdate() { | |||
authIdToken$.subscribe((token) => { | authIdToken$.subscribe((token) => { | |||
authToken = token | authToken = token | |||
}) | }) | |||
} | } | |||
skipping to change at line 87 | skipping to change at line 92 | |||
query: { | query: { | |||
fetchPolicy: "network-only", | fetchPolicy: "network-only", | |||
errorPolicy: "ignore", | errorPolicy: "ignore", | |||
}, | }, | |||
watchQuery: { | watchQuery: { | |||
fetchPolicy: "network-only", | fetchPolicy: "network-only", | |||
errorPolicy: "ignore", | errorPolicy: "ignore", | |||
}, | }, | |||
}, | }, | |||
}) | }) | |||
export function isApolloError(x: any): x is ApolloError { | ||||
return _isApolloError(x) | ||||
} | ||||
export function useGQLQuery<T = any, TVariables = OperationVariables>( | ||||
options: QueryOptions<TVariables, T> | ||||
): { loading: Ref<boolean>; data: Ref<T | ApolloError | undefined> } { | ||||
const loading = ref(true) | ||||
const data = ref<T | ApolloError | undefined>() | ||||
let subscription: ZenObservable.Subscription | null = null | ||||
onMounted(() => { | ||||
subscription = apolloClient.watchQuery(options).subscribe((result) => { | ||||
if (result.error) { | ||||
data.value = result.error | ||||
} else { | ||||
data.value = result.data | ||||
} | ||||
loading.value = false | ||||
}) | ||||
}) | ||||
onBeforeUnmount(() => { | ||||
if (subscription) { | ||||
subscription.unsubscribe() | ||||
} | ||||
}) | ||||
return { | ||||
loading, | ||||
data, | ||||
} | ||||
} | ||||
End of changes. 4 change blocks. | ||||
0 lines changed or deleted | 5 lines changed or added |