Skip to content

Commit

Permalink
Let typing flow better
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi committed Oct 23, 2024
1 parent 007ee40 commit a564953
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-walls-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'apollo-angular': patch
---

Let typing flow better
4 changes: 2 additions & 2 deletions packages/apollo-angular/headers/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('httpHeaders', () => {
Authorization: 'Bearer Foo',
},
},
}).subscribe((result: any) => {
}).subscribe(result => {
expect(result.data).toEqual(data);
done();
});
Expand All @@ -55,7 +55,7 @@ describe('httpHeaders', () => {

execute(link, {
query,
}).subscribe((result: any) => {
}).subscribe(result => {
expect(result.data).toEqual(data);
done();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-angular/http/tests/http-batch-link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('HttpBatchLink', () => {
};

execute(link, op).subscribe({
next: (result: any) => {
next: result => {
expect(result).toEqual({ data });
done();
},
Expand Down
12 changes: 6 additions & 6 deletions packages/apollo-angular/http/tests/http-link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('HttpLink', () => {
};

execute(link, op).subscribe({
next: (result: any) => expect(result).toEqual({ data }),
next: result => expect(result).toEqual({ data }),
error: () => {
throw new Error('Should not be here');
},
Expand All @@ -73,7 +73,7 @@ describe('HttpLink', () => {
};

execute(link, op).subscribe({
next: (result: any) => expect(result).toEqual({ data }),
next: result => expect(result).toEqual({ data }),
error: () => {
throw new Error('Should not be here');
},
Expand All @@ -100,7 +100,7 @@ describe('HttpLink', () => {
};

execute(link, op).subscribe({
next: (result: any) => expect(result).toEqual({ data }),
next: result => expect(result).toEqual({ data }),
error: () => {
throw new Error('Should not be here');
},
Expand Down Expand Up @@ -511,7 +511,7 @@ describe('HttpLink', () => {

test('should set response in context', (done: jest.DoneCallback) => {
const afterware = new ApolloLink((op, forward) => {
return forward(op).map((response: any) => {
return forward(op).map(response => {
const context = op.getContext();

expect(context.response).toBeDefined();
Expand Down Expand Up @@ -596,11 +596,11 @@ describe('HttpLink', () => {
return m2;
}),
).subscribe({
next(result: any) {
next(result) {
expect(result.data).toMatchObject(data2);
done();
},
error(error: any) {
error(error) {
done.fail(error);
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-angular/src/query-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class QueryRef<T, V extends OperationVariables = EmptyObject> {
return this.obsQuery.startPolling(pollInterval);
}

public setOptions(opts: any) {
public setOptions(opts: Partial<WatchQueryOptions<V, T>>) {
return this.obsQuery.setOptions(opts);
}

Expand Down
3 changes: 1 addition & 2 deletions packages/apollo-angular/testing/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export class ApolloTestingBackend implements ApolloTestingController {
}

private compare(expected?: string, value?: Object | string): boolean {
const prepare = (val: any) => (typeof val === 'string' ? val : JSON.stringify(val));
const received = prepare(value);
const received = typeof value === 'string' ? value : JSON.stringify(value);

return !expected || received === expected;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-angular/testing/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ApolloTestingModuleCore {
cache?: ApolloCache<any>,
@Optional()
@Inject(APOLLO_TESTING_NAMED_CACHE)
namedCaches?: any, // FIX: using NamedCaches here makes ngc fail
namedCaches?: NamedCaches,
) {
function createOptions(name: string, c?: ApolloCache<any> | null) {
return {
Expand Down
10 changes: 6 additions & 4 deletions packages/apollo-angular/testing/src/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { ExecutionResult, GraphQLError } from 'graphql';
import { Observer } from 'rxjs';
import { ApolloError, FetchResult, Operation as LinkOperation } from '@apollo/client/core';

const isApolloError = (err: any): err is ApolloError => err && err.hasOwnProperty('graphQLErrors');
function isApolloError(error: unknown): error is ApolloError {
return !!error && error.hasOwnProperty('graphQLErrors');
}

export type Operation = LinkOperation & {
clientName: string;
Expand All @@ -14,17 +16,17 @@ export class TestOperation<T = { [key: string]: any }> {
private readonly observer: Observer<FetchResult<T>>,
) {}

public flush(result: ExecutionResult | ApolloError): void {
public flush(result: ExecutionResult<T> | ApolloError): void {
if (isApolloError(result)) {
this.observer.error(result);
} else {
const fetchResult = result ? { ...result } : result;
this.observer.next(fetchResult as any);
this.observer.next(fetchResult);
this.observer.complete();
}
}

public flushData(data: { [key: string]: any } | null): void {
public flushData(data: T | null): void {
this.flush({
data,
});
Expand Down
18 changes: 9 additions & 9 deletions packages/apollo-angular/testing/tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ describe('Integration', () => {

// query
apollo.query<any>(op).subscribe({
next: (result: any) => {
next: result => {
expect(result.data).toMatchObject(data);
done();
},
error: (e: any) => {
error: e => {
done.fail(e);
},
});
Expand Down Expand Up @@ -75,11 +75,11 @@ describe('Integration', () => {

// query
apollo.query<any>(op).subscribe({
next: (result: any) => {
next: result => {
expect(result.data).toMatchObject(data);
done();
},
error: (e: any) => {
error: e => {
done.fail(e);
},
});
Expand Down Expand Up @@ -108,11 +108,11 @@ describe('Integration', () => {

// query
apollo.query<any>(op).subscribe({
next: (result: any) => {
next: result => {
expect(result.data).toMatchObject(data);
done();
},
error: (e: any) => {
error: e => {
done.fail(e);
},
});
Expand Down Expand Up @@ -144,11 +144,11 @@ describe('Integration', () => {

// query
apollo.query<any>(op).subscribe({
next: (result: any) => {
next: result => {
expect(result.data).toMatchObject(data);
done();
},
error: (e: any) => {
error: e => {
done.fail(e);
},
});
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('Integration', () => {
};

apollo.query<any>(op).subscribe({
next: (result: any) => {
next: result => {
expect(result.data).toMatchObject(data);
done();
},
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-angular/testing/tests/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ describe('ApolloTestingModule', () => {
};

apollo
.query({
.query<any>({
query: testQuery,
})
.subscribe((result: any) => {
.subscribe(result => {
expect(result.data.heroes[0].name).toBe('Spiderman');
done();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-angular/testing/tests/operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('TestOperation', () => {
test('accepts a null body', done => {
const operation = buildOperationForLink(testQuery, {});

execute(link, operation as any).subscribe((result: any) => {
execute(link, operation).subscribe(result => {
expect(result).toBeNull();
done();
});
Expand All @@ -38,7 +38,7 @@ describe('TestOperation', () => {
test('should accepts data for flush operation', done => {
const operation = buildOperationForLink(testQuery, {});

execute(link, operation as any).subscribe((result: any) => {
execute(link, operation).subscribe(result => {
expect(result).toEqual({
data: {
heroes: [],
Expand Down
8 changes: 6 additions & 2 deletions packages/apollo-angular/testing/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { DocumentNode } from 'graphql';
import type { GraphQLRequest } from '@apollo/client/link/core/types';
import { getOperationName } from '@apollo/client/utilities';

export const buildOperationForLink = (document: DocumentNode, variables: any) => {
export function buildOperationForLink<TVariables = Record<string, any>>(
document: DocumentNode,
variables: TVariables,
): GraphQLRequest<TVariables> {
return {
query: document,
variables,
operationName: getOperationName(document) || undefined,
context: {},
};
};
}
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 @@ -124,7 +124,7 @@ describe('Apollo', () => {
let calls = 0;

obs.valueChanges.subscribe({
next: ({ data }: any) => {
next: ({ data }) => {
calls++;

try {
Expand Down Expand Up @@ -766,7 +766,7 @@ describe('Apollo', () => {

const FooHero = { id: 1, name: 'Foo', __typename };
const BarHero = { id: 2, name: 'Bar', __typename };
const OptimisticHero: any = { id: null, name: 'Temp', __typename };
const OptimisticHero = { id: null, name: 'Temp', __typename };

const data1 = { allHeroes: [FooHero] };
const dataMutation = { addHero: BarHero };
Expand Down
14 changes: 7 additions & 7 deletions packages/apollo-angular/tests/QueryRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ describe('QueryRef', () => {
let calls = 0;
const obs = queryRef.valueChanges;

obs.pipe(map((result: any) => result.data)).subscribe({
next: (result: any) => {
obs.pipe(map(result => result.data)).subscribe({
next: result => {
calls++;

if (calls === 1) {
Expand All @@ -124,7 +124,7 @@ describe('QueryRef', () => {
done();
}
},
error: (e: any) => {
error: e => {
done.fail(e);
},
complete: () => {
Expand All @@ -139,7 +139,7 @@ describe('QueryRef', () => {

test('should be able to call updateQuery()', () => {
const mockCallback = jest.fn();
const mapFn: any = () => ({});
const mapFn = () => ({});
obsQuery.updateQuery = mockCallback;

queryRef.updateQuery(mapFn);
Expand All @@ -162,8 +162,8 @@ describe('QueryRef', () => {
let calls = 0;
const obs = queryRef.valueChanges;

obs.pipe(map((result: any) => result.data)).subscribe({
next: (result: any) => {
obs.pipe(map(result => result.data)).subscribe({
next: result => {
calls++;
const currentResult = queryRef.getCurrentResult();
expect(currentResult.data.heroes.length).toBe(result.heroes.length);
Expand All @@ -172,7 +172,7 @@ describe('QueryRef', () => {
done();
}
},
error: (e: any) => {
error: e => {
done.fail(e);
},
complete: () => {
Expand Down

0 comments on commit a564953

Please sign in to comment.