relay icon indicating copy to clipboard operation
relay copied to clipboard

Make contextual relay into a component

Open aihornmac opened this issue 10 years ago • 13 comments

I wonder if the Relay environment can be made in component like how it is done in redux

const store = createStore();

<Provider store={store}>
...
</Provider>

For relay we can make sth like

const environment = new Relay.Environment();  // future API

<RelayProvider environment={environment}>
...
</RelayProvider>

So that any RelayRootContainer in this Provider may have environment convey to its props. Instead of using RelayStore as environment, make it like

function RelayRootContainer({
  ...
  store
}: RootContainerProps): React$Element {
<RelayRenderer
      ...
      environment={store}
...

aihornmac avatar Mar 25 '16 21:03 aihornmac

This makes sense and would seem to make nested <RelayRenderer />s easier to use (no need to supply the environment prop to every instance). Have there been any downsides to this approach?

cc @gaearon @yungsters

josephsavona avatar Mar 28 '16 18:03 josephsavona

note: edited the original description to clarify the API a bit

josephsavona avatar Mar 28 '16 18:03 josephsavona

I am not sure if I have a say in this, given that no one was commenting on this issue, maybe I should make a complement. Please correct me if I said anything wrong or inappropriate.

I came up with this problem when I was making this isomorphic app with server render that requires authentication. Since GraphQL server, as an API server, should be stateless, that means auth info, Authorization in header for example, should be included in requests.

I was trying to work around this by adding a token argument to Viewer query, which wraps up any data queries. It did work, however, I haven't found any workable way when it comes to Mutations, and I don't really feel good to specify token in every mutation.

Then I reached to isomorphic-relay, which inspired me to check on some specific source codes, and finally I made it by instantiating a RelayEnvironment passed down to RelayRenderer.

Now redux handles session on client side for me, once it changes, depending on circumstances, it shall either injectNetworkLayer or reinstantiate a new RelayEnvironment to flush data. Anyway, Authorization would change.

this.state.relayEnvironment.injectNetworkLayer(
  new DefaultNetworkLayer(RELAY_NETWORK_URL_CLIENT, {
    headers: {
      Authorization: `Bearer ${props.session.token}`,
    },
  })
);

Later I found a problem in RelayContainer. As illustrated above, it should be RelayEnvironment now instead of Relay.Store to handle requests, so some operations like Relay.Store.commitUpdate should be contextual. But the relay in props bound to RelayContainer doesn't offer those methods.

I noticed relayProp is in fact a plain object, so I worked around this by wrapping createContainer, changing the __proto__ of relay passed down from RelayContainer to user component.

import React, { Component } from 'react';
import { createContainer, PropTypes as RelayPropTypes } from 'react-relay';

export default (spec) => (WrappedComponent) => {

  class ComponentWithRelayEnvironment extends Component {

    static contextTypes = {
      relay: RelayPropTypes.Environment
    }

    render () {
      this.props.relay.__proto__ = this.context.relay;
      return <WrappedComponent {...this.props} />;
    }

  }

  return createContainer(ComponentWithRelayEnvironment, spec);
};

And this is basically why I posted this issue. It seems to involve more than just the supply the environment prop to every instance problem. I am pretty sure you must have something done to tackle this beyond published versions.

Looking forward to any information.

aihornmac avatar Apr 03 '16 08:04 aihornmac

@aihornmac You can use this.context.relay in your component directly, you don't need a wrapper to pass it as a prop. Just don't forget to declare relay in the contextTypes of your component as you did above in your wrapper.

denvned avatar Apr 03 '16 09:04 denvned

Well, I was trying to state as less as possible in specific components, since I am using decorators, I am gonna wrap up the official createContainer anyway. As there has been a relay, I just think merging them seems reasonable. But sure this demand may be just personal.

Thank you for your time

aihornmac avatar Apr 03 '16 09:04 aihornmac

@aihornmac Thanks again for suggesting this idea. A <Provider> style component does separate the concerns of setting the context from rendering the data, and could be useful whenever there are nested RelayRenderers. Let's leave this open and see if there's any more input.

@taion Would this be helpful for react-router-relay?

josephsavona avatar Apr 05 '16 15:04 josephsavona

It's not a huge difference for me. It'd just be the difference between

<RelayProvider environment={environment}>
  <RelayRouter history={history} routes={routes} />
</RelayProvider>

and

<RelayRouter history={history} environment={environment} routes={routes} />

I think I actually like the latter slightly more.

Would it be too confusing to make this implicit? e.g. have RelayRenderer implicitly serve as the Provider, and branch between pulling in the environment from props (if specified) versus from context as a fallback.

taion avatar Apr 05 '16 16:04 taion

I haven't seen any downsides to this. For the rare cases when we want to avoid context we let people pass store as a prop. I guess Relay containers could optionally accept environment as a prop too, for the sake of providing an escape hatch.

gaearon avatar Apr 05 '16 22:04 gaearon

I think it might be better to make the <RelayRenderer> serve as the <RelayProvider>, though.

Given that you typically only have a few <RelayRenderer>s, they end up being not too dissimilar from React Redux <Provider>s.

It seems weird to specify an extra <RelayProvider> around the <RelayRenderer> in the general case.

taion avatar Apr 05 '16 22:04 taion

Thanks all for the input. In practice, <RelayRenderer> is acting as the provider already (the context is set via the environment prop). @aihornmac Does this address your intended use case?

josephsavona avatar May 10 '16 16:05 josephsavona

@josephsavona In practice, sometimes we don't need a RelayContainer for declarative data fetching, instead of we might just need ad-hoc data fetching or mutations, in this case, a contextual relay will be required. Since it throws errors when creating RelayContainer without specs, I ended up writing a simple provider. I wonder if I am missing anything.

aihornmac avatar Jun 07 '16 04:06 aihornmac

This issue should be closed. <Relay.ReadyStateRenderer> does in fact provide the Relay environment as context.relay.

taion avatar Nov 01 '16 00:11 taion

@taion good call, the use-case for a separate context-setter isn't really clear. Thanks for starting the discussion, @aihornmac!

josephsavona avatar Nov 01 '16 00:11 josephsavona