How to access the redux store from middleware, since I'm storing the token required for authentication there?
I think the best option is to reference the store directly, and call getState() on it.
Since the middleware isn't part of the UI component, you don't need to use something like Provider or dependency injection to do this.
@stubailo How would you suggest doing so?
The issue is that referencing the store directly from the middleware creates a cyclical dependency of store->middleware->client->store.
@morgante Did you end up figuring out a nice way to do it? The only way I could achieve this was with a singleton store which I was trying to avoid.
Couldn't you do something like this:
function initClientAndStore() {
const store = createStore(...);
const networkInterface = createNetworkInterface(...);
const client = new ApolloClient({ networkInterface });
networkInterface.use([{
applyMiddleware(req, next) {
store.getState();
}
}]);
}
@stubailo Thanks, exactly what I ended up doing. Just wasn't obvious that the networkInterface could have use() called after instantiating the client.
@matt-way @stubailo Thanks. I was stuck on the whole cyclic dependency thing, too
if anyone else had an issue using the network interface returned by addGraphQLSubscriptions, be sure to don't use it, use the original networkInterface returned by createNetworkInterface
Most helpful comment
@stubailo How would you suggest doing so?
The issue is that referencing the store directly from the middleware creates a cyclical dependency of store->middleware->client->store.