2017-06-17 3 views
0

私は条件付きで@include(if: $variable)を使用していくつかのフィールドを取得した中継コンテナを持っているが、私は、個々のフィールドのディレクティブを記述する必要があります。つ以上のフィールドに使用@includeディレクティブ

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 } 
    } 
} 
関連する問題