如何用变量组合查询片段

Dmi*_*sky 1 javascript relayjs

我正在使用Relay并且react-router-relay正在尝试组合几个Relay容器.内部容器需要一个查询片段变量,该变量必须从路由器向下传递到其父容器.它没有得到这个变量.

以下是代码的外观:

//  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
          }                                                                                                                                                    
        }                                                                                                                                                                                            
      `                                                                                                                                                                                               
     }
  }
);
Run Code Online (Sandbox Code Playgroud)

内部的草案字段BodyContainer永远不会$id从路径参数中获取.签名RelayContainer.getFragment()有参数似乎让你传递参数和变量,但我不知道如何使用它.

tai*_*ion 5

你的user片段<ComposeContainer>必须是这样的:

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

另外,当你<BodyContainer>从中作曲时<ComposeContainer>.你还需要传递id作为道具,例如

<BodyContainer id={this.props.id} />
Run Code Online (Sandbox Code Playgroud)

另请参阅https://github.com/facebook/relay/issues/309上的其他讨论.