单个对象上的多个突变

时间:2016-03-01 02:01:10

标签: relayjs graphql-js

我尝试在addColumn组件上发现removeColumnChart突变。

但是当我打电话时

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
      }
    `
  };
}

1 个答案:

答案 0 :(得分:2)

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