-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
variables/options
should be required if TVariables
is not empty/default
#11241
base: main
Are you sure you want to change the base?
Changes from all commits
cdc82ea
80b52fc
dbbbd10
f3aecc7
08793f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
useQuery: `variables/options` should be required if `TVariables` is not empty/purely optional/default |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8565,4 +8565,116 @@ describe.skip("Type Tests", () => { | |
// @ts-expect-error | ||
variables?.nonExistingVariable; | ||
}); | ||
|
||
describe("optional/required options/variables scenarios", () => { | ||
test("untyped document node, variables are optional and can be anything", () => { | ||
const query = {} as DocumentNode; | ||
useQuery(query); | ||
useQuery(query, {}); | ||
useQuery(query, { variables: {} }); | ||
useQuery(query, { variables: { opt: "opt" } }); | ||
useQuery(query, { variables: { req: "req" } }); | ||
}); | ||
test("typed document node with unspecified TVariables, variables are optional and can be anything", () => { | ||
const query = {} as TypedDocumentNode<{ result: string }>; | ||
useQuery(query); | ||
useQuery(query, {}); | ||
useQuery(query, { variables: {} }); | ||
useQuery(query, { variables: { opt: "opt" } }); | ||
useQuery(query, { variables: { req: "req" } }); | ||
}); | ||
test("empty variables are optional", () => { | ||
const query = {} as TypedDocumentNode< | ||
{ result: string }, | ||
Record<string, never> | ||
>; | ||
useQuery(query); | ||
useQuery(query, {}); | ||
useQuery(query, { variables: {} }); | ||
useQuery(query, { | ||
variables: { | ||
// @ts-expect-error on unknown variable | ||
foo: "bar", | ||
}, | ||
}); | ||
Comment on lines
+8594
to
+8599
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indenting these tests like this to highlight where exactly I am expecting the error to be propagated to the user. |
||
}); | ||
test("all-optional variables are optional", () => { | ||
const query = {} as TypedDocumentNode< | ||
{ result: string }, | ||
{ opt?: string } | ||
>; | ||
useQuery(query); | ||
useQuery(query, {}); | ||
useQuery(query, { variables: {} }); | ||
useQuery(query, { variables: { opt: "opt" } }); | ||
useQuery(query, { | ||
variables: { | ||
// @ts-expect-error on unknown variable | ||
foo: "bar", | ||
}, | ||
}); | ||
useQuery(query, { | ||
variables: { | ||
opt: "opt", | ||
// @ts-expect-error on unknown variable | ||
foo: "bar", | ||
}, | ||
}); | ||
}); | ||
test("non-optional variables are required", () => { | ||
const query = {} as TypedDocumentNode< | ||
{ result: string }, | ||
{ req: string } | ||
>; | ||
// @ts-expect-error on missing options | ||
useQuery(query); | ||
useQuery( | ||
query, | ||
// @ts-expect-error on missing variables | ||
{} | ||
); | ||
useQuery(query, { | ||
// @ts-expect-error on empty variables | ||
variables: {}, | ||
}); | ||
useQuery(query, { | ||
variables: { | ||
// @ts-expect-error on unknown variable | ||
foo: "bar", | ||
}, | ||
}); | ||
useQuery(query, { variables: { req: "req" } }); | ||
useQuery(query, { | ||
variables: { | ||
req: "req", | ||
// @ts-expect-error on unknown variable | ||
foo: "bar", | ||
}, | ||
}); | ||
}); | ||
test("mixed variables are required", () => { | ||
const query = {} as TypedDocumentNode< | ||
{ result: string }, | ||
{ req: string; opt?: string } | ||
>; | ||
// @ts-expect-error on missing options | ||
useQuery(query); | ||
// @ts-expect-error on missing variables | ||
useQuery(query, {}); | ||
|
||
useQuery(query, { | ||
// @ts-expect-error on empty variables | ||
variables: {}, | ||
}); | ||
|
||
useQuery(query, { | ||
// @ts-expect-error on missing required variable | ||
variables: { | ||
opt: "opt", | ||
}, | ||
}); | ||
useQuery(query, { variables: { req: "req" } }); | ||
useQuery(query, { variables: { req: "req", opt: "opt" } }); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,37 @@ const { | |
prototype: { hasOwnProperty }, | ||
} = Object; | ||
|
||
interface QueryHookOptionsWithVariables< | ||
TData, | ||
TVariables extends OperationVariables, | ||
> extends Omit<QueryHookOptions<TData, TVariables>, "variables"> { | ||
variables: TVariables; | ||
} | ||
|
||
type OnlyRequiredProperties<T> = { | ||
[K in keyof T as {} extends Pick<T, K> ? never : K]-?: T[K]; | ||
}; | ||
|
||
type HasRequiredVariables<T, TrueCase, FalseCase> = | ||
{} extends OnlyRequiredProperties<T> ? FalseCase : TrueCase; | ||
|
||
export function useQuery< | ||
TData = any, | ||
TVariables extends OperationVariables = OperationVariables, | ||
>( | ||
query: DocumentNode | TypedDocumentNode<TData, TVariables>, | ||
...[options]: HasRequiredVariables< | ||
TVariables, | ||
[ | ||
optionsWithVariables: QueryHookOptionsWithVariables< | ||
NoInfer<TData>, | ||
NoInfer<TVariables> | ||
>, | ||
], | ||
[options?: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>] | ||
> | ||
Comment on lines
+63
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have decided to use the "one signature, tuple behind conditional approach" here, as staying with only one public overload will make errors more readable and "localized". This is how an error would look like if we had two overloads - the full call is marked red and the error is hard to read: Instead, using this approach, the error looks like this - only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also something to highlight in these screenshots: the interface name |
||
): QueryResult<TData, TVariables>; | ||
|
||
export function useQuery< | ||
TData = any, | ||
TVariables extends OperationVariables = OperationVariables, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where this causes problems is in generic implementations like our
Query
HOC:Similar errors would always appear if we go through with the "options/variables sometimes required" approach here - there is no way of implementing that without that result.
Luckily, as you can see there's an easy fix - but this might cause some minor churn.