11import { from , Observable } from 'rxjs' ;
22import { NgZone } from '@angular/core' ;
33import type {
4- ApolloError ,
54 ApolloQueryResult ,
6- FetchMoreQueryOptions ,
75 ObservableQuery ,
86 OperationVariables ,
97 SubscribeToMoreOptions ,
108 TypedDocumentNode ,
11- UpdateQueryOptions ,
129} from '@apollo/client/core' ;
1310import { NetworkStatus } from '@apollo/client/core' ;
1411import { EmptyObject , WatchQueryOptions } from './types' ;
@@ -46,14 +43,14 @@ function useInitialLoading<T, V extends OperationVariables>(obsQuery: Observable
4643export type QueryRefFromDocument < T extends TypedDocumentNode > =
4744 T extends TypedDocumentNode < infer R , infer V > ? QueryRef < R , V & OperationVariables > : never ;
4845
49- export class QueryRef < T , V extends OperationVariables = EmptyObject > {
50- public valueChanges : Observable < ApolloQueryResult < T > > ;
51- public queryId : ObservableQuery < T , V > [ 'queryId' ] ;
46+ export class QueryRef < TData , TVariables extends OperationVariables = EmptyObject > {
47+ public readonly valueChanges : Observable < ApolloQueryResult < TData > > ;
48+ public readonly queryId : ObservableQuery < TData , TVariables > [ 'queryId' ] ;
5249
5350 constructor (
54- private readonly obsQuery : ObservableQuery < T , V > ,
51+ private readonly obsQuery : ObservableQuery < TData , TVariables > ,
5552 ngZone : NgZone ,
56- options : WatchQueryOptions < V , T > ,
53+ options : WatchQueryOptions < TVariables , TData > ,
5754 ) {
5855 const wrapped = wrapWithZone ( from ( fixObservable ( this . obsQuery ) ) , ngZone ) ;
5956
@@ -65,69 +62,80 @@ export class QueryRef<T, V extends OperationVariables = EmptyObject> {
6562
6663 // ObservableQuery's methods
6764
68- public get options ( ) {
65+ public get options ( ) : ObservableQuery < TData , TVariables > [ 'options' ] {
6966 return this . obsQuery . options ;
7067 }
7168
72- public get variables ( ) {
69+ public get variables ( ) : ObservableQuery < TData , TVariables > [ 'variables' ] {
7370 return this . obsQuery . variables ;
7471 }
7572
76- public result ( ) : Promise < ApolloQueryResult < T > > {
73+ public result ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'result' ] > {
7774 return this . obsQuery . result ( ) ;
7875 }
7976
80- public getCurrentResult ( ) : ApolloQueryResult < T > {
77+ public getCurrentResult ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'getCurrentResult' ] > {
8178 return this . obsQuery . getCurrentResult ( ) ;
8279 }
8380
84- public getLastResult ( ) : ApolloQueryResult < T > | undefined {
81+ public getLastResult ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'getLastResult' ] > {
8582 return this . obsQuery . getLastResult ( ) ;
8683 }
8784
88- public getLastError ( ) : ApolloError | undefined {
85+ public getLastError ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'getLastError' ] > {
8986 return this . obsQuery . getLastError ( ) ;
9087 }
9188
92- public resetLastResults ( ) : void {
89+ public resetLastResults ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'resetLastResults' ] > {
9390 return this . obsQuery . resetLastResults ( ) ;
9491 }
9592
96- public refetch ( variables ?: V ) : Promise < ApolloQueryResult < T > > {
93+ public refetch (
94+ variables ?: Parameters < ObservableQuery < TData , TVariables > [ 'refetch' ] > [ 0 ] ,
95+ ) : ReturnType < ObservableQuery < TData , TVariables > [ 'refetch' ] > {
9796 return this . obsQuery . refetch ( variables ) ;
9897 }
9998
100- public fetchMore < K = V > (
101- fetchMoreOptions : FetchMoreQueryOptions < K , T > ,
102- ) : Promise < ApolloQueryResult < T > > {
99+ public fetchMore < TFetchVars extends OperationVariables = TVariables > (
100+ fetchMoreOptions : Parameters < QueryRef < TData , TFetchVars > [ 'obsQuery' ] [ 'fetchMore' ] > [ 0 ] ,
101+ ) : ReturnType < QueryRef < TData , TFetchVars > [ 'obsQuery' ] [ 'fetchMore' ] > {
103102 return this . obsQuery . fetchMore ( fetchMoreOptions ) ;
104103 }
105104
106- public subscribeToMore < MT = any , MV = EmptyObject > (
107- options : SubscribeToMoreOptions < T , MV , MT > ,
108- ) : ( ) => void {
109- // XXX: there's a bug in apollo-client typings
110- // it should not inherit types from ObservableQuery
111- return this . obsQuery . subscribeToMore ( options as any ) ;
105+ public subscribeToMore <
106+ TSubscriptionData = TData ,
107+ TSubscriptionVariables extends OperationVariables = TVariables ,
108+ > (
109+ options : SubscribeToMoreOptions < TData , TSubscriptionVariables , TSubscriptionData , TVariables > ,
110+ ) : ReturnType < ObservableQuery < TData , TVariables > [ 'subscribeToMore' ] > {
111+ return this . obsQuery . subscribeToMore ( options ) ;
112112 }
113113
114- public updateQuery ( mapFn : ( previousQueryResult : T , options : UpdateQueryOptions < V > ) => T ) : void {
114+ public updateQuery (
115+ mapFn : Parameters < ObservableQuery < TData , TVariables > [ 'updateQuery' ] > [ 0 ] ,
116+ ) : ReturnType < ObservableQuery < TData , TVariables > [ 'updateQuery' ] > {
115117 return this . obsQuery . updateQuery ( mapFn ) ;
116118 }
117119
118- public stopPolling ( ) : void {
120+ public stopPolling ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'stopPolling' ] > {
119121 return this . obsQuery . stopPolling ( ) ;
120122 }
121123
122- public startPolling ( pollInterval : number ) : void {
124+ public startPolling (
125+ pollInterval : Parameters < ObservableQuery < TData , TVariables > [ 'startPolling' ] > [ 0 ] ,
126+ ) : ReturnType < ObservableQuery < TData , TVariables > [ 'startPolling' ] > {
123127 return this . obsQuery . startPolling ( pollInterval ) ;
124128 }
125129
126- public setOptions ( opts : Partial < WatchQueryOptions < V , T > > ) {
130+ public setOptions (
131+ opts : Parameters < ObservableQuery < TData , TVariables > [ 'setOptions' ] > [ 0 ] ,
132+ ) : ReturnType < ObservableQuery < TData , TVariables > [ 'setOptions' ] > {
127133 return this . obsQuery . setOptions ( opts ) ;
128134 }
129135
130- public setVariables ( variables : V ) {
136+ public setVariables (
137+ variables : Parameters < ObservableQuery < TData , TVariables > [ 'setVariables' ] > [ 0 ] ,
138+ ) : ReturnType < ObservableQuery < TData , TVariables > [ 'setVariables' ] > {
131139 return this . obsQuery . setVariables ( variables ) ;
132140 }
133141}
0 commit comments