2016-05-17 47 views
4

定义中继容器的片段时,我们可以有条件地包含或跳过字段。 For example,如果showComments变量为true,则以下代码仅包含comments中继:有条件地包含突变胖查询中的字段

Relay.createContainer(Story, { 
    initialVariables: { 
    numCommentsToShow: 10, 
    showComments: false, 
    }, 
    fragments: { 
    story: (variables) => Relay.QL` 
     fragment on Story { 
     comments(first: $numCommentsToShow) @include(if: $showComments) { 
      edges { 
      node { 
       author { name }, 
       id, 
       text, 
      }, 
      }, 
     }, 
     } 
    `, 
    } 
}); 

我们怎么能有条件地包括在mutation's fat query领域?

的使用用途:而不必单独muttaions更新型的各个领域,我们可以重复使用相同的突变更新任何领域,响应只获取该字段。这样做可以使我们减少有效负载。

这个问题的动机是另一个问题Reusing a Mutation in Relay

回答

0

实际上,你可以在FatQuery使用字符串插值:

getFatQuery() { 
    return Relay.QL` 
    fragment on EditCommentPayload { 
     comment { 
     ${this.props.fields.join(',')} 
     } 
    } 
    `; 

这似乎有点反GraphQL但遗憾的是脂肪查询(related issue)没有变量。

+0

有趣!但是,它似乎总是不起作用(ref:https://github.com/facebook/relay/issues/1046#issuecomment-241073226)。 –

相关问题