Search code examples
relaymutation

Why doesn't Relay's NODE_DELETE update the list of remaining Nodes


I've got a NODE_DELETE Mutation that removes a Category from the list of categories. Even though it provides the correct deletedIDFieldname, the list of categories doesn't get updated until I refresh the page.

Do I miss something here? Thanks for any hint!

import Relay from 'react-relay';

export default class DeleteCategoryMutation extends Relay.Mutation {

  getMutation() {
    return Relay.QL`mutation{deleteCategory}`;
  }
  getFatQuery() {
    return Relay.QL`
      fragment on DeleteCategoryPayload {
        category {
          id
        },
        viewer {
          allCategories
        }
      }
    `;
  }
  getConfigs() {
    return [{
      type: 'NODE_DELETE',
      parentName: 'viewer',
      parentID: this.props.viewer.id,
      connectionName: 'allCategories',
      deletedIDFieldName: 'category{id}',
    }];
  }
  getVariables() {
    return {
      id: this.props.categoryId,
    };
  }
  getOptimisticResponse () {
    return {
      deletedId: this.props.categoryId,
      viewer: this.props.viewer.id,
    }
  }
}

Solution

  • I think you need expand your getFatQuery a little further like the following.

     getFatQuery() {
        return Relay.QL`
          fragment on DeleteCategoryPayload {
            category {
              id
            },
            viewer {
              allCategories(first: 1000) {
                edges
              }
            }
          }
        `;
     }
    

    Hope this helps. But I agree that this is somewhat tricky and Relay should help you in situations like these...