-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Q: Defining data requirement in Relay.Route? #97
Comments
At the moment, there's a 1:1 mapping between a route root query and a fragment. You should also check #20 (comment), because the Digging through the source code, you might be able to achieve what you're looking for using variables, doing something like this: var MyComponentContainer = Relay.createContainer(MyComponent, {
initialVariables: {
showPopup: false,
},
fragments: {
album: (variables) => Relay.QL`
fragment on Album {
name,
${SongList.getFragment('songs').if(variables.showPopup)}
}
}``
}
}); And then running |
Another possibility would be to just use connections, and do something like: var MyComponentContainer = Relay.createContainer(MyComponent, {
initialVariables: {
songsToShow: 0,
},
fragments: {
album: (variables) => Relay.QL`
fragment on Album {
name,
songs(first: $songsToShow) {
edges {
node {
${SongList.getFragment('songs')}
}
}
}
}
}`
}
}); and use |
These conditional fragments would be great pattern for these use cases.
Could someone from fb please comment if thats pattern we can use at the moment/future? Thanks |
@jardakotesovec Sorry for missing this: yes, |
@jardakotesovec did that solution work for you? I'm going to close this issue since the question is answered, but feel free to reopen if you have any additional questions! |
My understanding is that data requirements on Relay Containers are defined statically and are fetched regardless if component is shown.
Therefore logic what data will be actually necessary has to be deduced in Relay.Route from the parameters - that might be coming from url parameters or some other context.
So I guess creating several Relay.Route objects covering different scenarios is way to go currently. I know that its obvious to use multiple Relay.Route when different views are shown. But I mean for example scenario when user is on page with list of albums and clicks on one of them to see pop-up menu with songs list (which is kind of sub-route scenario). That could affect url (using react-router) that could switch to different Relay.Route object that would additionally call
getFragment
on that pop-up menu.I noticed in examples that Relay.Route always just call
getFragment
onComponent
which is argument. But I assume that callinggetFragment
directly in Relay.Route also from other components is also reasonable?Does this approach make sense? Wondering if there are some other tricks how to conditionally change data requirement - and I mean decide whether data be fetched or not (for example based on the fact if pop-up menu is opened or not).
Thanks!
The text was updated successfully, but these errors were encountered: