Skip to content
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

How to dynamically change the network layer? #1215

Closed
ron-liu opened this issue Jun 15, 2016 · 3 comments
Closed

How to dynamically change the network layer? #1215

ron-liu opened this issue Jun 15, 2016 · 3 comments

Comments

@ron-liu
Copy link

ron-liu commented Jun 15, 2016

How to call injectNetworkLayer without token when bootstrapping, and then overwrite the networklayer after signing in, like the following code shows:

// when bootstrapping
Relay.injectNetworkLayer(
    new Relay.DefaultNetworkLayer('http://some-server/graphql')
);

// after signing in
signIn().then(token => {
   Relay.injectNetworkLayer(
    new Relay.DefaultNetworkLayer('/graphql'), {
        headers: {
            Authorization: `Bearer  ${token}`
        }
    } 
   // Currently, the above sentence will throw exception and prevent overwriting, what is the other way to accomplish?  

});
@nodkz
Copy link
Contributor

nodkz commented Jun 15, 2016

Very strange, but it should work.
Version 0.8.0 send noisy warning to console, when Default network layer was reinjected. But all works properly. Try to update till 0.8.1 or 0.9.0.

If it does not help, you may try my OSS implementation of network layer https://github.com/nodkz/react-relay-network-layer It contains several good middlewares, which solves common tasks.

So your code may be implemented such way:

const tokenInGlobalVars = null;

signIn().then(token => {
   tokenInGlobalVars = token;
});

Relay.injectNetworkLayer(new RelayNetworkLayer([
  urlMiddleware({
    url: (req) => 'http://some-server/graphql', // this method will be called every time, so you may change url on fly
  }),
  authMiddleware({
    token: () => tokenInGlobalVars,
    allowEmptyToken: true,
  }),
], { disableBatchQuery: true }));

Also was discussion where and how to store a token on the client side: relay-tools/react-relay-network-layer#6

@ron-liu
Copy link
Author

ron-liu commented Jun 15, 2016

I already in 0.9.0, I am sure when calling injectNetworkLayer second time, it will cause an exception.

@nodkz, thanks for your powerful library, I will use it soon.

@wincent
Copy link
Contributor

wincent commented Jun 15, 2016

I'd recommend using @nodkz's layer like he describes. The intention is to not inject new network layers all the time, but rather inject a single layer that is flexible enough to embody your desired behavior. (We made this stricter in response to bugs that cropped up internally in large apps where we were trying to update the layer in multiple places and unknowingly clobbering previous adjustments.)

As I think this is now resolved, going to mark it as closed.

@wincent wincent closed this as completed Jun 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants