[英]GraphQL mutation type missing in the response
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: 我在GraphQL API中内置了
DeleteUserMutation
,它应该允许我删除用户并收到一条带有消息的响应,但返回时出现错误:
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
我尝试实现以下目的:删除用户并成功响应后,我看到类型为
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. 但是以上内容不起作用,并且由于我是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: 我更改了
typeof
,并解决了该问题:
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;
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.