2017-06-17 51 views
0

使用@include指示我有使用@include(if: $variable)有条件地提取一些领域的继电器容器,但我写的每一个人领域的指令:为多个字段

const relayOptions = { 
    initialVariables: { 
    expandDetails: false, 
    }, 
    fragments: { 
    company: Relay.QL` 
     fragment on Company { 
     id 
     name 
     employees @include(if: $expandDetails) { 
      fullName 
     } 
     admins @include(if: $expandDetails) { 
      fullName 
     } 
     departments @include(if: $expandDetails) { 
      name 
     } 
     } 
    `, 
    }, 
} 

有什么办法,我可以写指令只有一次,例如像这样(我知道这个代码将无法正常工作):

const relayOptions = { 
    initialVariables: { 
    expandDetails: false, 
    }, 
    fragments: { 
    company: Relay.QL` 
     fragment on Company { 
     id 
     name 
     @include(if: $expandDetails) { 
      employees { 
      fullName 
      } 
      admins { 
      fullName 
      } 
      departments { 
      name 
      } 
     } 
     } 
    `, 
    }, 
} 

我尝试了一些其他的疯狂方式,但他们没有工作,我得到这个错误:

'@include' can't be applied to fragment definitions (allowed: fields, fragment spreads, inline fragments)

编辑:注:我需要这样做,以避免不必要的数据提取。如果展开数据的可折叠组件已扩展(我打算在安装组件后使用setVariables进行此操作),我只想抓取数据。 在这个例子中,只有3个条件字段,但在实际情况下,我试图解决的问题还有很多。

回答

1

你可以把内嵌片段的指令,并且只使用相同类型的外场:

fragment on Company { 
    ... on Company @include(if: $expandDetails) { 
    employees { fullName } 
    admins { fullName } 
    departments { fullName } 
    } 
}