Skip to content

Conversation

@vladinator1000
Copy link

Adds support for https://reactrouter.com/start/changelog#middleware-unstable

When @phryneas and I were discussing async loaders in #248 (comment) I had a feeling that context might feel really nice to use.

@vladinator1000 vladinator1000 requested a review from a team as a code owner March 14, 2025 18:48
@apollo-cla
Copy link

@vladinator1000: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@changeset-bot
Copy link

changeset-bot bot commented Mar 14, 2025

⚠️ No Changeset found

Latest commit: 9e1ab2d

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

@vercel
Copy link

vercel bot commented Mar 14, 2025

@vladinator1000 is attempting to deploy a commit to the Apollo Client - Next package - integration tests Team on Vercel.

A member of the Team first needs to authorize it.

@vladinator1000 vladinator1000 mentioned this pull request Mar 14, 2025
Copy link
Author

@vladinator1000 vladinator1000 left a comment

Choose a reason for hiding this comment

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

Notes for @phryneas 👀

expect(await page.getByText("loading...").count()).toBe(0);
});

test("middleware", { tag: ["@react-router"] }, async ({ page }) => {
Copy link
Author

Choose a reason for hiding this comment

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

Same as the async loader test, just different endpoint.


export const apolloContext = unstable_createContext<ApolloContext>();

export function createApolloMiddleware(
Copy link
Author

Choose a reason for hiding this comment

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

Same as createApolloLoaderHandler, but sets the context value instead of making the user wrap their loader with another function.

Comment on lines +12 to +25
export const unstable_middleware = [apolloMiddleware]

export async function loader({ context }: Route.LoaderArgs) {
const apollo = context.get(apolloContext)
const queryRef = apollo.preloadQuery(DEFERRED_QUERY, {
variables: { delayDeferred: 1000 },
});

await new Promise((resolve) => setTimeout(resolve, 300));

return {
queryRef,
};
}
Copy link
Author

Choose a reason for hiding this comment

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

Feels quite clean to use.

Comment on lines +60 to +63
const client = makeClient(request);
const preloader = createTransportedQueryPreloader(client);
const preloadQuery: typeof preloader = (...args) =>
replaceStreamWithPromiscade(preloader(...args));
Copy link
Member

Choose a reason for hiding this comment

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

Since we're working in the browser here, and don't need to transport data from the server to the browser, it would be optimal if we could somehow use the same client instance that the React tree sees and use the normal createQueryPreloader of Apollo Client - see https://www.apollographql.com/docs/react/data/suspense#initiating-queries-outside-react

Copy link
Member

Choose a reason for hiding this comment

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

Oh no, I mistook this as a feature only for client loaders. We might be getting some symmetry between server and client out of this in the end. Let's see.

@phryneas phryneas deleted the branch apollographql:next March 31, 2025 10:14
@phryneas phryneas closed this Mar 31, 2025
@phryneas
Copy link
Member

Not closed intentionally, but I merged next into main and deleted next.
I'll be revisiting the context topic at a later date, feel free to reopen this PR :)

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