2
votes

Say I have a GraphQL model that has People and Parties.

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.

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?

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. How do I ensure that all the people in the party in the Payload are invalidated?

1
This isn't really an answer to the question, but I managed to do a workaround by using react-relay-router and ensuring that data that changed was always in a different route, then turning on forceFetch so relay always got the latest data on route changes. - Dylan

1 Answers

0
votes

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. 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. If you're needing to remove somebody from a party you can look at 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.

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.