Search code examples
graphqlrelayjsgraphql-js

How do I add a Root Node


Adding a nested item is easy: the config is just a RANGE_ADD

query {
  viewer {
    item {
      edges {
        node {
          subitem {
            edges {
              node {
                id,
                title
              }
            }
          }
        }
      }
    }
  }
}

Adding a subitem will just require a RANGE_ADD with the parentId equal to the id of item.

However, how do I add an item which doesn't have a parent? I tried using a RANGE_ADD config without a parentId but it didn't work.

I feel like this is such a simple thing, but I can't seem to find it in the docs.


Solution

  • item does have a parent, which is viewer. It's just that viewer can have one item instead of multiple items as item is not a connection type. So when you want to add another item, you want to either:

    1) replace / update the existing item. FIELDS_CHANGE is usually used in this case.

    OR

    2) fetch an item which does not need to be put in the client store. In this case, you can use REQUIRED_CHILDREN mutator configuration. Check an excellent example of how to use it in an answer to another related question.