简体   繁体   English

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

[英]How to define object types with type-graphql

My resolver:我的解析器:

@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
        };
    }
}

And object type:和对象类型:

import {ObjectType, Field} from "type-graphql";
import User from "../entity/User";

@ObjectType()
class LoginObject {

  @Field()
  user: User;

  @Field()
  accessToken: string;

}

The error I get is - Error: Cannot determine GraphQL output type for 'user' of 'LoginObject' class.我得到的错误是 - 错误:无法确定“LoginObject”类的“用户”的 GraphQL 输出类型。 Does the value used as its TS type or explicit type is decorated with a proper decorator or is it a proper output value?用作其 TS 类型或显式类型的值是否使用适当的装饰器进行了修饰,还是适当的输出值?

How do I make it work?我如何使它工作?

Since every complex type exposed by the graphql API must be a known type.由于 graphql API 公开的每个复杂类型都必须是已知类型。 In your example, LoginObject exposes a complex property type of User so the User class should be annotated with @ObjectType() decorator.在您的示例中, LoginObject公开了一个复杂的User属性类型,因此应该使用@ObjectType()装饰器对User类进行注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM