Search code examples
reactjsgraphqlrelayjs

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.


Solution

  • 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.