Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rare-ways-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'apollo-angular': patch
---

`fetchMore` typing is duplicated from `@apollo/client`
5 changes: 0 additions & 5 deletions .changeset/thin-otters-move.md

This file was deleted.

162 changes: 97 additions & 65 deletions packages/apollo-angular/src/query-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { from, Observable } from 'rxjs';
import { NgZone } from '@angular/core';
import type {
ApolloQueryResult,
FetchMoreQueryOptions,
MaybeMasked,
ObservableQuery,
OperationVariables,
SubscribeToMoreOptions,
TypedDocumentNode,
Unmasked,
} from '@apollo/client/core';
import { NetworkStatus } from '@apollo/client/core';
import { EmptyObject, WatchQueryOptions } from './types';
Expand Down Expand Up @@ -42,79 +46,107 @@ function useInitialLoading<T, V extends OperationVariables>(obsQuery: Observable
export type QueryRefFromDocument<T extends TypedDocumentNode> =
T extends TypedDocumentNode<infer R, infer V> ? QueryRef<R, V & OperationVariables> : never;

export class QueryRef<TData, TVariables extends OperationVariables = EmptyObject>
implements
Pick<
ObservableQuery<TData, TVariables>,
| 'queryId'
| 'options'
| 'variables'
| 'result'
| 'getCurrentResult'
| 'getLastResult'
| 'getLastError'
| 'resetLastResults'
| 'refetch'
| 'fetchMore'
| 'subscribeToMore'
| 'updateQuery'
| 'stopPolling'
| 'startPolling'
| 'setOptions'
| 'setVariables'
>
{
export class QueryRef<TData, TVariables extends OperationVariables = EmptyObject> {
public readonly valueChanges: Observable<ApolloQueryResult<TData>>;

// Types flow straight from ObservableQuery
public readonly queryId;
public readonly result;
public readonly getCurrentResult;
public readonly getLastResult;
public readonly getLastError;
public readonly resetLastResults;
public readonly refetch;
public readonly fetchMore;
public readonly subscribeToMore;
public readonly updateQuery;
public readonly stopPolling;
public readonly startPolling;
public readonly setOptions;
public readonly setVariables;
public readonly queryId: ObservableQuery<TData, TVariables>['queryId'];

constructor(
private readonly query: ObservableQuery<TData, TVariables>,
private readonly obsQuery: ObservableQuery<TData, TVariables>,
ngZone: NgZone,
options: WatchQueryOptions<TVariables, TData>,
) {
const wrapped = wrapWithZone(from(fixObservable(this.query)), ngZone);
const wrapped = wrapWithZone(from(fixObservable(this.obsQuery)), ngZone);

this.valueChanges = options.useInitialLoading
? wrapped.pipe(useInitialLoading(this.query))
? wrapped.pipe(useInitialLoading(this.obsQuery))
: wrapped;
this.queryId = this.query.queryId;

// ObservableQuery's methods
this.result = this.query.result.bind(this.query);
this.getCurrentResult = this.query.getCurrentResult.bind(this.query);
this.getLastResult = this.query.getLastResult.bind(this.query);
this.getLastError = this.query.getLastError.bind(this.query);
this.resetLastResults = this.query.resetLastResults.bind(this.query);
this.refetch = this.query.refetch.bind(this.query);
this.fetchMore = this.query.fetchMore.bind(this.query);
this.subscribeToMore = this.query.subscribeToMore.bind(this.query);
this.updateQuery = this.query.updateQuery.bind(this.query);
this.stopPolling = this.query.stopPolling.bind(this.query);
this.startPolling = this.query.startPolling.bind(this.query);
this.setOptions = this.query.setOptions.bind(this.query);
this.setVariables = this.query.setVariables.bind(this.query);
}

public get options() {
return this.query.options;
}

public get variables() {
return this.query.variables;
this.queryId = this.obsQuery.queryId;
}

// ObservableQuery's methods

public get options(): ObservableQuery<TData, TVariables>['options'] {
return this.obsQuery.options;
}

public get variables(): ObservableQuery<TData, TVariables>['variables'] {
return this.obsQuery.variables;
}

public result(): ReturnType<ObservableQuery<TData, TVariables>['result']> {
return this.obsQuery.result();
}

public getCurrentResult(): ReturnType<ObservableQuery<TData, TVariables>['getCurrentResult']> {
return this.obsQuery.getCurrentResult();
}

public getLastResult(): ReturnType<ObservableQuery<TData, TVariables>['getLastResult']> {
return this.obsQuery.getLastResult();
}

public getLastError(): ReturnType<ObservableQuery<TData, TVariables>['getLastError']> {
return this.obsQuery.getLastError();
}

public resetLastResults(): ReturnType<ObservableQuery<TData, TVariables>['resetLastResults']> {
return this.obsQuery.resetLastResults();
}

public refetch(
variables?: Parameters<ObservableQuery<TData, TVariables>['refetch']>[0],
): ReturnType<ObservableQuery<TData, TVariables>['refetch']> {
return this.obsQuery.refetch(variables);
}

public fetchMore<TFetchData = TData, TFetchVars extends OperationVariables = TVariables>(
fetchMoreOptions: FetchMoreQueryOptions<TFetchVars, TFetchData> & {
updateQuery?: (
previousQueryResult: Unmasked<TData>,
options: {
fetchMoreResult: Unmasked<TFetchData>;
variables: TFetchVars;
},
) => Unmasked<TData>;
},
): Promise<ApolloQueryResult<MaybeMasked<TFetchData>>> {
return this.obsQuery.fetchMore(fetchMoreOptions);
}

public subscribeToMore<
TSubscriptionData = TData,
TSubscriptionVariables extends OperationVariables = TVariables,
>(
options: SubscribeToMoreOptions<TData, TSubscriptionVariables, TSubscriptionData, TVariables>,
): ReturnType<ObservableQuery<TData, TVariables>['subscribeToMore']> {
return this.obsQuery.subscribeToMore(options);
}

public updateQuery(
mapFn: Parameters<ObservableQuery<TData, TVariables>['updateQuery']>[0],
): ReturnType<ObservableQuery<TData, TVariables>['updateQuery']> {
return this.obsQuery.updateQuery(mapFn);
}

public stopPolling(): ReturnType<ObservableQuery<TData, TVariables>['stopPolling']> {
return this.obsQuery.stopPolling();
}

public startPolling(
pollInterval: Parameters<ObservableQuery<TData, TVariables>['startPolling']>[0],
): ReturnType<ObservableQuery<TData, TVariables>['startPolling']> {
return this.obsQuery.startPolling(pollInterval);
}

public setOptions(
opts: Parameters<ObservableQuery<TData, TVariables>['setOptions']>[0],
): ReturnType<ObservableQuery<TData, TVariables>['setOptions']> {
return this.obsQuery.setOptions(opts);
}

public setVariables(
variables: Parameters<ObservableQuery<TData, TVariables>['setVariables']>[0],
): ReturnType<ObservableQuery<TData, TVariables>['setVariables']> {
return this.obsQuery.setVariables(variables);
}
}
4 changes: 2 additions & 2 deletions packages/apollo-angular/tests/Apollo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ describe('Apollo', () => {
`,
};

const spy = jest.spyOn(client, 'watchQuery');
client.watchQuery = jest.fn().mockReturnValue(new Observable());
apollo.watchQuery(options);

expect(spy).toBeCalledWith(options);
expect(client.watchQuery).toBeCalledWith(options);
});

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