-
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
Improve useBackgroundQuery
type interface and add type tests
#10951
Improve useBackgroundQuery
type interface and add type tests
#10951
Conversation
🦋 Changeset detectedLatest commit: dcc6933 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
size-limit report 📦
|
@@ -131,6 +135,37 @@ function renderIntegrationTest({ | |||
return { ...rest, query, client: _client, renders }; | |||
} | |||
|
|||
interface VariablesCaseData { |
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.
Lifted VariablesCaseData
/VariablesCaseVariables
up to the file scope to make type tests easier to write, a nice pattern borrowed from the useSuspenseQuery
tests
refetch: ( | ||
variables?: Partial<OperationVariables> | undefined | ||
) => Promise<ApolloQueryResult<Data>>; | ||
refetch: RefetchFunction<Data, 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.
refetch
type is much cleaner now ✨
expectTypeOf(explicit).not.toEqualTypeOf<VariablesCaseData | undefined>(); | ||
}); | ||
|
||
// TODO: https://github.com/apollographql/apollo-client/issues/10893 |
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.
The remaining five type tests from useSuspenseQuery
have references to returnPartialData
and will be completed as part of the work in #10893
@@ -61,8 +61,6 @@ export type SubscribeToMoreFunction< | |||
TVariables extends OperationVariables | |||
> = ObservableQueryFields<TData, TVariables>['subscribeToMore']; | |||
|
|||
export type Version = 'main' | 'network'; |
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.
Pretty sure this is the last vestige of all your refactoring work, @jerelmiller - thought I'd zap it ⚡
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.
Good catch! Thanks for removing this!
@@ -6928,17 +6928,17 @@ describe('useSuspenseQuery', () => { | |||
it('returns TData | undefined with errorPolicy: "all"', () => { | |||
const { query } = useVariablesQueryCase(); | |||
|
|||
const { data: inferred } = useSuspenseQuery< |
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.
Found some tests that seemed to have the inferred
/explicit
labelling flipped and figured I'd include those small fixes in this PR, let me know if these look good to you
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.
Thank you! I noticed that too when adding skip
support. Appreciate it!
import { invariant } from '../../utilities/globals'; | ||
|
||
export type UseBackgroundQueryResult< | ||
TData = any, | ||
TData = unknown, |
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.
any
unknown
🎉
} | ||
]; | ||
|
||
export function useBackgroundQuery< | ||
TData = any, | ||
TData, |
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.
These overloads are taken from useSuspenseQuery
with the caveat that returnPartialData
/refetchWritePolicy
are not available in useBackgroundQuery yet (to be added in #10893)
@@ -20,6 +20,10 @@ import type { | |||
} from '../../core'; | |||
import type { SuspenseCache } from '../cache'; | |||
|
|||
/* QueryReference type */ | |||
|
|||
export type { QueryReference } from '../cache/QueryReference'; |
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.
This should fix the current import { QueryReference } from "@apollo/client/react/cache/QueryReference";
situation
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.
Ah thank you!
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.
Really great to get this dialed in! Thanks for putting this together!
SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>, | ||
'returnPartialData' | 'refetchWritePolicy' | ||
> & { | ||
returnPartialData: true; |
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.
Since this option is being added in a separate PR and because you have Omit<..., 'returnPartialData'>
in this overload, do we need this for this PR? I think you can omit this case for now and add it in #10960
@@ -61,8 +61,6 @@ export type SubscribeToMoreFunction< | |||
TVariables extends OperationVariables | |||
> = ObservableQueryFields<TData, TVariables>['subscribeToMore']; | |||
|
|||
export type Version = 'main' | 'network'; |
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.
Good catch! Thanks for removing this!
@@ -20,6 +20,10 @@ import type { | |||
} from '../../core'; | |||
import type { SuspenseCache } from '../cache'; | |||
|
|||
/* QueryReference type */ | |||
|
|||
export type { QueryReference } from '../cache/QueryReference'; |
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.
Ah thank you!
Closes #10878. Improves types for
useBackgroundQuery
and adds type tests.Follow-on work to support returning partial data (including representing as
DeepPartial<TData>
in the response type) will be tackled as part of #10893.Checklist: