类型 Graphql 自定义解析器
Type Graphql custom Resolver
我有一个 User ObjectType 并希望在我的 LoginResolver 中,return 用户和为该用户生成的令牌作为对象,如下所示:
{user:User, token:string}
目前我可以 return 用户或字符串,这取决于我在 @Mutation() 装饰器中给出的类型
@Mutation((returns) => User)
async login(
@Arg("email") email: string,
@Arg("password") password: string,
@Ctx("ctx") ctx: IContext
) {
const user = await this.userRepo.findOneUser({ where: { email } });
const success = await compare(password, user.password);
if (!success) ctx.throw(401);
const token = await this.tokenRepo.createToken(user);
return user;
}
当我尝试创建 UserWIthToken 对象类型时,我在用户实体的每个字段中都收到以下错误:
app_1 | error: [ValidationError: Cannot query field "id" on type "UserWithToken".] {
app_1 | locations: [ [Object] ],
app_1 | path: undefined,
app_1 | extensions: { code: 'GRAPHQL_VALIDATION_FAILED', exception: [Object] }
app_1 | }
app_1 | }```
dont mind the app_1 here, Iam using docker
您需要为此案例创建自定义 ObjectType
@ObjectType()
class UserWithToken {
@Field(() => User)
user: User
@Field()
token: string
}
然后在您的查询中使用该对象而不是用户
@Query((returns) => UserWithToken)
async login(
@Arg("email") email: string,
@Arg("password") password: string,
@Ctx("ctx") ctx: IContext
) : Promise<UserWithToken> {
const user = await this.userRepo.findOneUser({ where: { email } });
const success = await compare(password, user.password);
if (!success) ctx.throw(401);
const token = await this.tokenRepo.createToken(user);
return {
user,
token
};
}
请注意我在这里使用 @Query
而不是 @Mutation
因为它更适合这种情况,根据经验,如果您想读取数据而不修改它,请使用 Query,否则deleting/adding/editing 数据使用 Mutation。
然后你可以运行这样的登录查询
query {
login(email: "xx@xx.com", password: "xxxxx") {
token
user {
id
name
email
...
}
}
}
我有一个 User ObjectType 并希望在我的 LoginResolver 中,return 用户和为该用户生成的令牌作为对象,如下所示:
{user:User, token:string}
目前我可以 return 用户或字符串,这取决于我在 @Mutation() 装饰器中给出的类型
@Mutation((returns) => User)
async login(
@Arg("email") email: string,
@Arg("password") password: string,
@Ctx("ctx") ctx: IContext
) {
const user = await this.userRepo.findOneUser({ where: { email } });
const success = await compare(password, user.password);
if (!success) ctx.throw(401);
const token = await this.tokenRepo.createToken(user);
return user;
}
当我尝试创建 UserWIthToken 对象类型时,我在用户实体的每个字段中都收到以下错误:
app_1 | error: [ValidationError: Cannot query field "id" on type "UserWithToken".] {
app_1 | locations: [ [Object] ],
app_1 | path: undefined,
app_1 | extensions: { code: 'GRAPHQL_VALIDATION_FAILED', exception: [Object] }
app_1 | }
app_1 | }```
dont mind the app_1 here, Iam using docker
您需要为此案例创建自定义 ObjectType
@ObjectType()
class UserWithToken {
@Field(() => User)
user: User
@Field()
token: string
}
然后在您的查询中使用该对象而不是用户
@Query((returns) => UserWithToken)
async login(
@Arg("email") email: string,
@Arg("password") password: string,
@Ctx("ctx") ctx: IContext
) : Promise<UserWithToken> {
const user = await this.userRepo.findOneUser({ where: { email } });
const success = await compare(password, user.password);
if (!success) ctx.throw(401);
const token = await this.tokenRepo.createToken(user);
return {
user,
token
};
}
请注意我在这里使用 @Query
而不是 @Mutation
因为它更适合这种情况,根据经验,如果您想读取数据而不修改它,请使用 Query,否则deleting/adding/editing 数据使用 Mutation。
然后你可以运行这样的登录查询
query {
login(email: "xx@xx.com", password: "xxxxx") {
token
user {
id
name
email
...
}
}
}