【问题标题】:Graphql mutations without input args did't work没有输入参数的 Graphql 突变不起作用
【发布时间】: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() 的实现吗?

标签: graphql nestjs


【解决方案1】:

您的 mutation 名称是 updateUser,因此您应该使用类似的名称:

mutation {
    updateUser () 
    {
       success    
    } 
          }

考虑在括号内,您需要传递操作所需的数据。

无论如何,我建议使用 GraphQL PlayGround 来找到正确的查询格式。

【讨论】:

  • 我在操场上试了一下并附上了一张图片。不确定谁对此投反对票。
猜你喜欢
  • 2018-07-02
  • 2020-12-23
  • 2020-06-22
  • 2019-06-11
  • 2021-11-18
  • 2019-03-09
  • 2020-05-11
  • 2020-05-18
  • 2020-06-12
相关资源
最近更新 更多