简体   繁体   中英

Delete axios method returns empty data

I have successfully implemented a really simple example to use graphql and apollo-server and json-server. But the delete method returns empty data. I console.log(res.data) and is empty. Any help?

Resolver mutation methods:

type Mutation {
    addCustomer(name: String!, email: String!, age: Int!): Customer!
    editCustomer(id: ID!, name: String!, email: String!, age: Int!): Customer!
    deleteCustomer(id: ID!): Customer!
  }

The typeDef of deleted customer:

deleteCustomer: async (_, { id }) => {
    const res = await axios.delete("http://localhost:3000/customers/" + id);
    return res.data;
},

The code is here, in case the provided code is not enough: https://github.com/motapinto/learn-graphql-apollo-server

Just ran your code there. It looks like if you delete it it returns null or an empty object. In this case I believe you should return the id, and check your res variable, to make sure there is no errors and return that with the id.

Something like the code below. But, there could be a better way.

    deleteCustomer: async (_, { id }) => {
        const res = await axios.delete("http://localhost:3000/customers/" + id);
        if (res.status !== 200) return { success: false};

        return { success: true, id}; 
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM