【问题标题】:How to define object types with type-graphql如何使用 type-graphql 定义对象类型
【发布时间】:2021-01-09 13:25:57
【问题描述】:

我的解析器:

@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 类型或显式类型的值是用适当的装饰器装饰的还是适当的输出值?

如何让它发挥作用?

【问题讨论】:

  • 你可能需要用@ObjectType()装饰器注释类型User
  • 是的,我愿意。完全忘记了,谢谢
  • @Eldar 你能把你的评论作为答案吗?

标签: node.js typescript graphql typegraphql


【解决方案1】:

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

【讨论】:

  • 请问你能说明这是怎么做的吗?我正在努力解决同样的问题。谢谢
  • @Mel 你可以检查 OPs 问题。 LoginObject 类注释正确。基本上解释你想要公开的每个类都必须有 @ObjectType() 装饰器,并且你想要公开的类的每个字段都必须有 @Field() 装饰器。如果您有这些问题,您可能会遇到其他问题,您可以将其作为问题发布。
  • 感谢@Eldar。我一定是做错了什么。
猜你喜欢
  • 2020-05-29
  • 2020-12-19
  • 2019-08-07
  • 2018-06-22
  • 2021-09-12
  • 1970-01-01
  • 2019-06-24
  • 2012-12-28
  • 2019-12-15
相关资源
最近更新 更多