当我尝试提交删除突变时遇到问题。当我提交时,我收到错误Uncaught Invariant Violation: RelayMutationQuery: Invalid field name on fat query, `company`.
。查看、创建和更新节点都可以。由于某种原因我无法删除。它提到 fatQuery 中的公司字段,但 fat 查询中唯一的字段是从服务器返回的deletedUserId。提前致谢!
组件:
import React, {Component} from 'react';
import Relay from 'react-relay';
import {Link} from 'react-router';
import DeleteUserMutation from 'mutations/DeleteUserMutation';
import styles from './EmployeeItem.css';
class EmployeeItem extends Component {
render() {
const {user} = this.props;
return (
<div className={styles.employee}>
<p><strong>ID:</strong> {user.id}</p>
<p><strong>First Name:</strong> {user.firstName}</p>
<p><strong>Last Name:</strong> {user.lastName}</p>
<p><strong>Email:</strong> {user.email}</p>
<div className="btn-group">
<Link to={`/company/employees/${user.id}`} className="btn btn-primary">View Employee</Link>
<button onClick={this.handleRemove} className="btn btn-danger">Delete User</button>
</div>
</div>
)
}
handleRemove = (e) => {
e.preventDefault();
const {user, company} = this.props;
Relay.Store.commitUpdate(new DeleteUserMutation({user, company}));
};
}
export default Relay.createContainer(EmployeeItem, {
fragments: {
company: () => Relay.QL`
fragment on Company {
id
${DeleteUserMutation.getFragment('company')}
}
`,
user: () => Relay.QL`
fragment on User {
id
firstName
lastName
email
${DeleteUserMutation.getFragment('user')}
}
`
}
});
突变:
import React from 'react';
import Relay from 'react-relay';
export default class DeleteUserMutation extends Relay.Mutation {
static fragments = {
company: () => Relay.QL`
fragment on Company {
id
}
`,
user: () => Relay.QL`
fragment on User {
id
}
`
};
getMutation() {
return Relay.QL`mutation {deleteUser}`;
}
getFatQuery() {
return Relay.QL`
fragment on DeleteUserPayload {
deletedUserId
}
`;
}
getVariables() {
return {
id: this.props.user.id,
}
}
getConfigs() {
return [{
type: 'NODE_DELETE',
parentName: 'company',
parentID: this.props.company.id,
connectionName: 'employees',
deletedIDFieldName: 'deletedUserId'
}]
}
// Wasn't sure if this was causing the error but it appears to be
// something else.
// getOptimisticResponse() {
// return {
// deletedUserId: this.props.user.id
// }
// }
}
最佳答案
此错误是指您在 getConfigs()
实现中引用了“公司”。 NODE_DELETE
配置告诉 Relay 如何通过将存储中的节点(例如 parentID
)映射到 fat 查询上的字段(例如 parentName
)来构建突变查询>).
虽然您今天可能不一定需要它,但您应该将公司
添加到此处的突变负载和胖查询中,因为该公司受到此更改的影响。更具体地说,正在修改公司的员工连接:)
关于javascript - 删除时中继错误: RelayMutationQuery: Invalid field name on fat query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35165693/