Closed EfstathiadisD closed 2 months ago
Hi @EfstathiadisD!
The queryRef
you are seeing is only a partial transport object - in parallel, the data of that will also be streamed into the browser, injected into the DOM as a script
tag, and simulate an ongoing request in your actual ApolloClient
instance. The "transport query Ref" will then at some point (usually when consumed by a hook) be turned back into a real queryRef, latching onto that simulated ongoing request.
That said, I wouldn't try to replicate all of that in a test ;)
Instead, you can create a real queryRef
by using the @apollo/client
core api createQueryPreloader
.
That could look like this:
const mockLink = new MockLink([
/* Your mocks here */
]);
const client = new ApolloClient({
link: mockLink,
cache: new InMemoryCache(),
})
const preloader = createQueryPreloader(client);
const queryRef = preloader(query, { variables: { id: 1 } });
PS: There is some warning re. the toPromise() with NextJS as well. It tells me that it should pass pure objects btwn Client and Server components. And toPromise() isn't as far as I can tell. It works on runtime, but I am thinking this isn't what I am mocking correct since it could be far more complicated that it looks on the surface. Maybe wrong?
We're removing toPromise
in the next release because of that warning, but it's really just a warning from React that's a bit too eager, you can ignore it for now.
Thanks! That solve the issue.
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.
I have a Server and Client component combo. The server has the following..
The Client is reading it..
That works as expected. Now I am trying to write some tests.. I am using MockProvider and when I am passing the following object as queryRef to the component which I copy pasted from the console. I get the error..
I tried a few cause it seems what I get on the server is different from the one on the client.
The ProfileDocument is correct. I checked. I am not sure of two things..
It would be nice if someone can link to an example where such specs are written or a README as I couldn't find anything in the DOCS. Thank y!
PS: There is some warning re. the
toPromise()
with NextJS as well. It tells me that it should pass pure objects btwn Client and Server components. And toPromise() isn't as far as I can tell. It works on runtime, but I am thinking this isn't what I am mocking correct since it could be far more complicated that it looks on the surface. Maybe wrong?