Skip to content

Commit

Permalink
feat(core): more strict (#885)
Browse files Browse the repository at this point in the history
* feat(core): more strict

* fix(core): subscribeToMore should not inherit types from ObservableQuery

There's a bug in apollo-client typings

* fix(core): use proper defaults in subscribeToMore to not break apps

* docs(core): changelog
  • Loading branch information
kamilkisiela authored Oct 11, 2018
1 parent 987aa3f commit 1d526d9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 32 deletions.
2 changes: 2 additions & 0 deletions packages/apollo-angular/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### vNEXT

- Use more generic types and make everything more strict [PR #885](https://github.com/apollographql/apollo-angular/pull/885)

### v1.4.1

- schematics: bump dependencies
Expand Down
23 changes: 11 additions & 12 deletions packages/apollo-angular/src/Apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
ApolloQueryResult,
SubscriptionOptions,
ApolloClientOptions,
ObservableQuery,
} from 'apollo-client';
import {FetchResult} from 'apollo-link';
import {Observable, from} from 'rxjs';

import {QueryRef} from './QueryRef';
import {TypedVariables, ExtraSubscriptionOptions, R} from './types';
import {ExtraSubscriptionOptions, R} from './types';
import {APOLLO_OPTIONS} from './tokens';
import {fromPromise, wrapWithZone, fixObservable} from './utils';

Expand All @@ -22,36 +23,34 @@ export class ApolloBase<TCacheShape = any> {
private _client?: ApolloClient<TCacheShape>,
) {}

public watchQuery<T, V = R>(
options: WatchQueryOptions & TypedVariables<V>,
): QueryRef<T, V> {
public watchQuery<T, V = R>(options: WatchQueryOptions<V>): QueryRef<T, V> {
return new QueryRef<T, V>(
this.client.watchQuery<T>({...options}),
this.client.watchQuery<T, V>({...options}) as ObservableQuery<T, V>,
this.ngZone,
);
}

public query<T, V = R>(
options: QueryOptions & TypedVariables<V>,
options: QueryOptions<V>,
): Observable<ApolloQueryResult<T>> {
return fromPromise<ApolloQueryResult<T>>(() =>
this.client.query<T>({...options}),
this.client.query<T, V>({...options}),
);
}

public mutate<T, V = R>(
options: MutationOptions & TypedVariables<V>,
options: MutationOptions<T, V>,
): Observable<FetchResult<T>> {
return fromPromise<FetchResult<T>>(() =>
this.client.mutate<T>({...options}),
this.client.mutate<T, V>({...options}),
);
}

public subscribe(
options: SubscriptionOptions,
public subscribe<T, V = R>(
options: SubscriptionOptions<V>,
extra?: ExtraSubscriptionOptions,
): Observable<any> {
const obs = from(fixObservable(this.client.subscribe({...options})));
const obs = from(fixObservable(this.client.subscribe<T, V>({...options})));

return extra && extra.useZone !== true
? obs
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-angular/src/Mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Mutation<T = {}, V = R> {

public mutate(
variables?: V,
options?: MutationOptions,
options?: MutationOptions<T, V>,
): Observable<FetchResult<T>> {
return this.apollo.use(this.client).mutate<T, V>({
...options,
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-angular/src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Query<T = {}, V = R> {

constructor(protected apollo: Apollo) {}

public watch(variables?: V, options?: WatchQueryOptions): QueryRef<T, V> {
public watch(variables?: V, options?: WatchQueryOptions<V>): QueryRef<T, V> {
return this.apollo.use(this.client).watchQuery<T, V>({
...options,
variables,
Expand All @@ -24,7 +24,7 @@ export class Query<T = {}, V = R> {

public fetch(
variables?: V,
options?: QueryOptions,
options?: QueryOptions<V>,
): Observable<ApolloQueryResult<T>> {
return this.apollo.use(this.client).query<T, V>({
...options,
Expand Down
10 changes: 7 additions & 3 deletions packages/apollo-angular/src/QueryRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class QueryRef<T, V = R> {
public valueChanges: Observable<ApolloQueryResult<T>>;
public queryId: string;

constructor(private obsQuery: ObservableQuery<T>, ngZone: NgZone) {
constructor(private obsQuery: ObservableQuery<T, V>, ngZone: NgZone) {
this.valueChanges = wrapWithZone(
from(fixObservable(this.obsQuery)),
ngZone,
Expand Down Expand Up @@ -58,8 +58,12 @@ export class QueryRef<T, V = R> {
return this.obsQuery.fetchMore(fetchMoreOptions);
}

public subscribeToMore(options: SubscribeToMoreOptions): () => void {
return this.obsQuery.subscribeToMore(options);
public subscribeToMore<MT = any, MV = R>(
options: SubscribeToMoreOptions<MT, MV>,
): () => void {
// XXX: there's a bug in apollo-client typings
// it should not inherit types from ObservableQuery
return this.obsQuery.subscribeToMore(options as any);
}
public updateQuery(
mapFn: (previousQueryResult: T, options: UpdateQueryOptions<V>) => T,
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-angular/src/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export class Subscription<T = any, V = R> {

public subscribe(
variables?: V,
options?: SubscriptionOptions,
options?: SubscriptionOptions<V>,
extra?: ExtraSubscriptionOptions,
): Observable<SubscriptionResult<T>> {
return this.apollo.use(this.client).subscribe(
return this.apollo.use(this.client).subscribe<T, V>(
{
...options,
variables,
Expand Down
22 changes: 10 additions & 12 deletions packages/apollo-angular/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
} from 'apollo-client';
import {ExecutionResult} from 'graphql';

export type R = Record<string, any>;

export type TypedVariables<T> = {
variables?: T;
export type R = {
[key: string]: any;
};

export interface ExtraSubscriptionOptions {
Expand All @@ -18,17 +16,17 @@ export interface ExtraSubscriptionOptions {

export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export interface WatchQueryOptions
extends Omit<CoreWatchQueryOptions, 'query' | 'variables'> {}
export interface WatchQueryOptions<V>
extends Omit<CoreWatchQueryOptions<V>, 'query' | 'variables'> {}

export interface QueryOptions
extends Omit<CoreQueryOptions, 'query' | 'variables'> {}
export interface QueryOptions<V>
extends Omit<CoreQueryOptions<V>, 'query' | 'variables'> {}

export interface MutationOptions
extends Omit<CoreMutationOptions, 'mutation' | 'variables'> {}
export interface MutationOptions<T, V>
extends Omit<CoreMutationOptions<T, V>, 'mutation' | 'variables'> {}

export interface SubscriptionOptions
extends Omit<CoreSubscriptionOptions, 'query' | 'variables'> {}
export interface SubscriptionOptions<V>
extends Omit<CoreSubscriptionOptions<V>, 'query' | 'variables'> {}

export interface SubscriptionResult<T> extends ExecutionResult {
data?: T;
Expand Down

0 comments on commit 1d526d9

Please sign in to comment.