node.js - 如何使用 type-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;
}
我得到的错误是 - 错误:无法确定“LoginObject”类的“用户”的 GraphQL 输出类型。用作其 TS 类型或显式类型的值是用适当的装饰器装饰的还是适当的输出值?
我如何使它工作?
解决方案
由于 graphql API 公开的每个复杂类型都必须是已知类型。在您的示例中,LoginObject
公开了一个复杂的属性类型,User
因此User
该类应使用@ObjectType()
装饰器进行注释。
推荐阅读
- python - 一次在不同数据集上使用 tf.feature_column.input_layer 的自定义模型(不是 tf.estimator)?
- android - 如何在活动中从适配器调用方法?
- sql - 无法从 SQL Server 中的表中删除“第一条”记录
- java - JUnit 用于测试单链表的反转
- javascript - 未从 AJAX '$_POST' 到 PHP 文件执行 SQL 查询
- python - 生成真/假的所有长度-n 排列?
- c# - 来自客户端html和服务器端C#的POST方法有什么区别
- multithreading - 多线程行为从机器更改为其他
- php - 为 Woocommerce 在 quantity-input.php 中添加数量单位
- angular - 角度:如何解决这个问题