如何使用类型graphql定义对象类型



我的解析器:

@Resolver()
class UserResolver{
@Query(()  => Boolean)
async userExists(@Arg('email') email: string) {
const user = await User.findOne({email});
return user ? true : false;
}
@Mutation(() => LoginObject)
async login(
@Arg('email') email: string,
@Arg('password') password: string
): Promise<LoginObject>{
const user = await User.findOne({email});
if(!user) throw new Error('User not found!');
const match = await cmp(password, user.password);
if(!match) throw new Error('Passwords do not match!');
return {
accessToken: createAccessToken(user),
user
};
}
}

对象类型:

import {ObjectType, Field} from "type-graphql";
import User from "../entity/User";
@ObjectType()
class LoginObject {
@Field()
user: User;
@Field()
accessToken: string;
}

我得到的错误是-错误:无法确定"LogiObject"类的"user"的GraphQL输出类型。用作TS类型或显式类型的值是用适当的装饰器装饰的,还是用适当的输出值装饰的?

我该如何让它发挥作用?

由于graphql API公开的每个复杂类型都必须是已知类型。在您的示例中,LoginObject公开了一个复杂的属性类型User,因此User类应该用@ObjectType()装饰器进行注释。

最新更新