2

I would like write a mutation updater that unlinks a linked record from another record.

For example, suppose I have the following in my store:

client:root {
  user: {__ref: "someid"}
}

I am looking for a way to unlink user and client:root. user is a nullable field. I would like subscribers that are interested in the user field to see it has gone null.

Right now the only way I am seeing to unlink this field is to delete the node from the store. I've tried setting the link to null using setLinkedRecord but that throws an error.

Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160

2 Answers2

1

.setLinkedRecored() expects the first argument to be an instance of RecordProxy. Use .setValue() for unlinking nodes, as whitep4nther commented above.

store.getRoot().setValue(undefined, 'user');
smagch
  • 450
  • 1
  • 5
  • 7
  • 1
    Hey, thanks for adding this answer. I must have misread @whitep4nther's comments when he left them because what you're saying makes sense. I'll try this out and upvote/accept next time I get a chance. – Dmitry Minkovsky Mar 22 '18 at 14:02
  • This doesn't work. Setting a linked field with to `undefined` with `setValue()` doesn't alter the store. – Dmitry Minkovsky Mar 27 '18 at 19:30
  • It's strange. In my use case, setting `null` or `undefined` in `commitLocalUpdate` callback works without any issue. – smagch Mar 30 '18 at 05:55
  • Yeah setting it `null` works. But then the record is linked, just null – Dmitry Minkovsky Mar 30 '18 at 12:40
  • I added an example app for simple demonstration of unlinking records. https://github.com/smagch/relay-example-unlinking-records You can see working example: https://smagch.github.io/relay-example-unlinking-records/ – smagch Mar 30 '18 at 17:01
0

I was just fighting with this as well, and after digging through the relay codebase, I decided there isn't an appropriate way to go about this right now. I've opened an issue here.

Mike Marcacci
  • 1,913
  • 1
  • 18
  • 24