1
1
import { from , Observable } from 'rxjs' ;
2
2
import { NgZone } from '@angular/core' ;
3
3
import type {
4
- ApolloError ,
5
4
ApolloQueryResult ,
6
- FetchMoreQueryOptions ,
7
5
ObservableQuery ,
8
6
OperationVariables ,
9
7
SubscribeToMoreOptions ,
10
8
TypedDocumentNode ,
11
- UpdateQueryOptions ,
12
9
} from '@apollo/client/core' ;
13
10
import { NetworkStatus } from '@apollo/client/core' ;
14
11
import { EmptyObject , WatchQueryOptions } from './types' ;
@@ -46,14 +43,14 @@ function useInitialLoading<T, V extends OperationVariables>(obsQuery: Observable
46
43
export type QueryRefFromDocument < T extends TypedDocumentNode > =
47
44
T extends TypedDocumentNode < infer R , infer V > ? QueryRef < R , V & OperationVariables > : never ;
48
45
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' ] ;
52
49
53
50
constructor (
54
- private readonly obsQuery : ObservableQuery < T , V > ,
51
+ private readonly obsQuery : ObservableQuery < TData , TVariables > ,
55
52
ngZone : NgZone ,
56
- options : WatchQueryOptions < V , T > ,
53
+ options : WatchQueryOptions < TVariables , TData > ,
57
54
) {
58
55
const wrapped = wrapWithZone ( from ( fixObservable ( this . obsQuery ) ) , ngZone ) ;
59
56
@@ -65,69 +62,80 @@ export class QueryRef<T, V extends OperationVariables = EmptyObject> {
65
62
66
63
// ObservableQuery's methods
67
64
68
- public get options ( ) {
65
+ public get options ( ) : ObservableQuery < TData , TVariables > [ 'options' ] {
69
66
return this . obsQuery . options ;
70
67
}
71
68
72
- public get variables ( ) {
69
+ public get variables ( ) : ObservableQuery < TData , TVariables > [ 'variables' ] {
73
70
return this . obsQuery . variables ;
74
71
}
75
72
76
- public result ( ) : Promise < ApolloQueryResult < T > > {
73
+ public result ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'result' ] > {
77
74
return this . obsQuery . result ( ) ;
78
75
}
79
76
80
- public getCurrentResult ( ) : ApolloQueryResult < T > {
77
+ public getCurrentResult ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'getCurrentResult' ] > {
81
78
return this . obsQuery . getCurrentResult ( ) ;
82
79
}
83
80
84
- public getLastResult ( ) : ApolloQueryResult < T > | undefined {
81
+ public getLastResult ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'getLastResult' ] > {
85
82
return this . obsQuery . getLastResult ( ) ;
86
83
}
87
84
88
- public getLastError ( ) : ApolloError | undefined {
85
+ public getLastError ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'getLastError' ] > {
89
86
return this . obsQuery . getLastError ( ) ;
90
87
}
91
88
92
- public resetLastResults ( ) : void {
89
+ public resetLastResults ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'resetLastResults' ] > {
93
90
return this . obsQuery . resetLastResults ( ) ;
94
91
}
95
92
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' ] > {
97
96
return this . obsQuery . refetch ( variables ) ;
98
97
}
99
98
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' ] > {
103
102
return this . obsQuery . fetchMore ( fetchMoreOptions ) ;
104
103
}
105
104
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 ) ;
112
112
}
113
113
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' ] > {
115
117
return this . obsQuery . updateQuery ( mapFn ) ;
116
118
}
117
119
118
- public stopPolling ( ) : void {
120
+ public stopPolling ( ) : ReturnType < ObservableQuery < TData , TVariables > [ 'stopPolling' ] > {
119
121
return this . obsQuery . stopPolling ( ) ;
120
122
}
121
123
122
- public startPolling ( pollInterval : number ) : void {
124
+ public startPolling (
125
+ pollInterval : Parameters < ObservableQuery < TData , TVariables > [ 'startPolling' ] > [ 0 ] ,
126
+ ) : ReturnType < ObservableQuery < TData , TVariables > [ 'startPolling' ] > {
123
127
return this . obsQuery . startPolling ( pollInterval ) ;
124
128
}
125
129
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' ] > {
127
133
return this . obsQuery . setOptions ( opts ) ;
128
134
}
129
135
130
- public setVariables ( variables : V ) {
136
+ public setVariables (
137
+ variables : Parameters < ObservableQuery < TData , TVariables > [ 'setVariables' ] > [ 0 ] ,
138
+ ) : ReturnType < ObservableQuery < TData , TVariables > [ 'setVariables' ] > {
131
139
return this . obsQuery . setVariables ( variables ) ;
132
140
}
133
141
}
0 commit comments