relay icon indicating copy to clipboard operation
relay copied to clipboard

Refetching data in multiple parts of the code at the same time

Open ruben-kasaz opened this issue 7 years ago • 7 comments

My problem is : I have a component named HeaderComponent with some props coming from graphql. This component is used in two pages of my app.

In the HeaderComponent in page (1), I modify one of the props by doing a mutation. Then I refetch the new data. In the HeaderComponent in page (2), I want to have the new data that was modify in page (1)

How can I do that ?

Because I try some stuffs but it didn't work. Thanks !

ruben-kasaz avatar Nov 29 '19 11:11 ruben-kasaz

If I understand you correctly, this should "just work" as is. Since it seems like it doesn't, please post your code and we'll see if we can spot what's wrong.

zth avatar Nov 29 '19 12:11 zth

You have to think in a different way when using Relay

You should return everything from your output fields of your mutation to update your frontend

If you are just editting a node, relay will take care to update the store based on the global id of this node

If you adding or removing an edge from a connection you need to use configs or updater to properly update relay store

So your queries/mutations/subscriptions updates the relay store, then relay will update all components that subscribed (using fragments) to this data

sibelius avatar Nov 29 '19 12:11 sibelius

Thanks! So I started to use updater to update my Relay store. But I have some issues.

First of all this is my fragment container :

Capture d’écran 2019-11-29 à 18 04 19

What I want, It's to add a new favorite in my favorites's list. So I need to create a new favorite. This is my mutation :

Capture d’écran 2019-11-29 à 18 04 42

The mutation returns the new favorite with its ID.

When the query is completed, I want to update my Relay Store, so that the new data appears in the view. This is the updater function :

Capture d’écran 2019-11-29 à 18 04 52

I thought I did good, but the Relay Store does not update. Do you have an idea on what I am doing wrong ?

Thanks for your time!

ruben-kasaz avatar Nov 29 '19 17:11 ruben-kasaz

Mutations are:

  • Write, then
  • Read

Just ask for the fields of favorites on mutation and Relay will update it automatically

renanmav avatar Nov 29 '19 17:11 renanmav

@ruben-kasaz I think it is because you're not passing the arguments to setLinkedRecords. Try passing the same arguments (third parameter) to that as you're doing to getLinkedRecords.

zth avatar Nov 29 '19 18:11 zth

I was able to produce exactly what you are describing - having a component reused in multiple places updated following a mutation. The key to that is making proper use of Relay store with mutations.

Relay's experimental docs sample project helped with that a ton (note: that app was posting comments to the actual repo issues discussion!): https://github.com/relayjs/relay-examples/tree/master/issue-tracker

ruslanvs avatar Dec 02 '19 03:12 ruslanvs

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 25 '20 16:12 stale[bot]