【发布时间】:2021-08-17 05:36:10
【问题描述】:
我创建了一个 NestJS 示例,我添加了一个 Mutation 以将用户信息同步到我的后端。
并添加以下代码,@GqlUser 是从我的 jwt 令牌中提取用户信息。
@Mutation((returns) => UpdateUserResult)
@UseGuards(JwtAuthGuard)
updateUser(@GqlUser() user: UserPrincipal): Observable<UpdateUserResult> {
console.log('gql user:', user);
const { userId, email, name } = user;
return this.usersService.update({ id: userId, email, name }).pipe(
map((b) => ({
success: b,
})),
);
}
相关的 GraphQL 定义由 NestJS 生成如下:
type Mutation {
//... other defs.
updateUser: UpdateUserResult!
}
type UpdateUserResult {
message: String!
success: Boolean!
}
// other defs.
但是当通过以下形式执行这个突变时。
// failed due to *the expectedName is not a ")"*, it is a syntax error
mutation SyncUser{
updateUser(){//error
success
}
}
// or
mutation SyncUser(){ // error
updateUser(){
success
}
}
或
// failed due to *this.subQuery is not a function*
mutation {
updateUser {
success
}
}
在 GraphQLspec 中,输入参数在突变定义中应该是可选的。
【问题讨论】:
-
没有参数 => 当然没有括号
-
你也可以发布 usersService.update() 的实现吗?