I built in my GraphQL API a DeleteUserMutation
which should allow me to delete a user and have a response with a message but on the return, I'm getting an error:
SError: ⨯ Unable to compile TypeScript:
src/graphql/mutations/user/delete-user.ts:21:9 - error TS2740: Type '{ responseCode: number; message: string; user: any; }' is missing the following properties from type 'User': id, name, last_name, email, and 4 more.
21 return response;
~~~~~~~~~~~~~~~~
I tried to achieve that when a User is deleted and success in the response I see the message from my type DeleteUserresponse
export const DeleteUserResponse = gql`
type DeleteUserResponse {
responseCode: Number!
message: String!
user: User
}
`;
The mutation should delete the user and if success shows the success values and if not shows the opposite and I tried in this way:
import { getRepository } from 'typeorm';
import { Entities } from '../../../entities/entities';
export const deleteUserMutation = {
async deleteUser(_, { id }): Promise<typeof user> {
const response = {
responseCode: 500,
message: 'Error, user not deleted',
user: null,
};
const repository = getRepository(Entities.user);
const user = await repository.findOne({ id });
if (user && repository.delete({ id })) {
response.responseCode = 200;
response.message = 'User deleted successfully';
response.user = user;
}
return response;
},
};
But this above doesn't work and cannot understand what is the issue as I'm a newbie in GraphQL. The error suggests the types are missing but I tried to play around adding and removing types but that error is not going away.
What is the issue in my code and how to fix it.
输入Mutation {deleteUserMutation(input:{}):Respone}输入Respone {responseCode:Number!消息:String!user:User}请使用上面的模式
I want to show my solution to my problem as based on comments I read on my question.
I changed the typeof
as follow and that fixed the issue:
Promise<typeof deleteUserMutation>
Full code:
import { getRepository } from 'typeorm';
import { Entities } from '../../../entities/entities';
export const deleteUserMutation = {
async deleteUser(_, { id }): Promise<typeof deleteUserMutation> {
const response = {
responseCode: 500,
message: 'Error, user not deleted',
user: null,
};
const repository = getRepository(Entities.user);
const user = await repository.findOne({ id });
if (user && repository.delete({ id })) {
response.responseCode = 200;
response.message = 'User deleted successfully';
response.user = user;
}
return response;
},
};
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.