Skip to content

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

Merged
merged 17 commits into from
Jun 18, 2025

Conversation

jerelmiller
Copy link
Member

As I was playing around with trying to use observable.getCurrentResult inside useSyncExternalStore, 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.

@jerelmiller jerelmiller requested a review from phryneas June 16, 2025 21:48
Copy link

changeset-bot bot commented Jun 16, 2025

⚠️ No Changeset found

Latest commit: b7925b6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

});

// Maintain object identity as much as possible
return masked === result.data ? result : { ...result, data: masked };
Copy link
Member Author

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;
Copy link
Member Author

@jerelmiller jerelmiller Jun 16, 2025

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.

Copy link

pkg-pr-new bot commented Jun 16, 2025

npm i https://pkg.pr.new/apollographql/apollo-client/@apollo/client@12703

commit: b7925b6

observable
);
if (!watchQueryOptions.fetchPolicy) {
watchQueryOptions.fetchPolicy = observable.options.initialFetchPolicy;
Copy link
Member Author

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.

Copy link
Member

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 :)

Copy link
Member Author

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 👍

Copy link
Member Author

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(
Copy link
Member Author

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>(
Copy link
Member Author

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.

Copy link
Member

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

Comment on lines -455 to +423
() => resultOverride || resultData.current,
() => resultOverride || resultData.current
() => resultData.current,
() => (ssr === false ? useQuery.ssrDisabledResult : resultData.current)
Copy link
Member Author

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 &&
Copy link
Member Author

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

Base automatically changed from jerel/change-vars-error to release-4.0 June 17, 2025 17:42
@github-actions github-actions bot added the auto-cleanup 🤖 label Jun 17, 2025
@jerelmiller jerelmiller force-pushed the jerel/get-emitted-result branch from 47d8f5e to e3365eb Compare June 18, 2025 18:13
Copy link
Contributor

github-actions bot commented Jun 18, 2025

size-limit report 📦

Path Size
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (CJS) 42.54 KB (-0.15% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) (CJS) 37.7 KB (-0.15% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" 32.56 KB (+0.15% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) 26.94 KB (-0.08% 🔽)
import { ApolloProvider } from "@apollo/client/react" 5.68 KB (0%)
import { ApolloProvider } from "@apollo/client/react" (production) 963 B (0%)
import { useQuery } from "@apollo/client/react" 7.42 KB (-1.29% 🔽)
import { useQuery } from "@apollo/client/react" (production) 2.65 KB (-3.15% 🔽)
import { useLazyQuery } from "@apollo/client/react" 6.94 KB (0%)
import { useLazyQuery } from "@apollo/client/react" (production) 2.2 KB (0%)
import { useMutation } from "@apollo/client/react" 6.52 KB (0%)
import { useMutation } from "@apollo/client/react" (production) 1.76 KB (0%)
import { useSubscription } from "@apollo/client/react" 6.62 KB (0%)
import { useSubscription } from "@apollo/client/react" (production) 1.86 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" 8.48 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" (production) 3.75 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" 8.22 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" (production) 3.51 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" 8.52 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" (production) 3.82 KB (0%)
import { useReadQuery } from "@apollo/client/react" 6.43 KB (0%)
import { useReadQuery } from "@apollo/client/react" (production) 1.68 KB (0%)
import { useFragment } from "@apollo/client/react" 6.47 KB (0%)
import { useFragment } from "@apollo/client/react" (production) 1.74 KB (0%)

@jerelmiller jerelmiller merged commit 92212f2 into release-4.0 Jun 18, 2025
43 of 44 checks passed
@jerelmiller jerelmiller deleted the jerel/get-emitted-result branch June 18, 2025 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants