-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add feature or document how to reset Apollo store #158
Comments
There's way to achieve this today in Redux. We can compose a HO Root Reducer like function superMegaRootReducer(state, action) {
if (action.type === 'USER_LOGOUT') {
state = undefined
}
return rootReducer(state, action);
}; What would be cool is next having a reset middleware, so when i want to do these resets, i can just hit an generic reset action type and handle some function i want to be called in the process. https://gist.github.com/abhiaiyer91/a8a7ca18a8f9b11faa02c899165d942b Exposing this could be a way to handle tons of things we know we want to do when a user resets |
Yep, just need to add some tests and make sure the client behaves correctly when all of the data disappears! |
Im thinking we should do the following
what do you think? |
What's the higher order reducer for? |
The higher order reducer is for people who want to reset their normal redux state based on the same state clearing the apolloReducer will have. Thoughts? So essentially whatever we clear apolloReducer with i can wrap a redux reducer
where composeResetReducer takes a reducer and some initial state function composeResetReducer(reducer: Function, initialState: any): Function {
return function composedReducer(state: any, action: Object): any {
if (action.type === "RESET_STATE") {
return initialState;
}
return reducer(state, action);
};
} Could be a nice little utility for peeps |
Hmm, I'd rather skip that for now - it doesn't seem relevant to Apollo IMO. I'd rather just document "Apollo uses the I'd rather not get into the business of writing higher-order reducers for people, that sounds like a Redux thing. But if you want to write a separate package for that, it would be fine to include that in the docs as a code example! |
Okay Step 1 function to use. https://github.com/abhiaiyer91/redux-reset |
I think the current workaround is to explicitly pass the store to apollo on construction, and then remove the reference to that store. I think setting the store content to null wouldn't be enough, because you also want to get rid of history. But then again, I don't know much about Redux, so I'll defer to the experts. |
FYI: |
So I tried out a store reset using the mechanism described here, but I found the QueryManager threw an exception whenever I dispatched my RESET action. The internal state for query listeners is actually the issue. If there are outstanding watched queries, it fails inside the And just to clarify - there seems to be no issue if you make sure that all the watched queries are stopped BEFORE resetting the store (although I am not sure there isn't some other state that might need cleaning up as well). From my perspective i think a
|
Should now be fixed w/ PR #314. |
Not sure if this is a complete example on how to reset the store (especially for a logout scenario). The |
For example, when the user logs out we might want to clear our some or all of the data on the client.
The text was updated successfully, but these errors were encountered: