-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Minor improvements to useQuery
/ObservableQuery
#12703
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
Conversation
|
}); | ||
|
||
// Maintain object identity as much as possible | ||
return masked === result.data ? result : { ...result, data: masked }; |
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.
I figured we should try and maintain object identity as much as possible, so it makes sense to return the original result
when data
hasn't changed.
|
||
default: | ||
return defaultResult; | ||
return uninitialized; |
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 these are a part of getInitialResult
, no need to create new objects just to return values deep equal to empty
/uninitialized
. This better maintains object identity this way.
commit: |
src/react/hooks/useQuery.ts
Outdated
observable | ||
); | ||
if (!watchQueryOptions.fetchPolicy) { | ||
watchQueryOptions.fetchPolicy = observable.options.initialFetchPolicy; |
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.
We already modify watchQueryOptions.fetchPolicy
in the if (skip)
condition above, so I didn't think it was necessary to have to extract this to its own function just to "use no memo" for the compiler.
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.
Let's get the compiler merged in before we merge this so we'll notice if something changed there :)
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.
Yep, I'm ok with 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.
Good call on waiting. This caused the compiler to remove all the optimizations so I reverted this back. Looks better now https://github.com/apollographql/apollo-client/actions/runs/15740542099
|
||
useResubscribeIfNecessary<TData, TVariables>( | ||
resultData, // might get mutated during render | ||
observable, // might get mutated during render | ||
watchQueryOptions | ||
); | ||
|
||
const resultOverride = useSyncExternalStore( |
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.
Rather than using a separate useSyncExternalStore
for ssr === false
, I merged this with the uSES in useResultSubscription
.
} | ||
|
||
function useResultSubscription<TData, TVariables extends OperationVariables>( | ||
function useResult<TData, TVariables extends 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.
I shortened the name to save a touch of bundle size.
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.
I think a minifier should already do that on userland side, but I won't complain :D
() => resultOverride || resultData.current, | ||
() => resultOverride || resultData.current | ||
() => resultData.current, | ||
() => (ssr === false ? useQuery.ssrDisabledResult : resultData.current) |
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 is the result of merging both uSES together
return; | ||
} | ||
|
||
if ( | ||
previousResult && |
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.
previousResult
is always guaranteed a value as its a non-null type, so this check was unnecessary
47d8f5e
to
e3365eb
Compare
size-limit report 📦
|
As I was playing around with trying to use
observable.getCurrentResult
insideuseSyncExternalStore
, I found a couple places that could use a small bit of improvement. This is the result of those changes which reduces some bundle size in the process.