单个对象的多个突变

Multiple Mutations on a single object

我尝试在 Chart 组件上进行 addColumnremoveColumn 突变。

但是当我打电话时

Relay.Store.commitUpdate(new RemoveChartColumnMutation({
  chart: this.props.viewer.chart,
  column: this.props.viewer.chart.columns[0]
})

我收到这个错误

"Fragment \"F2\" cannot be spread here as objects of type "AddChartColumnPayload\" can never be of type \"RemoveChartColumnPayload\"."

我做错了什么?

export default Relay.createContainer(Home, {
  fragments: {
    viewer: () => Relay.QL`
      fragment on User {
        chart {
          columns
          ${AddChartColumnMutation.getFragment('chart')}
          ${RemoveChartColumnMutation.getFragment('chart')}
        }
      }`
  }
});

有了这些突变

class AddChartColumnMutation extends Relay.Mutation {
  getMutation() {
    return Relay.QL`mutation {addChartColumn}`;
  }
  getVariables() {
    return {
      id: this.props.chart.id,
      key: this.props.key,
      aggregation: this.props.aggregation
    };
  }
  getFatQuery() {
    return Relay.QL`
      fragment on AddChartColumnPayload {
        chart {
          columns
        }
      }
    `;
  }
  getConfigs() {
    return [{
      type: 'FIELDS_CHANGE',
      fieldIDs: {
        chart: this.props.chart.id
      }
    }];
  }
  static fragments = {
    chart: () => Relay.QL`
      fragment on Chart {
        id
        type
      }
    `
  };
}

class RemoveChartColumnMutation extends Relay.Mutation {
  getMutation() {
    return Relay.QL`mutation {addChartColumn}`;
  }
  getVariables() {
    return {
      chartId: this.props.chart.id,
      columnId: this.props.column.id
    };
  }
  getFatQuery() {
    return Relay.QL`
      fragment on RemoveChartColumnPayload {
        chart {
          columns
        }
      }
    `;
  }
  getConfigs() {
    return [{
      type: 'FIELDS_CHANGE',
      fieldIDs: {
        chart: this.props.chart.id
      }
    }];
  }
  static fragments = {
    chart: () => Relay.QL`
      fragment on Chart {
        id
        type
      }
    `
  };
}

我认为您的代码中有一个拼写错误。看起来两个突变都在调用 addChartColumn 突变,这将解释期望删除图表列有效负载的错误。