如何解决无法通过 Apollo 客户端使用突变删除 Mongo 文档
我在使用 Apollo Client 删除 MongoDB 文档时遇到问题。我不认为我的查询语法是原因,因为我在 Graphiql 中测试了查询并且它工作正常。这是我得到的错误:
Unhandled Runtime Error
Error: Response not successful: Received status code 500
Call Stack
new ApolloError
node_modules/@apollo/client/errors/index.js (26:0)
Object.error
node_modules/@apollo/client/core/QueryManager.js (127:0)
notifySubscription
node_modules/zen-observable/lib/Observable.js (140:0)
onNotify
node_modules/zen-observable/lib/Observable.js (179:0)
SubscriptionObserver.error
node_modules/zen-observable/lib/Observable.js (240:0)
eval
node_modules/@apollo/client/utilities/observables/iteration.js (4:48)
Array.forEach
<anonymous>
iterateObserveRSSafely
node_modules/@apollo/client/utilities/observables/iteration.js (4:0)
Object.error
node_modules/@apollo/client/utilities/observables/Concast.js (35:42)
notifySubscription
node_modules/zen-observable/lib/Observable.js (140:0)
onNotify
node_modules/zen-observable/lib/Observable.js (179:0)
SubscriptionObserver.error
node_modules/zen-observable/lib/Observable.js (240:0)
eval
node_modules/@apollo/client/link/http/createHttpLink.js (110:0)
这是我的后端代码:
const CompanyType = new GraphQLObjectType({
name: 'Company',fields: () => ({
_id: { type: GraphQLString },name: { type: GraphQLString },description: { type: GraphQLString },users: {
type: new GraphQLList(UserType),resolve(parentValue,args) {
return User.findUsers(parentValue._id);
},},}),});
const mutation = new GraphQLObjectType({
name: 'Mutation',fields: {
deleteCompany: {
type: CompanyType,args: { _id: { type: new GraphQLNonNull(GraphQLString) } },{ _id }) {
return Company.remove({ _id }).catch((err) => console.log(err));
},});
这是我的 React 前端代码
import React from 'react';
import { gql,useMutation } from '@apollo/client';
const DELETE_COMPANY = gql`
mutation DeleteCompany($id: String!) {
deleteCompany(_id: $id) {
_id
}
}
`;
const CompanyList = () => {
const { loading: loadingList,error: errorList,data: dataList } = useQuery(
COMPANY_LIST
);
const [
deleteCompany,{ loading: loadingDelete,error: errorDelete,data: dataDelete },] = useMutation(DELETE_COMPANY);
const renderCompanies = () =>
dataList.companies.map((company) => (
<li key={company._id}>
{company.name}
<button
onClick={() => {
deleteCompany({ variables: { _id: company._id } });
}}
>
delete
</button>
</li>
));
return (
<div>
{loadingList || loadingDelete ? <h1>Loading...</h1> : renderCompanies()}
</div>
);
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。