React Relay:复杂变异脂肪查询

时间:2016-07-04 21:10:35

标签: relayjs relay

我正在创建一个投票应用程序,其中包含可以投票的民意调查。我目前坚持投票进行投票(基本上是投票)。

我的架构:

(长话短说:民意调查可以从主商店访问,但观众的民意调查和投票可以直接由他(作为字段)访问。投票也可以从民商店访问,但民意调查&# 39;投票也可以直接访问。

query {
  store {
    polls { //poll connection
      edges {
        node { //poll
          votes { //vote connection
            ...
          }
        }
      }
    }
    votes { //vote connection
      edges {
        node { //vote
          ...
        }
      }
    }
  }
  viewer {
   polls { //poll connection
      edges {
        node { //poll
          votes { //vote connection
            ...
          }
        }
      }
    }
    votes { //vote connection
      edges {
        node { //vote
          ...
        }
      }
    }
  }
}

这个复杂的架构让我对如何定义胖查询感到困惑,因为它应该定义所有可能正在改变的内容。

以下是可以改变的内容:

  1. 创建投票(因此胖查询中的voteEdge)。
  2. 投票将添加到商店下的投票连接中。
  3. 投票将添加到查看器下的投票连接中。
  4. 在商店内的民意调查连接中的某个民意调查下,投票会被添加到投票连接中。
  5. (仅当观众也是民意调查的创建者时,这是可能的):在观众的民意调查连接中的某个民意调查下,投票连接被添加到投票连接中。
  6. 所以我的问题是如何在我的胖查询中表达这一点,这是否足够?

    getFatQuery() {
        return Relay.QL`
          fragment on CreateVoteMutation {
            voteEdge,
            viewer{
              polls,
              voteCount
              votes,
            },
            store{
              polls,
              voteCount,
              votes,
            }
          }
        `;
      }
    

    或者我应该以某种方式在民意调查中包括投票?

    getFatQuery() {
        return Relay.QL`
          fragment on CreateVoteMutation {
            voteEdge,
            viewer{
              polls{
                edges{
                  node{
                    voteCount,
                    votes
                  }
                }
              },
              voteCount
              votes,
            },
            store{
              polls{
                edges{
                  node{
                    voteCount,
                    votes
                  }
                }
              },
              voteCount,
              votes,
            }
          }
        `;
      }
    

    谢谢!

1 个答案:

答案 0 :(得分:1)

看起来你有正确的想法。在Relay中定义FatQuery时,您应该尽可能保持返回字段的最佳状态,因为这是GraphQL的好处(您不需要返回比在客户端中使用的更多)。所以你的FatQuery应该是这样的:

getFatQuery() {
    return Relay.QL`
      fragment on CreateVoteMutation {
        voteEdge,
        viewer{
          polls { // this is most likely a GraphQL object type, so you'll need a sub-selection, but you don't necessarily need to return all the Votes again since you do that later
            name
            timestamp
          }
          voteCount // scalar integer
          votes { // again, here you'll need to return a sub-selection since Votes is a GraphQL object type
            name
            timestamp
          }
        },
        store{
          polls { // this is most likely a GraphQL object type, so you'll need a sub-selection, but you don't necessarily need to return all the Votes again since you do that later
            name
            timestamp
          }
          voteCount // scalar integer
          votes { // again, here you'll need to return a sub-selection since Votes is a GraphQL object type
            name
            timestamp
          }
        }
      }
    `;
  }

我们的想法是,您可以在技术上再次将votes作为polls的子选项返回;但是,没有必要,因为你已经回到了观众面前。这将为您提供系统中的totalCount和所有投票。如果你想通过民意调查来过滤投票和投票,那么你可以做相反的事情,看起来像这样:

getFatQuery() {
    return Relay.QL`
      fragment on CreateVoteMutation {
        voteEdge,
        viewer{
          polls { // this is most likely a GraphQL object type, so you'll need a sub-selection, but you don't necessarily need to return all the Votes again since you do that later
            name
            timestamp
            voteCount // scalar integer
            votes { // again, here you'll need to return a sub-selection since Votes is a GraphQL object type
                name
                timestamp
            }
          }
        },
        store{
          polls { // this is most likely a GraphQL object type, so you'll need a sub-selection, but you don't necessarily need to return all the Votes again since you do that later
            name
            timestamp
            voteCount // scalar integer
            votes { // again, here you'll need to return a sub-selection since Votes is a GraphQL object type
              name
              timestamp
            }
          }
        }
      }
    `;
  }

希望这有帮助!