React Relay:复杂变异脂肪查询

React Relay: Complex mutation fat query

我正在创建一个可以投票的投票应用程序。我目前坚持投票(本质上是创建投票)。

我的架构:

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

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. (仅当查看者也是投票的创建者,这是可能的):在查看者下的投票连接中的某个投票下的投票连接中添加投票。

所以我的问题是我应该如何在我的胖查询中表达这个,这就足够了吗?

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,
        }
      }
    `;
  }

谢谢!

看来你的想法是对的。在 Relay 中定义 FatQuery 时,您应该尽可能保持 return 字段的最佳状态,因为这是 GraphQL 的好处(您不需要 return 多于您将在客户端中使用的字段)。所以你的 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
          }
        }
      }
    `;
  }

这个想法是,您可以在技术上再次 return votes 作为 polls 的子选择;但是,没有必要,因为您 return 已经在查看器下了。这将为您提供系统中的总投票数和所有投票数。如果你想通过民意调查过滤投票和投票计数,那么你可以做相反的事情,看起来像这样:

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
            }
          }
        }
      }
    `;
  }

希望对您有所帮助!