-
Notifications
You must be signed in to change notification settings - Fork 787
getDataFromTree throws errors when any query fails #615
Comments
Hey @choonkending, sorry it took me so long to get around this issue. I agree with you 100% that this behavior isn't consistent, but because of the way server side rendering is implemented, we can't easily change it to do what you want. The reason is that the queries are all executed in a first pass on the route, then their data is stored in the cache. When you call We're planning improvements in the next version of the API that would make this problem go away, but for the moment I think it's quite difficult to change. If you're very interested and motivated to work on this problem, just let me know, we could definitely use the help! |
@helfer No worries! Thanks for getting back to us.
Definitely. Just to get a better understanding:
|
@choonkending the cache normalizes all data, and when you run a query against the cache, the result is assembled from the normalized parts. That's a problem if errors don't have paths, because then there's no way to know which field an error is associated with, and no way to know if an error should be returned with the query run against the cache. There's no discussion issue yet for the next version of the API, but I'm trying to put an initial post together soon. In the meantime, I think the best thing to do would be to write a use-case for each requirement that we can think of. Having SSR be consistent with client rendering is one use-case, and I think we should write that down and document it well, so we can have a discussion around it. For that purpose, could you add a comment here, please? #519 You should also feel free to write down other use-cases that haven't been mentioned yet (so far there are very few, I'm hoping people will add more to it, even current functionality of react-apollo that they are relying on). |
This really needs to be talked through and fixed 💪 After I clean up the tests and add better typings. I'll try to get around to error handling and SSR! |
Just want to leave a comment in support of @choonkending proposal. I already handle errors in components that perform queries: |
I would just like to say that this has become a big issue for me. As my app has gotten more complex, i have gone down the route of having 2 bundles, and if a 404 then rendering 2nd bundle, and losing the SPA benefits. If you could still keep query errors in state, and allow SSR of 404 pages, it would simply things massively |
Hi @helfer
I'm very interested and motivated (as long as it's my impediment). |
Hi @edoroshenko. That's awesome. @jbaxleyiii can probably tell you more about it since he designed it together with Tom and is now working on Apollo full time! |
Thanks @helfer! |
@edoroshenko you bet! Check out http://dev.apollodata.com/community/ and join us on slack! I'd be happy to work together on finally bringing some better error support to Apollo and SSR in general! |
The way I see the solution:
This plan will affect @jbaxleyiii , @helfer, what do you think about this plan? Or do you have another ideas, how to approach this issue? |
This issue has been automatically labled because it has not had recent activity. If you have not received a response from anyone, please mention the repository maintainer (most likely @jbaxleyiii). It will be closed if no further activity occurs. Thank you for your contributions to React Apollo! |
This issue has been automatically closed because it has not had recent activity after being marked as no recent activyt. If you belive this issue is still a problem or should be reopened, please reopen it! Thank you for your contributions to React Apollo! |
This is still a huge issue for us, how do I re-open it? We've wasted a lot of time trying to figure out why our error handling doesn't work on SSR while it works fine in the browser. It should be documented somewhere that it isn't expected to work yet! |
Same problem here. Any news? For example i want to throw error with info when user is not authenticated. This doesn't work with SSR getDataFromTree 😢 |
@jbaxleyiii any news on this since apollo 2.0 is coming, would be nice to see something like this sorted |
This is still happening and it kinda breaks SSR. |
@jbaxleyiii Can this issue please be re-opened? |
Any news on this? Having the same issue with |
Hey peeps, just giving an update of the approach we've taken in my team since this issue was created. Hopefully it helps if it applies to your use case! We opted to rely less on react-apollo when it came to data fetching. (I'm not bagging the good work that's been done on react-apollo! It's just the way we decided to go from a technical perspective)
You should be able to handle server side rendering with just apollo client! I hope this helps! |
This issue should be reopened. I am using a similar setup as described in the question and every time there is an error, the .catch() statement logs the error to the console and stops the entire page from rendering. Any help would be appreciated. |
Here's one approach to dealing with this. Just ignore errors on the server, but make sure you handle them in your components;
|
@mhaagens but how do you handle the errors in your components? |
If the app errors on the server I add the error to apollo local state in a second pass from a try/catch handler and then render a generic error component in the app. I can then pick up that error state on the client as well. If it errors on the client I render the same component with the client side error instead. Basic flow;
Now make sure you don't follow this example to the letter in production, you don't want to leak those errors to the client, but you can use the same idea. SSR Route handler;
Top level app component;
|
any update? |
This comment has been minimized.
This comment has been minimized.
Please, reopen the issue. |
Wow - sorry, this issue should have been re-opened a while ago 😳. I'll digest everything here and hash out a plan of attack based on how |
Thanks a lot @hwillson, Apollo is great, this is just the last pain point for me right now |
Was this issue closed by an accident or is it expected? |
Wasn't #2753 published in 2.4.1? |
network errors thrown from |
@gbiryukov i believe the fix is only for GraphQL errors, as Network errors are realistically a different thing all together and shouldn't be swallowed. |
aah, the issue with this idea is that it works different on client. when network error occurs in browser – component receives error prop. this makes impossible to handle errors consistently on both environments |
Well there is a lot of debate around this, but i think the current solution of network errors throwing and graphql errors being passed to props is correct in my opinion. We have full consistency with graphql errors on both environments, and that's the important part, because that part is what we realistically control, we don't control the network (if you get what l mean). My solution is if l hit a network error, refresh the page. My server should then throw an error if it still is a network error, so l can redirect to say a status page. |
react-apollo version: 3.1.1 I can confirm using ApolloError while using renderToStringWithData or getDataFromTree just throws it and doesn't pass down the error as intended. I am rerendering without apollo on catch statement of renderToStringWithData and letting the client redo the request. |
@kaanozcan do you have |
That indeed works. Thanks @OllieJennings |
I was stuck on this forever. It seems possible that if you're using useQuery hooks that there are better way to handles this. Since the project I'm working on was built before hooks was introduced, I had to look for an alternative method. The problem seemed to stem from rendering Apollo providers with a null Apollo state. It seemed to get stuck in an endless loop, thus the unresponsive server. To get around this, I evaluated if the apolloState was empty or not. If it was in fact empty, that pointed to an Error in getDataFromTree. From there, knowing I had an error, I'd render my app (_app) with an error prop passed in. The error flag stops the ApolloProviders from rendering, and the app from rendering, and instead returns an ErrorComponent. This has stopped network errors from bombing my server. It's not pretty, and I don't love it, but it means that the server won't require a manual restart in the event of a backend outage, and that I can render a consistent error message. I believe it won't automatically resolve on a resumed connection + navigation to another page, because the client does not have any other routes in this. This means you'd need to refresh the browser to clear the error. I'll post some samples below in case it will help anyone, and I'm open to additional opinions!
|
Background
I asked a question on StackOverflow today here.
I thunk about it harder afterwards and decided that this is a discussion worthy of a Git issue.
Problem
Any error gets thrown in getDataFromTree.
Expected/Proposed Behaviour
Return the data with an
error
Array, without throwing an error.Why?
Our GraphQL server speaks to multiple APIs and returns a graphQL response which is handled by Apollo UI.
Certain GraphQL errors are not considered critical, and we want to still render a page with it. E.g.
With the above response we get from apollo, we want to still render a page with
thing
.I have followed #488 (comment) and got to this point.
In my mind the errors should be returned and not thrown unless necessary, because not all errors are equal.
Ideally I would like to do the following
Version
P.S. Thanks for an awesome library. Happy to help out if needed.
The text was updated successfully, but these errors were encountered: