简体   繁体   English

从React Relay Mutation更新多个节点

[英]Updating multiple nodes from a React Relay Mutation

Say I have a GraphQL model that has People and Parties . 说我有,有一个GraphQL模型PeopleParties

Something like this: 像这样:

Person {
   name: String
   parties: PartyConnection
}

Party {
   place: String
   dateTime: String
   people: PeopleConnection
}

I can look at a Person and see what Parties that they are going to, which then gets cached by Relay. 我可以看一个Person ,看看他们要去的Parties ,然后由Relay缓存。

I can also do a mutation to create a new party, adding some people to it. 我也可以做一个变体来创建一个新的聚会,增加一些人。 How do I ensure that everyone I have added to the party is invalidated in the cache so that when I navigate to the person's page, I see them in the party I just added. 如何确保我添加到聚会的每个人在缓存中都无效,因此当我导航到该人的页面时,我会在刚添加的聚会中看到他们。

The mutation's payload looks something like: 突变的有效载荷看起来像:

fragment on CreatePartyMutationPayload {
    party
    partyMembers - not sure what type this should be, an array of ids?
}

what getConfigs and/or payload structure would I need to tell Relay that every person in the partyMembers array needs to have their parties field in the cache invalidated? 我需要告诉什么样的getConfigs和/或有效负载结构,partyMembers数组中的每个人都需要使缓存中的party字段无效?

Also, it may be that there are some server side rules where if I invite one person, their partner is also invited automatically. 另外,可能有一些服务器端规则,如果我邀请一个人,也会自动邀请他们的伙伴。 This means that I don't have a definitive list of the Person ids that will be invalidated in the client side mutation object until the payload comes back. 这意味着我没有一个确定的Person ID列表,该列表将在客户端突变对象中失效,直到有效载荷返回为止。 How do I ensure that all the people in the party in the Payload are invalidated? 如何确保有效载荷中所有参加聚会的人都无效?

Can a person only go to one party, or can a person be going to many parties? 一个人只能参加一个派对,还是一个人可以参加多个派对? I ask that because you use the word invalidation. 我之所以这样问是因为您使用了无效一词。 If a person could be attending multiple parties then I don't think invalidation is needed. 如果一个人可以参加多个聚会,那么我认为不需要无效。 If they can only be going to one party, then you may need to make sure you adjust the connections as necessary. 如果他们只能参加一个聚会,则可能需要确保根据需要调整连接。

You can use RANGE_ADD to add the new party to party connections. 您可以使用RANGE_ADD将新参与方添加到参与方连接中。 If you're up for the more targeted, manual approach, you can also use RANGE_ADD to add the people to their respective party connections. 如果您希望采用更具针对性的手动方法,则还可以使用RANGE_ADD将人员添加到其各自的聚会联系中。 If you're needing to remove somebody from a party you can look at NODE_DELETE . 如果您需要从聚会中移除某人,可以查看NODE_DELETE If you don't quite know who or what to change, but you know something about the party attendees has changed, you can go for the "refetch it all" approach and use FIELDS_CHANGE . 如果您不太了解要更改的人或内容,但是您知道与会参与者的某些更改,则可以采用“全部获取”方法并使用FIELDS_CHANGE

If your server-side logic is complex and does a lot of mutating there, you can just use FIELDS_CHANGE to refetch it all, catching any changes, or you can try to structure a mutation payload so that it properly reports all of the changes. 如果您的服务器端逻辑很复杂并且在那里进行了很多更改,则可以只使用FIELDS_CHANGE来重新获取所有内容,以捕获任何更改,或者可以尝试构造一个更改有效负载,以便其正确报告所有更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM