-
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
Better TData
types when using returnPartialData
or a different errorPolicy
#10766
Conversation
🦋 Changeset detectedLatest commit: fa854ea 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 |
For the Caveat: All this logic could also be replaced by export function useSuspenseQuery_experimental<
TData,
TVariables extends OperationVariables,
Options extends SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options?: SuspenseQueryHookOptions<NoInfer<TData>, NoInfer<TVariables>> & Options
): UseSuspenseQueryResult<
Options['errorPolicy'] extends 'ignore' | 'all'
? Options['returnPartialData'] extends true
? DeepPartial<TData> | undefined
: TData | undefined
: Options['returnPartialData'] extends true
? DeepPartial<TData>
: TData,
TVariables
>; This would have it's own Caveats, though:
I would generally say that 1. is a good change we should make anyways before shipping As for 2., that is unfortunate - and we could "fix" that by combining both approaches: add this as an additional first signature for inferred use and keep all the others for user-specified generic usages. |
Yes, I absolutely agree with this. I'd like to start leaning on |
Updated the type tests to also check for the return type when you explicitly set the generic type: 27efa5c |
Updated the default |
@phryneas your solution actually works quite well, except it breaks this test which checks that a wider This is one area of TypeScript I'm not very good at. Do you know if there is a way to ensure the |
Irritatingly, that test works fine for me. Are we using different TypeScript versions? I'm on 5.0.4. Also, why are the tests green in that case - do TypeScript tests not fail our CI yet? |
Thats a good question and very possible. Let me make sure my editor is using the right TS version. For some reason this is difficult information to get in neovim 🙃.
I haven't pushed up any code that causes this to fail, so its expected that everything on this branch is passing. If I copy your code snippet into Our CI should catch TS failures and cause our tests to fail (in fact, here is one of them for commit e008004). |
Yeah I see it now. I believe that this would be easier with only So maybe now would be the best moment in time to do that, and tackle this PR again after that? |
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.
🎉🎉🎉
Fixes #10713
This PR introduces more robust types for the
data
property returned fromuseSuspenseQuery
. In general,data
should beTData
, but there are some cases whereTData
could be different:errorPolicy
other thannone
When using the default error policy of
none
, the hook will throw the error to the nearest suspense boundary. In these cases, we can guaranteedata
isTData
since GraphQL guarantees thatdata
will always have a value when there are no errors returned. When enablingall
orignore
error policies, the hook will no longer throw, so there is a chance thatdata
could now beundefined
. Rather than returningTData | undefined
as the type,useSuspenseQuery
incorrectly reported this type asTData
which could cause runtime errors.returnPartialData: true
When
returnPartialData
is set totrue
and we have data in the cache for the query,useSuspenseQuery
will avoid suspending and instead show render any data it can. Because the data could be partial, thedata
type was incorrectly set toTData
and instead should be aDeepPartial<TData>
.Both of these scenarios are now fixed in this PR. You can combine the options and receive the correct typings. For example:
This PR also changes the default type of
TData
tounknown
instead ofany
whenTData
is not given or cannot be inferred.Checklist: