Dmitry Minkovsky
Dmitry Minkovsky

Reputation: 38143

How can I compose query fragments with variables

I am using Relay and react-router-relay and am trying to compose several Relay containers. The inner container needs a query fragment variable that must be passed from the router down through its parent containers. It's not getting this variable.

Here's how the code looks:

//  A "viewer":

const UserQueries = { user: () => Relay.QL`query { user }` };

//  A route to compose a message:

<Route                                                                                                                                                                                  
  path='/compose/:id'                                                                                                                                                                          
  component={ ComposeContainer }                                                                                                                                                               
  queries={ UserQueries }                                                                                                                                                             
/>

//  A container:

const ComposeContainer = Relay.createContainer(
  Compose,
  {
    initialVariables: {
      id: null
    },                                                                                                                                                                                                                                                                                                                                                                                                  
    fragments: {                                                                                                                                                                                        
      user: () => Relay.QL`                                                                                                                                                                           
        fragment on User {                                                                                                                                                                          
          ${ BodyContainer.getFragment('user') }                                                                                                                                                           
          // other fragments                                                                                                                                              
        }                                                                                                                                                                                            
      `                                                                                                                                                                                               
     }
  }
);

//  And the composed container:

const BodyContainer = React.createContainer(
  Body,
  {
    initialVariables: {
      id: null
    },                                                                                                                                                                                                                                                                                                                                                                                                  
    fragments: {                                                                                                                                                                                        
      user: () => Relay.QL`                                                                                                                                                                           
        fragment on User {                                                                                                                                                                          
          draft(id: $id) {
            // fields
          }                                                                                                                                                    
        }                                                                                                                                                                                            
      `                                                                                                                                                                                               
     }
  }
);

The draft field inside BodyContainer never gets $id from the route param. The signature to RelayContainer.getFragment() has arguments that seem to let you pass params and variables, but I am not sure how this should be used.

Upvotes: 1

Views: 3050

Answers (1)

taion
taion

Reputation: 2837

Your user fragment on <ComposeContainer> has to be something like:

user: ({ id }) => Relay.QL`
  fragment on User {
    ${BodyContainer.getFragment('user', { id })}
    // other fragments
  }
`

Additionally, when you compose in <BodyContainer> from <ComposeContainer>. You'll also need to pass in id as a prop, e.g.

<BodyContainer id={this.props.id} />

See also additional discussion at https://github.com/facebook/relay/issues/309.

Upvotes: 5

Related Questions