在创建模型的 GraphQL/Relay 突变中,有没有办法取回模型 ID?

In a GraphQL/Relay mutation that creates a model, is there a way to get back the model ID?

我们正在新项目中使用 Relay 和 GraphQL。

我们有一个在数据库中创建新模型的中继突变:

export default class AddCampaignMutation extends Relay.Mutation {
    getMutation() {
        return Relay.QL`mutation { addCampaign }`;
    }
    getVariables() {
        return {
            type: this.props.campaignType
        };
    }
    getFatQuery() {
        return Relay.QL`
            fragment on AddCampaignPayload {
                campaignEdge
                viewer
            }
        `;
    }
    getConfigs() {
        return [{
            type: 'RANGE_ADD',
            parentName: 'viewer',
            parentID: this.props.viewer.id,
            connectionName: 'campaigns',
            edgeName: 'campaignEdge',
            rangeBehaviors: {
              '': 'append',
            },
        }];
    }
    static fragments = {
        viewer: () => Relay.QL`
            fragment on User {
                id
            }
        `,
    };
}

但是,由于 none 个组件当前正在查询 RANGE_ADD (viewer { campaigns }) 中指定的范围,中继会智能地从 AddCampaignPayload 中排除该查询。

这会导致控制台警告:

Warning: writeRelayUpdatePayload(): Expected response payload to include the newly created edge `campaignEdge` and its `node` field. Did you forget to update the `RANGE_ADD` mutation config?

我真的很想取回新创建模型的 ID,以便我可以将客户端导航到它。比如拿回新的campaignEdge,我想把客户端发到/campaigns/${campaignEdge.node.id}.

有什么方法可以告诉 Relay 获取那个边吗?我是否正确配置了这个突变?

您可以在此上下文中使用 REQUIRED_CHILDREN。有关详细信息,请参阅 https://github.com/facebook/relay/issues/237 and https://github.com/facebook/relay/issues/236

REQUIRED_CHILDREN 允许您为此模式指定额外的数据依赖性。