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

Passing variables to containers from route/query root #99

Closed
taion opened this issue Aug 17, 2015 · 4 comments
Closed

Passing variables to containers from route/query root #99

taion opened this issue Aug 17, 2015 · 4 comments

Comments

@taion
Copy link
Contributor

taion commented Aug 17, 2015

Let's say I have a client side route like /widgets?color={color}. Correspondingly, I want a query that looks like

query {
  viewer {
    widgets(color: $color first: 10) {
      ...widgetFields
    }
  }
}

After some stumbling, it seems like the best way to do this is something like:

WidgetListView = Relay.createContainer(WidgetListView, {
  initialVariables: {
    color: null
  },

  fragments: {
    viewer: () => Relay.QL`
      widgets(color: $color first: 10) {
        ${WidgetList.getFragment('widgets')}
      }
    `
  }
});

class WidgetListRoute extends Relay.Route {
  static routeName = 'WidgetListRoute';

  static routeParams = {
    color: {}
  };

  static queries = {
    viewer: (Component, {color}) => Relay.QL`
      query {
        viewer {
          ${Component.getFragment('viewer', {color})}
        }
      }
    `
  }
}

class WidgetListApp extends React.Component {
  render() {
    return (
      <Relay.RootContainer
        Component={WidgetListView}
        route={new WidgetListRoute({color: this.props.color})}
      />
    );
  }
}

Is this indeed the correct approach, or am I horribly abusing undocumented features?

@taion
Copy link
Contributor Author

taion commented Aug 17, 2015

I imagine all will be made clear once the todo example gets fleshed out, but this was just one of the first things I hit in trying to do a quick Relay-based PoC, so wanted to see how far I was from the right track.

@cpojer
Copy link
Contributor

cpojer commented Aug 18, 2015

Yes this seems correct.

@taion
Copy link
Contributor Author

taion commented Aug 18, 2015

Cool, just wanted to make sure I wasn't abusing private APIs. Thanks!

@taion taion closed this as completed Aug 18, 2015
@devknoll
Copy link
Contributor

@taion Found this too: https://facebook.github.io/relay/docs/api-reference-relay-mutation.html#getfragment-static-method so I guess that makes it official? 😉

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