Reactjs 使用必需的\u子配置时中继乐观响应
我有一个突变,看起来是这样的:Reactjs 使用必需的\u子配置时中继乐观响应,reactjs,relayjs,Reactjs,Relayjs,我有一个突变,看起来是这样的: export default class CreateTaskMutation extends Relay.Mutation { //noinspection JSUnusedGlobalSymbols getMutation() { return Relay.QL` mutation { createTaskMutation } `;
export default class CreateTaskMutation extends Relay.Mutation {
//noinspection JSUnusedGlobalSymbols
getMutation() {
return Relay.QL`
mutation {
createTaskMutation
}
`;
}
//noinspection JSUnusedGlobalSymbols
getCollisionKey() {
return `check_${this.props.id}`;
}
//noinspection JSMethodCanBeStatic,JSUnusedGlobalSymbols
getFatQuery() {
return Relay.QL`
fragment on CreateTaskMutationPayload {
savedTask {
_id,
isPublic,
isDone,
taskDescription {
description
}
}
}
`;
}
// noinspection JSUnusedGlobalSymbols
getConfigs() {
return [{
type: 'REQUIRED_CHILDREN',
children: [
Relay.QL`
fragment on CreateTaskMutationPayload {
savedTask {
_id,
isPublic,
isDone,
taskDescription {
description
}
}
}
`
]
}];
}
//noinspection JSUnusedGlobalSymbols
getVariables() {
return {
cookie: this.props.cookie,
owner: this.props.owner,
taskDescription: this.props.taskDescription,
isPublic: this.props.isPublic,
isDone: this.props.isDone
};
}
//noinspection JSUnusedGlobalSymbols
getOptimisticResponse() {
return {
savedTask: {
owner: this.props.owner,
taskDescription: this.props.taskDescription,
isPublic: this.props.isPublic,
isDone: this.props.isDone
}
};
}
}
我是这样使用它的:
Relay.Store.commitUpdate(
new CreateTaskMutation({
cookie: this.state.cookie,
owner: this.state.id,
taskDescription: {description: this.state.taskDescriptionInput},
isPublic: this.state.isTaskPublic,
isDone: this.state.isTaskDone
}),
{
onSuccess: (response) => {
const newArray = this.state.newlyAddedTasks;
newArray.push(response.createTaskMutation.savedTask);
this.setState({
newlyAddedTasks: newArray
});
}
}
);
现在,当您使用类似于FIELDS\u CHANGED的配置时,我可以执行this.props.relay.haspoptimisticupdates()
,但由于我正在创建
中继存储还不知道的一些事情,没有数据ID供它查找。因此,我目前的解决方案是使用两个任务源,中继片段响应和一个在成功时更新的状态
<CreateTaskRequestBox
tasks={this.props.currentUserFragment.userField.tasks}
newTasks={this.state.newlyAddedTasks}
/>
有没有一种简洁的方法可以让我在中继存储中输入一个乐观的响应,并将其更新,就像我使用了FIELDS\u CHANGED config一样?我目前的解决方案似乎有点老套,也不乐观。那么,你想让这种变异实际上修改图表,而不仅仅是返回要暂时使用的有效负载?然后使用一个记录的变异配置:
FIELDS\u CHANGE
和RANGE\u ADD
将变异本地图中的连接,并相应地更新视图REQUIRED_CHILDREN
明确不适用于不应实际影响局部图形的突变,并且没有文档记录,因此我不确定您为什么要使用它……:)
要使用FIELDS\u CHANGE
,请更新有效负载以包括父节点(在您的示例中类似于userField
),然后只需对userField{tasks}
进行胖查询
要使用
RANGE\u ADD
,请更新您的有效负载,使其也包括新的任务边缘(例如savedTaskEdge{cursor,node}
),并在突变配置中引用该边缘。此处提供了所需子任务的文档:啊哈,好的,它已添加到文档中。无论如何,这并不意味着你在做什么:“作为必需的_CHILDREN配置的结果获取的数据不会写入客户端存储”