Jason Waldrip
Jason Waldrip

Reputation: 5148

Root Query Create and Delete in Relay.js

I am trying to figure out how to create and delete nodes with Relay where I don't have a parent node. It seems that NODE_DELETE/RANGE_DELETE and RANGE_ADD all require a parent node. Is there a way to perform create and delete mutations from the root query object in Relay.js?

Note: I did find example where creates can be performed with a FIELDS_CHANGE query, but they lack any documentation or reason.

Upvotes: 3

Views: 1091

Answers (2)

Scimonster
Scimonster

Reputation: 33409

You can pass client:root as the parentID. And then your pathToConnection would be ['client:root', 'someConnection'].

(Tested with Relay Modern. Not sure if this also applies to Relay Classic, but that's officially deprecated now anyways. But this is still one of the top Google results for this issue, so answering.)

(Found in this GitHub issue)

Upvotes: 1

Greg Hurrell
Greg Hurrell

Reputation: 5437

You should be able to use REQUIRED_CHILDREN for this purpose. It's not currently well-documented (or even documented), and it has a somewhat confusing name (as a result, we have a task for renaming it and improving the docs). It will likely be renamed to EXTRA_FRAGMENT in the future.

Normally when you issue a mutation, we perform an intersection between the "fat query" (all the fields that could possibly change as the result of the mutation) and the "tracked query" (all the fields that your app has requested for a node so far, and which should be updated when they change) and we send this query to the server with the mutation.

So, for the use case of creating an entirely new node with no parent, you can specify an identifying field like id in the REQUIRED_CHILDREN, and then use that to, for example, navigate to a view showing the newly-created object. This answer has a very detailed example of how you would do this.

Upvotes: 3

Related Questions