apollographql / apollo-client-nextjs

Apollo Client support for the Next.js App Router
https://www.npmjs.com/package/@apollo/experimental-nextjs-app-support
MIT License
358 stars 25 forks source link

Export of symbols for test resets? #34

Closed luchillo17 closed 11 months ago

luchillo17 commented 11 months ago

Problem

I am probably doing something weird to have a wrapper defined in an NX monorepo & have it tested like this, but so far this is the most concise test I could make.

The issue stems from trying to check if the ApolloClient in the context has the correct link URL, since this lib at the moment seems to store state in the global.window, the only way for me to make more than 1 test work is by resetting the content of the variables this library sets in window.

2 things I want to ask:

  1. Why does this need to store the state in the window?
  2. Can we export those symbols for type safety? redeclaring them myself as well as the type definition in the window interface is bound to be error prone.

image

luchillo17 commented 11 months ago

The specific file where I got the symbols from: package/src/ssr/ApolloRehydrateSymbols.tsx

phryneas commented 11 months ago

Hi @luchillo17, good point, this needs some way to reset in tests.

I don't want to expose the symbols though - this is an implementation detail and can change in future versions. Instead, I've added a resetNextSSRApolloSingletons function in #35.

You can use it like

afterEach(resetNextSSRApolloSingletons);

As for your question

Why does this need to store the state in the window?

These singletons are needed to transport data from SSR into the browser - as the Client in the browser already starts up before the SSR pass finishes, we need to continuously push data from the server into the browser client.
Next.js actually does something very similar to this, take a look at window.__next_f.

phryneas commented 11 months ago

A 0.2.0 release with this will be available in a moment :)

luchillo17 commented 11 months ago

Awesome, will try it ASAP.

luchillo17 commented 11 months ago

Not working unfortunately, these are your resets:

    delete window[ApolloRehydrationCache];
    delete window[ApolloResultCache];
    delete window[ApolloSSRDataTransport];

And these are mine:

    delete window[ApolloResultCache];
    delete window[ApolloClientSingleton];
    delete window[SuspenseCacheSingleton];
luchillo17 commented 11 months ago

@phryneas can we re-open this? the resets you put were not enough for my test...

phryneas commented 10 months ago

Hi @luchillo17 I'm sorry for the delay - I'll put out a fix for this in a bit. (And yes, please always just reopen or open a new issue - it helps the visibility a lot!)

phryneas commented 10 months ago

Released with https://github.com/apollographql/apollo-client-nextjs/releases/tag/v0.2.2 - if there's anything else, please reopen, or create a new issue :)

luchillo17 commented 10 months ago

Working perfectly, thanks.