如何从突变中获取新对象的 ID?
How to get the ID of a new object from a mutation?
我有一个 createObject
突变,returns 新对象的 ID。
之后 returns 我想重定向到有关新对象的详细信息页面。
如何使用 react/relay 从包含组件中的突变中获取响应字段?
例如我的 createObject
页面包含代码如下的突变:
var onFailure = (transaction) => {
};
var onSuccess = () => {
redirectTo('/thing/${newthing.id}'); // how can I get this ID?
};
// To perform a mutation, pass an instance of one to `Relay.Store.update`
Relay.Store.update(new AddThingMutation({
userId: this.props.userId,
title: this.refs.title.value,
}), { onFailure, onSuccess });
}
newthing
应该是mutation创建的object,但是怎么才能拿到呢?
通常我们会使用 RANGE_ADD
和 return 配置变更的服务器端的新 thingEdge
,但在这里你没有客户端上要添加新节点的范围。要告诉 Relay 获取任意字段,请使用 REQUIRED_CHILDREN
配置。
服务器端突变
var AddThingMutation = mutationWithClientMutationId({
/* ... */
outputFields: {
newThingId: {
type: GraphQLID,
// First argument: post-mutation 'payload'
resolve: ({thing}) => thing.id,
},
},
mutateAndGetPayload: ({userId, title}) => {
var thing = createThing(userId, title);
// Return the 'payload' here
return {thing};
},
/* ... */
});
客户端突变
class AddThingMutation extends Relay.Mutation {
/* ... */
getConfigs() {
return [{
type: 'REQUIRED_CHILDREN',
// Forces these fragments to be included in the query
children: [Relay.QL`
fragment on AddThingPayload {
newThingId
}
`],
}];
}
/* ... */
}
用法示例
var onFailure = (transaction) => {
// ...
};
var onSuccess = (response) => {
var {newThingId} = response.addThing;
redirectTo(`/thing/${newThingId}`);
};
Relay.Store.update(
new AddThingMutation({
title: this.refs.title.value,
userId: this.props.userId,
}),
{onSuccess, onFailure}
);
请注意,您使用此技术查询的任何字段都可用于 onSuccess
回调,但 不会 添加到客户端存储。
我有一个 createObject
突变,returns 新对象的 ID。
之后 returns 我想重定向到有关新对象的详细信息页面。
如何使用 react/relay 从包含组件中的突变中获取响应字段?
例如我的 createObject
页面包含代码如下的突变:
var onFailure = (transaction) => {
};
var onSuccess = () => {
redirectTo('/thing/${newthing.id}'); // how can I get this ID?
};
// To perform a mutation, pass an instance of one to `Relay.Store.update`
Relay.Store.update(new AddThingMutation({
userId: this.props.userId,
title: this.refs.title.value,
}), { onFailure, onSuccess });
}
newthing
应该是mutation创建的object,但是怎么才能拿到呢?
通常我们会使用 RANGE_ADD
和 return 配置变更的服务器端的新 thingEdge
,但在这里你没有客户端上要添加新节点的范围。要告诉 Relay 获取任意字段,请使用 REQUIRED_CHILDREN
配置。
服务器端突变
var AddThingMutation = mutationWithClientMutationId({
/* ... */
outputFields: {
newThingId: {
type: GraphQLID,
// First argument: post-mutation 'payload'
resolve: ({thing}) => thing.id,
},
},
mutateAndGetPayload: ({userId, title}) => {
var thing = createThing(userId, title);
// Return the 'payload' here
return {thing};
},
/* ... */
});
客户端突变
class AddThingMutation extends Relay.Mutation {
/* ... */
getConfigs() {
return [{
type: 'REQUIRED_CHILDREN',
// Forces these fragments to be included in the query
children: [Relay.QL`
fragment on AddThingPayload {
newThingId
}
`],
}];
}
/* ... */
}
用法示例
var onFailure = (transaction) => {
// ...
};
var onSuccess = (response) => {
var {newThingId} = response.addThing;
redirectTo(`/thing/${newThingId}`);
};
Relay.Store.update(
new AddThingMutation({
title: this.refs.title.value,
userId: this.props.userId,
}),
{onSuccess, onFailure}
);
请注意,您使用此技术查询的任何字段都可用于 onSuccess
回调,但 不会 添加到客户端存储。