【问题标题】:Creating Mutations In Nestjs Using The Code First approach使用 Code First 方法在 Nestjs 中创建突变
【发布时间】:2019-09-17 22:19:23
【问题描述】:

我正在创建基于 Nestjs(代码优先方法)和用于数据库管理的 prisma 的 graphql api。我关注了nestjs的官方页面,并成功创建了解析器来容纳我的查询、订阅和突变。我的第一个查询工作正常,没有任何问题。但是,在创建突变后,创建的突变不会提示输入任何参数,并且 graphql 游乐场不会指示我的突变请求中的错误。 该突变负责创建用户,并且应该采用两个参数来成功完成操作。

我尝试使用架构优先方法重新创建突变,但应用程序在启动期间崩溃。我还尝试向突变添加名称属性,但仍然没有任何反应。

这是我的变种

@Mutation(returns => User, { name: 'createUser' })
  async createUser(args) {
    return await this.userService.createUser(args);
  }

在用户服务中

  async create(args) {
    return await this.prismah.mutation.createUser({
      name: args.name,
      email: args.email,
    });
  }

变异应该在提交之前提示输入两个参数,在提交之后它应该在数据库中创建一个用户实例

【问题讨论】:

    标签: typescript nestjs


    【解决方案1】:

    我创建了一个类似 UserInput 的类

    import { InputType, Field } from 'type-graphql';
    
    @InputType()
    export class UserInput {
      @Field() name: string;
      @Field() email: string;
    }
    
    

    并将我的解析器更改为看起来像

    import {Args} from '@nestjs/graphql';
    import {UserInput} from './path-to-the-user-input-class-created';
    
    
    @Mutation(returns => User, { name: 'createUser' })
      async createUser(@Args('data') user: UserInput) {
        return await this.userService.createUser(user);
      }
    

    随着这些变化,我的突变现在按预期工作。它提示输入所需的变量并完成提交过程。

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多