【发布时间】:2018-03-25 14:03:31
【问题描述】:
我使用react-table 和GraphQL query 将所有帐户显示到表格中。现在我想使用删除突变来删除 1 个帐户。但是,我不知道如何找到该行的索引/帐户id,以便我可以删除该行!
我的用户界面是这样的:
ShowAccount.js
import React from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { Button, Icon } from 'semantic-ui-react';
import ReactTable from 'react-table';
import DeleteAccountWithData from './DeleteAccount'
const columns = [
{
Header: 'ADMIN',
columns: [
{
Header: 'Id',
accessor: 'id',
width: 70,
},
{
Header: 'First Name',
accessor: 'firstName',
},
{
Header: 'Last Name',
accessor: 'lastName',
},
{
Header: 'Address',
accessor: 'address',
width: 450,
},
{
Header: 'Email',
accessor: 'email',
width: 250,
},
{
Header: 'Phone',
accessor: 'mobile',
},
],
},
];
class ShowAccount extends React.Component {
render() {
const { data } = this.props;
return (
<div>
<ReactTable
data={data.GetAllAccounts}
columns={columns}
defaultPageSize={10}
className="-highlight"
SubComponent={row =>
<div style={{ padding: '10px' }}>
<DeleteAccountWithData />
</div>}
/>
</div>
);
}
}
export const queryAccountList = gql`
query GetAllAccounts {
GetAllAccounts {
id
firstName
lastName
address
email
mobile
}
}
`;
const AccountListWithData = graphql(queryAccountList)(ShowAccount);
export default AccountListWithData;
删除帐户.js
import React from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { queryAccountList } from './ShowAccount'
class DeleteAccount extends React.Component {
constructor(props) {
super(props);
}
onDeleteAccount = () => {
this.props
.mutate({
refetchQueries: [{
query: queryAccountList
}],
variables: {
id: 2 // my problem here: just delete row with id 2
},
})
.then(({ data }) => {
console.log('mutation deleteAccount: SUCCESS', data);
})
.catch(error => {
console.log('mutation deleteAccount: ERROR', error);
});
}
render() {
return (
<div>
<button onClick={this.onDeleteAccount}>Delete</button>
</div>
);
}
}
const mutationDeleteAccount = gql`
mutation deleteAccount($id: Int) {
deleteAccount(id: $id) {
id
}
}
`;
const DeleteAccountWithData = graphql(mutationDeleteAccount)(DeleteAccount);
export default DeleteAccountWithData;
【问题讨论】:
标签: reactjs graphql apollo-client