import { NgZone } from '@angular/core' ; import type { ApolloQueryResult , ObservableQuery , ApolloError , FetchMoreQueryOptions , FetchMoreOptions , SubscribeToMoreOptions , UpdateQueryOptions , TypedDocumentNode , } from '@apollo/client/core' ; import { NetworkStatus } from '@apollo/client/core' ; import { Observable , from } from 'rxjs' ; import { wrapWithZone , fixObservable } from './utils' ; import { WatchQueryOptions , EmptyObject } from './types' ; function useInitialLoading < T , V > ( obsQuery ) { return function useInitialLoadingOperator < T > ( source ) : Observable < T > { return new Observable ( function useInitialLoadingSubscription ( subscriber ) { const currentResult = obsQuery . getCurrentResult ( ) ; const { loading , errors , error , partial , data } = currentResult ; const { partialRefetch , fetchPolicy } = obsQuery . options ; const hasError = errors || error ; if ( partialRefetch && partial && ( ! data || Object . keys ( data ) . length === 0 ) && fetchPolicy !== 'cache-only' && ! loading && ! hasError ) { subscriber . next ( { ... currentResult , loading : true , networkStatus : NetworkStatus . loading , } as any ) ; } return source . subscribe ( subscriber ) ; } ) ; } ; } export type QueryRefFromDocument < T extends TypedDocumentNode > = T extends TypedDocumentNode < infer R , infer V > ? QueryRef < R , V > : never ; export class QueryRef < T , V = EmptyObject > { public valueChanges : Observable < ApolloQueryResult < T > > ; public queryId : ObservableQuery < T , V > [ 'queryId' ] ; constructor ( private obsQuery , ngZone , options ) { const wrapped = wrapWithZone ( from ( fixObservable ( this . obsQuery ) ) , ngZone ) ; this . valueChanges = options . useInitialLoading ? wrapped . pipe ( useInitialLoading ( this . obsQuery ) ) : wrapped ; this . queryId = this . obsQuery . queryId ; } public get options ( ) { return this . obsQuery . options ; } public get variables ( ) { return this . obsQuery . variables ; } public result ( ) { return this . obsQuery . result ( ) ; } public getCurrentResult ( ) { return this . obsQuery . getCurrentResult ( ) ; } public getLastResult ( ) { return this . obsQuery . getLastResult ( ) ; } public getLastError ( ) { return this . obsQuery . getLastError ( ) ; } public resetLastResults ( ) { return this . obsQuery . resetLastResults ( ) ; } public refetch ( variables ? ) { return this . obsQuery . refetch ( variables ) ; } public fetchMore < K extends keyof V > ( fetchMoreOptions ) { return this . obsQuery . fetchMore ( fetchMoreOptions ) ; } public subscribeToMore < MT = any , MV = EmptyObject > ( options ) { return this . obsQuery . subscribeToMore ( options as any ) ; } public updateQuery ( mapFn ) { return this . obsQuery . updateQuery ( mapFn ) ; } public stopPolling ( ) { return this . obsQuery . stopPolling ( ) ; } public startPolling ( pollInterval ) { return this . obsQuery . startPolling ( pollInterval ) ; } public setOptions ( opts ) { return this . obsQuery . setOptions ( opts ) ; } public setVariables ( variables ) { return this . obsQuery . setVariables ( variables ) ; } }