【问题标题】:In a GraphQL/Relay mutation that creates a model, is there a way to get back the model ID?在创建模型的 GraphQL/Relay 突变中,有没有办法取回模型 ID?
【发布时间】:2016-01-31 09:40:45
【问题描述】:

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

我们有一个 Relay 突变,可以在数据库中创建一个新模型:

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

但是,由于当前没有任何组件查询 RANGE_ADD (viewer { campaigns }) 中指定的范围,因此 Relay 会智能地从 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 去获取那个边?我是否正确配置了这个突变?

【问题讨论】:

  • 为什么不直接在payload中返回呢?
  • @ChrisMartin Relay 控制负载。我需要弄清楚如何控制 Relay。

标签: relayjs


【解决方案1】:

您可以在这种情况下使用REQUIRED_CHILDREN。详情请见https://github.com/facebook/relay/issues/237https://github.com/facebook/relay/issues/236

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

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2019-11-10
    • 1970-01-01
    • 2022-06-17
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多