Skip to content

Commit c114cf2

Browse files
committed
Revert "Let types flow naturally from ObservableQuery"
This reverts commit d734dd0.
1 parent 2f9a815 commit c114cf2

File tree

4 files changed

+97
-110
lines changed

4 files changed

+97
-110
lines changed

.changeset/thin-otters-move.md

-5
This file was deleted.

packages/apollo-angular/src/query-ref.ts

+86-65
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
ApolloQueryResult,
55
ObservableQuery,
66
OperationVariables,
7+
SubscribeToMoreOptions,
78
TypedDocumentNode,
89
} from '@apollo/client/core';
910
import { NetworkStatus } from '@apollo/client/core';
@@ -42,79 +43,99 @@ function useInitialLoading<T, V extends OperationVariables>(obsQuery: Observable
4243
export type QueryRefFromDocument<T extends TypedDocumentNode> =
4344
T extends TypedDocumentNode<infer R, infer V> ? QueryRef<R, V & OperationVariables> : never;
4445

45-
export class QueryRef<TData, TVariables extends OperationVariables = EmptyObject>
46-
implements
47-
Pick<
48-
ObservableQuery<TData, TVariables>,
49-
| 'queryId'
50-
| 'options'
51-
| 'variables'
52-
| 'result'
53-
| 'getCurrentResult'
54-
| 'getLastResult'
55-
| 'getLastError'
56-
| 'resetLastResults'
57-
| 'refetch'
58-
| 'fetchMore'
59-
| 'subscribeToMore'
60-
| 'updateQuery'
61-
| 'stopPolling'
62-
| 'startPolling'
63-
| 'setOptions'
64-
| 'setVariables'
65-
>
66-
{
46+
export class QueryRef<TData, TVariables extends OperationVariables = EmptyObject> {
6747
public readonly valueChanges: Observable<ApolloQueryResult<TData>>;
68-
69-
// Types flow straight from ObservableQuery
70-
public readonly queryId;
71-
public readonly result;
72-
public readonly getCurrentResult;
73-
public readonly getLastResult;
74-
public readonly getLastError;
75-
public readonly resetLastResults;
76-
public readonly refetch;
77-
public readonly fetchMore;
78-
public readonly subscribeToMore;
79-
public readonly updateQuery;
80-
public readonly stopPolling;
81-
public readonly startPolling;
82-
public readonly setOptions;
83-
public readonly setVariables;
48+
public readonly queryId: ObservableQuery<TData, TVariables>['queryId'];
8449

8550
constructor(
86-
private readonly query: ObservableQuery<TData, TVariables>,
51+
private readonly obsQuery: ObservableQuery<TData, TVariables>,
8752
ngZone: NgZone,
8853
options: WatchQueryOptions<TVariables, TData>,
8954
) {
90-
const wrapped = wrapWithZone(from(fixObservable(this.query)), ngZone);
55+
const wrapped = wrapWithZone(from(fixObservable(this.obsQuery)), ngZone);
9156

9257
this.valueChanges = options.useInitialLoading
93-
? wrapped.pipe(useInitialLoading(this.query))
58+
? wrapped.pipe(useInitialLoading(this.obsQuery))
9459
: wrapped;
95-
this.queryId = this.query.queryId;
96-
97-
// ObservableQuery's methods
98-
this.result = this.query.result.bind(this.query);
99-
this.getCurrentResult = this.query.getCurrentResult.bind(this.query);
100-
this.getLastResult = this.query.getLastResult.bind(this.query);
101-
this.getLastError = this.query.getLastError.bind(this.query);
102-
this.resetLastResults = this.query.resetLastResults.bind(this.query);
103-
this.refetch = this.query.refetch.bind(this.query);
104-
this.fetchMore = this.query.fetchMore.bind(this.query);
105-
this.subscribeToMore = this.query.subscribeToMore.bind(this.query);
106-
this.updateQuery = this.query.updateQuery.bind(this.query);
107-
this.stopPolling = this.query.stopPolling.bind(this.query);
108-
this.startPolling = this.query.startPolling.bind(this.query);
109-
this.setOptions = this.query.setOptions.bind(this.query);
110-
this.setVariables = this.query.setVariables.bind(this.query);
111-
}
112-
113-
public get options() {
114-
return this.query.options;
115-
}
116-
117-
public get variables() {
118-
return this.query.variables;
60+
this.queryId = this.obsQuery.queryId;
61+
}
62+
63+
// ObservableQuery's methods
64+
65+
public get options(): ObservableQuery<TData, TVariables>['options'] {
66+
return this.obsQuery.options;
67+
}
68+
69+
public get variables(): ObservableQuery<TData, TVariables>['variables'] {
70+
return this.obsQuery.variables;
71+
}
72+
73+
public result(): ReturnType<ObservableQuery<TData, TVariables>['result']> {
74+
return this.obsQuery.result();
75+
}
76+
77+
public getCurrentResult(): ReturnType<ObservableQuery<TData, TVariables>['getCurrentResult']> {
78+
return this.obsQuery.getCurrentResult();
79+
}
80+
81+
public getLastResult(): ReturnType<ObservableQuery<TData, TVariables>['getLastResult']> {
82+
return this.obsQuery.getLastResult();
83+
}
84+
85+
public getLastError(): ReturnType<ObservableQuery<TData, TVariables>['getLastError']> {
86+
return this.obsQuery.getLastError();
87+
}
88+
89+
public resetLastResults(): ReturnType<ObservableQuery<TData, TVariables>['resetLastResults']> {
90+
return this.obsQuery.resetLastResults();
91+
}
92+
93+
public refetch(
94+
variables?: Parameters<ObservableQuery<TData, TVariables>['refetch']>[0],
95+
): ReturnType<ObservableQuery<TData, TVariables>['refetch']> {
96+
return this.obsQuery.refetch(variables);
97+
}
98+
99+
public fetchMore<TFetchVars extends OperationVariables = TVariables>(
100+
fetchMoreOptions: Parameters<ObservableQuery<TData, TFetchVars>['fetchMore']>[0],
101+
): ReturnType<ObservableQuery<TData, TFetchVars>['fetchMore']> {
102+
return this.obsQuery.fetchMore(fetchMoreOptions);
103+
}
104+
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+
}
113+
114+
public updateQuery(
115+
mapFn: Parameters<ObservableQuery<TData, TVariables>['updateQuery']>[0],
116+
): ReturnType<ObservableQuery<TData, TVariables>['updateQuery']> {
117+
return this.obsQuery.updateQuery(mapFn);
118+
}
119+
120+
public stopPolling(): ReturnType<ObservableQuery<TData, TVariables>['stopPolling']> {
121+
return this.obsQuery.stopPolling();
122+
}
123+
124+
public startPolling(
125+
pollInterval: Parameters<ObservableQuery<TData, TVariables>['startPolling']>[0],
126+
): ReturnType<ObservableQuery<TData, TVariables>['startPolling']> {
127+
return this.obsQuery.startPolling(pollInterval);
128+
}
129+
130+
public setOptions(
131+
opts: Parameters<ObservableQuery<TData, TVariables>['setOptions']>[0],
132+
): ReturnType<ObservableQuery<TData, TVariables>['setOptions']> {
133+
return this.obsQuery.setOptions(opts);
134+
}
135+
136+
public setVariables(
137+
variables: Parameters<ObservableQuery<TData, TVariables>['setVariables']>[0],
138+
): ReturnType<ObservableQuery<TData, TVariables>['setVariables']> {
139+
return this.obsQuery.setVariables(variables);
119140
}
120141
}

packages/apollo-angular/tests/Apollo.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ describe('Apollo', () => {
8383
`,
8484
};
8585

86-
const spy = jest.spyOn(client, 'watchQuery');
86+
client.watchQuery = jest.fn().mockReturnValue(new Observable());
8787
apollo.watchQuery(options);
8888

89-
expect(spy).toBeCalledWith(options);
89+
expect(client.watchQuery).toBeCalledWith(options);
9090
});
9191

9292
test('should be able to refetch', (done: jest.DoneCallback) => {

packages/apollo-angular/tests/QueryRef.spec.ts

+9-38
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@ const createClient = (link: ApolloLink) =>
1212
cache: new InMemoryCache(),
1313
});
1414

15-
type Result = {
16-
heroes: { name: string }[];
17-
};
18-
19-
type Variables = {
20-
foo?: number;
21-
};
22-
2315
const heroesOperation = {
24-
query: gql<Result, Variables>`
16+
query: gql`
2517
query allHeroes {
2618
heroes {
2719
name
@@ -48,7 +40,8 @@ const Batman = {
4840
describe('QueryRef', () => {
4941
let ngZone: NgZone;
5042
let client: ApolloClient<any>;
51-
let obsQuery: ObservableQuery<Result, Variables>;
43+
let obsQuery: ObservableQuery<any>;
44+
let queryRef: QueryRef<any>;
5245

5346
beforeEach(() => {
5447
ngZone = { run: jest.fn(cb => cb()) } as any;
@@ -65,14 +58,10 @@ describe('QueryRef', () => {
6558

6659
client = createClient(mockedLink);
6760
obsQuery = client.watchQuery(heroesOperation);
61+
queryRef = new QueryRef<any>(obsQuery, ngZone, {} as any);
6862
});
6963

70-
function createQueryRef(obsQuery: ObservableQuery<Result>): QueryRef<Result, Variables> {
71-
return new QueryRef(obsQuery, ngZone, { query: heroesOperation.query });
72-
}
73-
7464
test('should listen to changes', done => {
75-
const queryRef = createQueryRef(obsQuery);
7665
queryRef.valueChanges.subscribe({
7766
next: result => {
7867
expect(result.data).toBeDefined();
@@ -88,7 +77,6 @@ describe('QueryRef', () => {
8877
const mockCallback = jest.fn();
8978
obsQuery.refetch = mockCallback;
9079

91-
const queryRef = createQueryRef(obsQuery);
9280
queryRef.refetch();
9381

9482
expect(mockCallback.mock.calls.length).toBe(1);
@@ -97,7 +85,6 @@ describe('QueryRef', () => {
9785
test('should be able refetch and receive new results', done => {
9886
let calls = 0;
9987

100-
const queryRef = createQueryRef(obsQuery);
10188
queryRef.valueChanges.subscribe({
10289
next: result => {
10390
calls++;
@@ -123,7 +110,6 @@ describe('QueryRef', () => {
123110

124111
test('should be able refetch and receive new results after using rxjs operator', done => {
125112
let calls = 0;
126-
const queryRef = createQueryRef(obsQuery);
127113
const obs = queryRef.valueChanges;
128114

129115
obs.pipe(map(result => result.data)).subscribe({
@@ -153,10 +139,9 @@ describe('QueryRef', () => {
153139

154140
test('should be able to call updateQuery()', () => {
155141
const mockCallback = jest.fn();
156-
const mapFn = () => undefined;
142+
const mapFn = () => ({});
157143
obsQuery.updateQuery = mockCallback;
158144

159-
const queryRef = createQueryRef(obsQuery);
160145
queryRef.updateQuery(mapFn);
161146

162147
expect(mockCallback.mock.calls.length).toBe(1);
@@ -167,7 +152,6 @@ describe('QueryRef', () => {
167152
const mockCallback = jest.fn();
168153
obsQuery.result = mockCallback.mockReturnValue('expected');
169154

170-
const queryRef = createQueryRef(obsQuery);
171155
const result = queryRef.result();
172156

173157
expect(result).toBe('expected');
@@ -176,7 +160,6 @@ describe('QueryRef', () => {
176160

177161
test('should be able to call getCurrentResult() and get updated results', done => {
178162
let calls = 0;
179-
const queryRef = createQueryRef(obsQuery);
180163
const obs = queryRef.valueChanges;
181164

182165
obs.pipe(map(result => result.data)).subscribe({
@@ -206,7 +189,6 @@ describe('QueryRef', () => {
206189
const mockCallback = jest.fn();
207190
obsQuery.getLastResult = mockCallback.mockReturnValue('expected');
208191

209-
const queryRef = createQueryRef(obsQuery);
210192
const result = queryRef.getLastResult();
211193

212194
expect(result).toBe('expected');
@@ -217,7 +199,6 @@ describe('QueryRef', () => {
217199
const mockCallback = jest.fn();
218200
obsQuery.getLastError = mockCallback.mockReturnValue('expected');
219201

220-
const queryRef = createQueryRef(obsQuery);
221202
const result = queryRef.getLastError();
222203

223204
expect(result).toBe('expected');
@@ -228,7 +209,6 @@ describe('QueryRef', () => {
228209
const mockCallback = jest.fn();
229210
obsQuery.resetLastResults = mockCallback.mockReturnValue('expected');
230211

231-
const queryRef = createQueryRef(obsQuery);
232212
const result = queryRef.resetLastResults();
233213

234214
expect(result).toBe('expected');
@@ -237,11 +217,10 @@ describe('QueryRef', () => {
237217

238218
test('should be able to call fetchMore()', () => {
239219
const mockCallback = jest.fn();
240-
const opts = { variables: { foo: 1 } };
220+
const opts = { foo: 1 };
241221
obsQuery.fetchMore = mockCallback.mockReturnValue('expected');
242222

243-
const queryRef = createQueryRef(obsQuery);
244-
const result = queryRef.fetchMore(opts);
223+
const result = queryRef.fetchMore(opts as any);
245224

246225
expect(result).toBe('expected');
247226
expect(mockCallback.mock.calls.length).toBe(1);
@@ -250,11 +229,10 @@ describe('QueryRef', () => {
250229

251230
test('should be able to call subscribeToMore()', () => {
252231
const mockCallback = jest.fn();
253-
const opts = { document: heroesOperation.query };
232+
const opts = { foo: 1 };
254233
obsQuery.subscribeToMore = mockCallback;
255234

256-
const queryRef = createQueryRef(obsQuery);
257-
queryRef.subscribeToMore(opts);
235+
queryRef.subscribeToMore(opts as any);
258236

259237
expect(mockCallback.mock.calls.length).toBe(1);
260238
expect(mockCallback.mock.calls[0][0]).toBe(opts);
@@ -264,7 +242,6 @@ describe('QueryRef', () => {
264242
const mockCallback = jest.fn();
265243
obsQuery.stopPolling = mockCallback;
266244

267-
const queryRef = createQueryRef(obsQuery);
268245
queryRef.stopPolling();
269246

270247
expect(mockCallback.mock.calls.length).toBe(1);
@@ -274,7 +251,6 @@ describe('QueryRef', () => {
274251
const mockCallback = jest.fn();
275252
obsQuery.startPolling = mockCallback;
276253

277-
const queryRef = createQueryRef(obsQuery);
278254
queryRef.startPolling(3000);
279255

280256
expect(mockCallback.mock.calls.length).toBe(1);
@@ -286,7 +262,6 @@ describe('QueryRef', () => {
286262
const opts = {};
287263
obsQuery.setOptions = mockCallback.mockReturnValue('expected');
288264

289-
const queryRef = createQueryRef(obsQuery);
290265
const result = queryRef.setOptions(opts);
291266

292267
expect(result).toBe('expected');
@@ -299,7 +274,6 @@ describe('QueryRef', () => {
299274
const variables = {};
300275
obsQuery.setVariables = mockCallback.mockReturnValue('expected');
301276

302-
const queryRef = createQueryRef(obsQuery);
303277
const result = queryRef.setVariables(variables);
304278

305279
expect(result).toBe('expected');
@@ -308,7 +282,6 @@ describe('QueryRef', () => {
308282
});
309283

310284
test('should handle multiple subscribers', done => {
311-
const queryRef = createQueryRef(obsQuery);
312285
const obsFirst = queryRef.valueChanges;
313286
const obsSecond = queryRef.valueChanges;
314287

@@ -365,7 +338,6 @@ describe('QueryRef', () => {
365338
});
366339

367340
test('should unsubscribe', done => {
368-
const queryRef = createQueryRef(obsQuery);
369341
const obs = queryRef.valueChanges;
370342
const id = queryRef.queryId;
371343

@@ -384,7 +356,6 @@ describe('QueryRef', () => {
384356

385357
test('should unsubscribe based on rxjs operators', done => {
386358
const gate = new Subject<void>();
387-
const queryRef = createQueryRef(obsQuery);
388359
const obs = queryRef.valueChanges.pipe(takeUntil(gate));
389360
const id = queryRef.queryId;
390361

0 commit comments

Comments
 (0)