【问题标题】:How can I compose query fragments with variables如何用变量组合查询片段
【发布时间】:2016-04-01 14:34:01
【问题描述】:

我正在使用 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
          }                                                                                                                                                    
        }                                                                                                                                                                                            
      `                                                                                                                                                                                               
     }
  }
);

BodyContainer 中的草稿字段永远不会从路由参数中获得$idRelayContainer.getFragment() 的签名具有似乎可以让您传递参数和变量的参数,但我不确定应该如何使用它。

【问题讨论】:

    标签: javascript relayjs


    【解决方案1】:

    &lt;ComposeContainer&gt; 上的 user 片段必须类似于:

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

    此外,当您从&lt;ComposeContainer&gt; 中撰写&lt;BodyContainer&gt; 时。您还需要传入 id 作为道具,例如

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

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

    【讨论】:

    • 谢谢!真的很感激
    猜你喜欢
    • 2011-09-08
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    相关资源
    最近更新 更多