Skip to content

Commit

Permalink
Fix variables type errors when other options are present
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 11, 2023
1 parent 19e1056 commit 7561ee9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
57 changes: 57 additions & 0 deletions src/react/query-preloader/__tests__/createQueryPreloader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ describe.skip("type tests", () => {

preloadQuery(query);
preloadQuery(query, { variables: {} });
preloadQuery(query, { returnPartialData: true, variables: {} });
preloadQuery(query, { variables: { foo: "bar" } });
preloadQuery(query, { variables: { foo: "bar", bar: 2 } });
});
Expand All @@ -1005,6 +1006,7 @@ describe.skip("type tests", () => {

preloadQuery(query);
preloadQuery(query, { variables: {} });
preloadQuery(query, { returnPartialData: true, variables: {} });
preloadQuery(query, { variables: { foo: "bar" } });
preloadQuery(query, { variables: { foo: "bar", bar: 2 } });
});
Expand All @@ -1017,6 +1019,7 @@ describe.skip("type tests", () => {

preloadQuery(query);
preloadQuery(query, { variables: {} });
preloadQuery(query, { returnPartialData: true, variables: {} });
// @ts-expect-error unknown variables
preloadQuery(query, { variables: { foo: "bar" } });
});
Expand All @@ -1028,6 +1031,8 @@ describe.skip("type tests", () => {
// @ts-expect-error no variables option allowed
preloadQuery(query, { variables: {} });
// @ts-expect-error no variables option allowed
preloadQuery(query, { returnPartialData: true, variables: {} });
// @ts-expect-error no variables option allowed
preloadQuery(query, { variables: { foo: "bar" } });
});

Expand All @@ -1037,20 +1042,37 @@ describe.skip("type tests", () => {

preloadQuery(query);
preloadQuery(query, { variables: {} });
preloadQuery(query, { returnPartialData: true, variables: {} });
preloadQuery(query, { variables: { limit: 10 } });
preloadQuery(query, { returnPartialData: true, variables: { limit: 10 } });
preloadQuery(query, {
variables: {
// @ts-expect-error unknown variable
foo: "bar",
},
});
preloadQuery(query, {
returnPartialData: true,
variables: {
// @ts-expect-error unknown variable
foo: "bar",
},
});
preloadQuery(query, {
variables: {
limit: 10,
// @ts-expect-error unknown variable
foo: "bar",
},
});
preloadQuery(query, {
returnPartialData: true,
variables: {
limit: 10,
// @ts-expect-error unknown variable
foo: "bar",
},
});
});

test("enforces required variables", () => {
Expand All @@ -1061,8 +1083,18 @@ describe.skip("type tests", () => {
preloadQuery(query);
// @ts-expect-error empty variables
preloadQuery(query, { variables: {} });
// @ts-expect-error empty variables
preloadQuery(query, { returnPartialData: true, variables: {} });
preloadQuery(query, { variables: { id: "1" } });
preloadQuery(query, { returnPartialData: true, variables: { id: "1" } });
preloadQuery(query, {
variables: {
// @ts-expect-error unknown variable
foo: "bar",
},
});
preloadQuery(query, {
returnPartialData: true,
variables: {
// @ts-expect-error unknown variable
foo: "bar",
Expand All @@ -1075,6 +1107,14 @@ describe.skip("type tests", () => {
foo: "bar",
},
});
preloadQuery(query, {
returnPartialData: true,
variables: {
id: "1",
// @ts-expect-error unknown variable
foo: "bar",
},
});
});

test("requires variables with mixed TVariables", () => {
Expand All @@ -1087,6 +1127,8 @@ describe.skip("type tests", () => {
preloadQuery(query);
// @ts-expect-error missing variables argument
preloadQuery(query, { variables: {} });
// @ts-expect-error missing variables argument
preloadQuery(query, { returnPartialData: true, variables: {} });
preloadQuery(query, { variables: { id: "1" } });
// @ts-expect-error missing required variable
preloadQuery(query, { variables: { language: "en" } });
Expand All @@ -1098,6 +1140,21 @@ describe.skip("type tests", () => {
},
});
preloadQuery(query, {
returnPartialData: true,
variables: {
// @ts-expect-error unknown variable
foo: "bar",
},
});
preloadQuery(query, {
variables: {
id: "1",
// @ts-expect-error unknown variable
foo: "bar",
},
});
preloadQuery(query, {
returnPartialData: true,
variables: {
id: "1",
// @ts-expect-error unknown variable
Expand Down
16 changes: 11 additions & 5 deletions src/react/query-preloader/createQueryPreloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,22 @@ type PreloadQueryOptionsArg<
> = [TVariables] extends [never] ?
[options?: PreloadQueryOptions<never> & TOptions]
: {} extends OnlyRequiredProperties<TVariables> ?
[options?: PreloadQueryOptions<NoInfer<TVariables>> & TOptions]
: [options: PreloadQueryOptions<NoInfer<TVariables>> & TOptions];
[
options?: PreloadQueryOptions<NoInfer<TVariables>> &
Omit<TOptions, "variables">,
]
: [
options: PreloadQueryOptions<NoInfer<TVariables>> &
Omit<TOptions, "variables">,
];

export function createQueryPreloader(client: ApolloClient<any>) {
const suspenseCache = getSuspenseCache(client);

function preloadQuery<
TData,
TVariables extends OperationVariables,
TOptions extends Omit<PreloadQueryOptions<never>, "variables">,
TOptions extends Omit<PreloadQueryOptions, "variables">,
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
...[options]: PreloadQueryOptionsArg<NoInfer<TVariables>, TOptions>
Expand All @@ -76,7 +82,7 @@ export function createQueryPreloader(client: ApolloClient<any>) {
TVariables extends OperationVariables = OperationVariables,
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options: PreloadQueryOptions & {
options: PreloadQueryOptions<NoInfer<TVariables>> & {
returnPartialData: true;
errorPolicy: "ignore" | "all";
}
Expand All @@ -97,7 +103,7 @@ export function createQueryPreloader(client: ApolloClient<any>) {
TVariables extends OperationVariables = OperationVariables,
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options: PreloadQueryOptions & {
options: PreloadQueryOptions<NoInfer<TVariables>> & {
returnPartialData: true;
}
): QueryReference<DeepPartial<TData>, TVariables>;
Expand Down

0 comments on commit 7561ee9

Please sign in to comment.