Skip to content

Replace useFragment with Apollo's native #517

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

fidelman
Copy link
Contributor

@fidelman fidelman requested review from alloy and vladar February 17, 2025 15:29
@fidelman fidelman force-pushed the fidelman/replace-useFragment-with-apollos-native branch from e3b01ef to d27d4dd Compare February 17, 2025 15:39
@fidelman fidelman marked this pull request as draft February 17, 2025 19:36
@@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@apollo/client": "~3.6.0",
"@apollo/client": "3.8.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the version we are using in TMP today? If not, I believe we agreed to use TMP's version and include a patch-package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, TMP uses 3.6.10. I gonna leave the graphitation as it is and patch it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because dep-up duct-tape's Apollo will cause problem in TMP as it is patched there. Discussed w/@vladar so far the easiest way is to patch duct-tape's Apollo (btw it is already it is), and only dep-up duct-tape in TMP.


const client = useOverridenOrDefaultApolloClient();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing from the new implementation, and I do recall that I added this for a reason. It would be good to check the history for when I added this ability and see if there's a reason left in those commits.

Copy link
Contributor Author

@fidelman fidelman Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alloy TLDR; we do not need that in the new implementation

My explanation

The old implementation were introduced by 3 commits:

  1. bf2f03e - replace useState with observableQuery.getCurrentResult(), commit text: Only trigger useState change when we really want it to
  2. 21f61ee#diff-72832f4a1f9e169a840a8d60938b0a66249d0dc384d1cf3124f6ca049747f63f - replace forceUpdate with a dedicated hook, commit text: [apollo-react-relay-duct-tape] Implement useRefetchableFragment
  3. 87cb259, added a comment // Unclear why, but this yields twice with the same results, so skip one., commit text: [ARRDT] Cleanup

My assumption, correct me if you recall, The subscription was triggered twice (for unknown reason), so there is a logic to skip the first trigger and then forceUpdate triggers the hook again getting an actual value from the observableQuery.getCurrentResult();.

Here is how it looks without "skip". Just load and then the "Load more" button click

Screenshot 2025-02-24 173550

Here is how it looks w/ "skip" aka "old version":

Screenshot 2025-02-24 173609
Screenshot 2025-02-24 173836.

And here is the logs of the new version:
Screenshot 2025-02-24 174213
As you can see it is identical to the old version, this is why I think we are good to remove that.

@alloy
Copy link
Member

alloy commented Feb 19, 2025

Great to see how simple this turned out to be! With this in place, and especially if we end up not needing to use TMP's patched Apollo version, I think we could actually go out and advertise duct-tape as something others could use. (But we'd need to discuss that more first.)

Comment on lines 33 to 36
invariant(
typeof from === "string",
"useFragment(): Expected the fragment reference to have a string id. " +
"Did you forget to invoke the compiler?",
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do it here, because fragmentReference.id can be undefined, which is expected according to comments here:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fragmentReference.id is required for useFragment args to connect fragment with a query. @alloy do you think we can accept fragmentReference.id is nillable? Previous implementation did not have such invariant, but cache.watch did not require it either

Comment on lines -69 to -82
if (result.partial) {
invariant(
false,
"useFragment(): Missing data expected to be seeded by the execution query document: %s. Please check your type policies and possibleTypes configuration. If only subset of properties is missing you might need to configure merge functions for non-normalized types.",
JSON.stringify(
// we need the cast because queryInfo and lastDiff are private but very useful for debugging
(
observableQuery as unknown as {
queryInfo?: { lastDiff?: { diff?: Cache.DiffResult<unknown> } };
}
).queryInfo?.lastDiff?.diff?.missing?.map((e) => e.path),
),
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must keep this invariant, but trigger it if useFragment returns !result.complete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed

@fidelman
Copy link
Contributor Author

Great to see how simple this turned out to be! With this in place, and especially if we end up not needing to use TMP's patched Apollo version, I think we could actually go out and advertise duct-tape as something others could use. (But we'd need to discuss that more first.)

sure it is!

@fidelman fidelman force-pushed the fidelman/replace-useFragment-with-apollos-native branch from a3f6a95 to 6ee69a1 Compare February 24, 2025 16:54
@fidelman fidelman marked this pull request as ready for review February 24, 2025 16:55
@fidelman fidelman requested review from vladar and alloy February 24, 2025 16:55
yarn.lock Outdated
@@ -90,12 +90,13 @@
dependencies:
"@babel/highlight" "^7.18.6"

"@babel/code-frame@^7.25.7":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we can revert all changes to yarn.lock now that we have a patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants