Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable strict mode in tsconfig and fix type errors #11200

Merged
merged 33 commits into from Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
aca904f
Enable strict mode in tsconfig.json
jerelmiller Sep 7, 2023
0da3d3d
Fix type issues in utilities/graphql/transform
jerelmiller Sep 7, 2023
78ccf61
Use spread to push incoming objects in offsetLimitPagination
jerelmiller Sep 7, 2023
69d528a
Fix type issues in writeToStore test
jerelmiller Sep 7, 2023
e503cbb
Fix type issues in Concast
jerelmiller Sep 7, 2023
0fe49a0
Remove unused filterInPlace utility
jerelmiller Sep 7, 2023
e34d084
Add this type to itAsync
jerelmiller Sep 7, 2023
962bd47
Add ! annotation to mockLink
jerelmiller Sep 7, 2023
36129f8
Fix assignment from unknown in error link
jerelmiller Sep 7, 2023
cea054e
Fix type issue in readFromStore
jerelmiller Sep 7, 2023
bed3439
Fix type issues in useQuery
jerelmiller Sep 7, 2023
b568689
some progress
phryneas Sep 8, 2023
6498326
more type fixes
phryneas Sep 13, 2023
b8c6eb6
change `addExportedVariables` signature
phryneas Sep 13, 2023
c3a761d
better typing
phryneas Sep 13, 2023
9eb5e3e
undo deprecation
phryneas Sep 13, 2023
0966cc9
Merge branch 'main' into enable-strict-mode
phryneas Sep 13, 2023
a101977
test types
phryneas Sep 13, 2023
55a6351
add api extractor
phryneas Sep 14, 2023
f909921
add api reports
phryneas Sep 14, 2023
e1e2fbb
add workflow
phryneas Sep 14, 2023
0426c84
formatting
phryneas Sep 14, 2023
be151b0
package lock
phryneas Sep 14, 2023
c83b042
remove `ensureResult` for this PR, add `TODO` type
phryneas Sep 14, 2023
4aa485b
Merge branch 'pr/api-extractor' into enable-strict-mode
phryneas Sep 14, 2023
1e7c5c6
API updates for this PR
phryneas Sep 14, 2023
c080309
Merge remote-tracking branch 'origin/main' into enable-strict-mode
phryneas Nov 10, 2023
0b2cf25
size-limit
phryneas Nov 10, 2023
9303630
introduce IDE-only `tsconfig.json`
phryneas Nov 10, 2023
5e3fb70
formatting
phryneas Nov 10, 2023
04dcc0d
fix path
phryneas Nov 10, 2023
2cb5c83
Merge pull request #11359 from apollographql/pr/enable-test-ts
jerelmiller Nov 10, 2023
94b07f9
Add changeset
jerelmiller Nov 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/__tests__/ApolloClient.ts
Expand Up @@ -4,7 +4,6 @@ import {
ApolloClient,
ApolloError,
DefaultOptions,
FetchPolicy,
QueryOptions,
makeReference,
} from "../core";
Expand Down Expand Up @@ -2131,8 +2130,8 @@ describe("ApolloClient", () => {
}
`;

["network-only", "cache-and-network"].forEach(
(fetchPolicy: FetchPolicy) => {
(["network-only", "cache-and-network"] as const).forEach(
(fetchPolicy) => {
const observable = client.watchQuery({
query,
fetchPolicy,
Expand Down Expand Up @@ -2161,13 +2160,15 @@ describe("ApolloClient", () => {
}
`;

[
"cache-first",
"cache-and-network",
"network-only",
"cache-only",
"no-cache",
].forEach((fetchPolicy: FetchPolicy) => {
(
[
"cache-first",
"cache-and-network",
"network-only",
"cache-only",
"no-cache",
] as const
).forEach((fetchPolicy) => {
const observable = client.watchQuery({
query,
fetchPolicy,
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/client.ts
Expand Up @@ -1821,7 +1821,7 @@ describe("client", () => {
link,

cache: new InMemoryCache({
dataIdFromObject: (obj: { id: any }) => obj.id,
dataIdFromObject: (obj: any) => obj.id,
addTypename: false,
}),
});
Expand Down Expand Up @@ -1870,7 +1870,7 @@ describe("client", () => {
callback();
throw new Error("not reached");
} catch (thrown) {
expect(thrown.message).toBe(cacheAndNetworkError);
expect((thrown as Error).message).toBe(cacheAndNetworkError);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/graphqlSubscriptions.ts
Expand Up @@ -495,7 +495,7 @@ describe("GraphQL Subscriptions", () => {

client.subscribe(options).subscribe({
next() {
expect(link.operation.getContext().someVar).toEqual(
expect(link.operation?.getContext().someVar).toEqual(
options.context.someVar
);
resolve();
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/local-state/general.ts
Expand Up @@ -1208,7 +1208,7 @@ describe("Combining client and server state/operations", () => {
watchCount += 1;
client.mutate({
mutation,
update(proxy, { data: { updateUser } }: { data: any }) {
update(proxy, { data: { updateUser } }) {
proxy.writeQuery({
query: userQuery,
data: {
Expand Down
9 changes: 6 additions & 3 deletions src/__tests__/local-state/resolvers.ts
Expand Up @@ -11,7 +11,7 @@ import {
WatchQueryOptions,
} from "../../core";

import { InMemoryCache } from "../../cache";
import { InMemoryCache, isReference } from "../../cache";
import { Observable, Observer } from "../../utilities";
import { ApolloLink } from "../../link/core";
import { itAsync } from "../../testing";
Expand Down Expand Up @@ -747,10 +747,13 @@ describe("Writing cache data from resolvers", () => {
},
},
});
cache.modify({
cache.modify<{ field: { field2: number } }>({
id: "Object:uniqueId",
fields: {
field(value: { field2: number }) {
field(value) {
if (isReference(value)) {
fail("Should not be a reference");
}
expect(value.field2).toBe(1);
return { ...value, field2: 2 };
},
Expand Down
9 changes: 5 additions & 4 deletions src/__tests__/optimistic.ts
Expand Up @@ -235,7 +235,7 @@ describe("optimistic mutation results", () => {
await promise;
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe("forbidden (test error)");
expect((err as Error).message).toBe("forbidden (test error)");

const dataInStore = (client.cache as InMemoryCache).extract(true);
expect((dataInStore["TodoList5"] as any).todos.length).toBe(3);
Expand Down Expand Up @@ -489,7 +489,7 @@ describe("optimistic mutation results", () => {
await promise;
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe("forbidden (test error)");
expect((err as Error).message).toBe("forbidden (test error)");

const dataInStore = (client.cache as InMemoryCache).extract(true);
expect((dataInStore["TodoList5"] as any).todos.length).toBe(3);
Expand Down Expand Up @@ -2019,11 +2019,12 @@ describe("optimistic mutation results", () => {
const wrapReject = <TArgs extends any[], TResult>(
fn: (...args: TArgs) => TResult
): typeof fn => {
return function () {
return function (this: unknown, ...args: TArgs) {
try {
return fn.apply(this, arguments);
return fn.apply(this, args);
} catch (e) {
reject(e);
throw e;
}
};
};
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/subscribeToMore.ts
Expand Up @@ -8,8 +8,10 @@ import { itAsync, mockSingleLink, mockObservableLink } from "../testing";

const isSub = (operation: Operation) =>
(operation.query as DocumentNode).definitions
.filter((x) => x.kind === "OperationDefinition")
.some((x: OperationDefinitionNode) => x.operation === "subscription");
.filter(
(x): x is OperationDefinitionNode => x.kind === "OperationDefinition"
)
.some((x) => x.operation === "subscription");

describe("subscribeToMore", () => {
const query = gql`
Expand Down
2 changes: 1 addition & 1 deletion src/cache/core/types/Cache.ts
Expand Up @@ -93,7 +93,7 @@ export namespace Cache {
this: TCache,
watch: Cache.WatchOptions,
diff: Cache.DiffResult<any>,
lastDiff: Cache.DiffResult<any> | undefined
lastDiff?: Cache.DiffResult<any> | undefined
) => any;
}

Expand Down
1 change: 1 addition & 0 deletions src/cache/inmemory/__tests__/cache.ts
Expand Up @@ -3815,6 +3815,7 @@ describe("ReactiveVar and makeVar", () => {
let broadcastCount = 0;
cache["broadcastWatches"] = function () {
++broadcastCount;
// @ts-expect-error
return broadcast.apply(this, arguments);
};

Expand Down
8 changes: 4 additions & 4 deletions src/cache/inmemory/__tests__/diffAgainstStore.ts
Expand Up @@ -493,8 +493,8 @@ describe("diffing queries against the store", () => {
};

const cache = new InMemoryCache({
dataIdFromObject({ id }: { id: string }) {
return id;
dataIdFromObject(obj: any) {
return obj.id;
},
});

Expand Down Expand Up @@ -841,7 +841,7 @@ describe("diffing queries against the store", () => {

const writer = new StoreWriter(
new InMemoryCache({
dataIdFromObject: ({ id }: { id: string }) => id,
dataIdFromObject: (obj: any) => obj.id,
})
);

Expand Down Expand Up @@ -1067,7 +1067,7 @@ describe("diffing queries against the store", () => {
});
throw new Error("should have thrown");
} catch (e) {
expect(e.message).toEqual(
expect((e as Error).message).toEqual(
"Missing selection set for object of type Message returned for query field messageList"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/__tests__/readFromStore.ts
Expand Up @@ -1904,7 +1904,7 @@ describe("reading from the store", () => {
);
expect(value.__ref).toBe('Deity:{"name":"Zeus"}');
// Interim ruler Apollo takes over for real.
return toReference(apolloRulerResult.ruler);
return toReference(apolloRulerResult.ruler)!;
},
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/__tests__/roundtrip.ts
Expand Up @@ -61,7 +61,7 @@ function storeRoundtrip(query: DocumentNode, result: any, variables = {}) {
(immutableResult as any).illegal = "this should not work";
throw new Error("unreached");
} catch (e) {
expect(e.message).not.toMatch(/unreached/);
expect((e as Error).message).not.toMatch(/unreached/);
expect(e).toBeInstanceOf(TypeError);
}
assertDeeplyFrozen(immutableResult);
Expand Down
31 changes: 17 additions & 14 deletions src/cache/inmemory/__tests__/writeToStore.ts
Expand Up @@ -26,8 +26,13 @@ import { InMemoryCache } from "../inMemoryCache";
import { withErrorSpy, withWarningSpy } from "../../../testing";
import { TypedDocumentNode } from "../../../core";
import { extractFragmentContext } from "../helpers";
import { KeyFieldsFunction } from "../policies";
import { invariant } from "../../../utilities/globals";

const getIdField = ({ id }: { id: string }) => id;
const getIdField: KeyFieldsFunction = ({ id }) => {
invariant(typeof id === "string", "id is not a string");
return id;
};

describe("writing to the store", () => {
const cache = new InMemoryCache({
Expand Down Expand Up @@ -1293,19 +1298,17 @@ describe("writing to the store", () => {
}

testData.forEach((data) => {
data.mutation.definitions.forEach(
(definition: OperationDefinitionNode) => {
if (isOperationDefinition(definition)) {
definition.selectionSet.selections.forEach((selection) => {
if (isField(selection)) {
expect(
storeKeyNameFromField(selection, data.variables)
).toEqual(data.expected);
}
});
}
data.mutation.definitions.forEach((definition) => {
if (isOperationDefinition(definition)) {
definition.selectionSet.selections.forEach((selection) => {
if (isField(selection)) {
expect(storeKeyNameFromField(selection, data.variables)).toEqual(
data.expected
);
}
});
}
);
});
});
});

Expand Down Expand Up @@ -1357,7 +1360,7 @@ describe("writing to the store", () => {
return value.kind === "OperationDefinition";
}

mutation.definitions.map((def: OperationDefinitionNode) => {
mutation.definitions.map((def) => {
if (isOperationDefinition(def)) {
const writer = new StoreWriter(
new InMemoryCache({
Expand Down
14 changes: 9 additions & 5 deletions src/cache/inmemory/entityStore.ts
Expand Up @@ -548,7 +548,7 @@ class CacheGroup {

// Used by the EntityStore#makeCacheKey method to compute cache keys
// specific to this CacheGroup.
public keyMaker: Trie<object>;
public keyMaker!: Trie<object>;

constructor(
public readonly caching: boolean,
Expand Down Expand Up @@ -755,7 +755,11 @@ class Layer extends EntityStore {
public getStorage(): StorageType {
let p: EntityStore = this.parent;
while ((p as Layer).parent) p = (p as Layer).parent;
return p.getStorage.apply(p, arguments);
return p.getStorage.apply(
p,
// @ts-expect-error
arguments
);
}
}

Expand All @@ -778,20 +782,20 @@ class Stump extends Layer {
return this;
}

public merge() {
public merge(older: string | StoreObject, newer: string | StoreObject) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This minifies even shorter than the old code.

// We never want to write any data into the Stump, so we forward any merge
// calls to the Root instead. Another option here would be to throw an
// exception, but the toReference(object, true) function can sometimes
// trigger Stump writes (which used to be Root writes, before the Stump
// concept was introduced).
return this.parent.merge.apply(this.parent, arguments);
return this.parent.merge(older, newer);
}
}

function storeObjectReconciler(
existingObject: StoreObject,
incomingObject: StoreObject,
property: string
property: string | number
): StoreValue {
const existingValue = existingObject[property];
const incomingValue = incomingObject[property];
Expand Down
6 changes: 3 additions & 3 deletions src/cache/inmemory/fixPolyfills.native.ts
Expand Up @@ -33,10 +33,10 @@ try {
// https://github.com/apollographql/react-apollo/issues/2442#issuecomment-426489517
testMap.set(frozen, frozen).delete(frozen);
} catch {
const wrap = (method: <T>(obj: T) => T): typeof method => {
const wrap = <M extends <T>(obj: T) => T>(method: M): M => {
return (
method &&
((obj) => {
(((obj) => {
try {
// If .set succeeds, also call .delete to avoid leaking memory.
testMap.set(obj, obj).delete(obj);
Expand All @@ -45,7 +45,7 @@ try {
// by this return-from-finally statement:
return method.call(Object, obj);
}
})
}) as M)
);
};
Object.freeze = wrap(Object.freeze);
Expand Down