Nestjs:从GraphQL架构生成解析器

时间:2019-07-10 15:40:07

标签: node.js graphql nestjs

我在nestjs中使用prisma创建了一个graphql服务器。我意识到,对于每种graphql类型,我必须创建一个解析器并生成客户端代码,我必须再次创建查询和变异。是否有针对每种类型生成解析器,查询和变异的方法?

假设我的帐户类型为

type Account {
    id
    email
    uid
    nom
    prenom
}

我必须创建一个解析器,查询和变异。

解析器

@Resolver('Account')
export class AccountResolver {

    constructor(private readonly prisma: PrismaService) { }

    @Query('accounts')
    async accounts(@Args('where') where: AccountWhereInput) {
        return await this.prisma.prisma.accounts({ where });
    }

    @Query('account')
    async account(@Args('where') where: AccountWhereUniqueInput) {
        return await this.prisma.prisma.account(where);
    }

    @Mutation('createAccount')
    async createAccount(@Args('data') data: AccountCreateInput) {
        return await this.prisma.prisma.createAccount(data);
    }

    @Mutation('updateAccount')
    async updateAccount(@Args('data') data: AccountUpdateInput, @Args('where') where: AccountWhereUniqueInput) {
        return await this.prisma.prisma.updateAccount({data, where});
    }

    @Mutation('deleteAccount')
    async deleteAccount(@Args('data') where: AccountWhereUniqueInput) {
        return await this.prisma.prisma.deleteAccount(where);
    }


}

查询和变异

fragment AccountFragment on Account{
    id
    email
    uid
    nom
    prenom
}

query accounts($where: AccountWhereInput) {
  accounts(where: $where) {
    ...AccountFragment
  }
}

query account($where: AccountWhereUniqueInput!) {
  account(where: $where) {
    ...AccountFragment
  }
}

mutation createAccount($data: AccountCreateInput!) {
  createAccount(data: $data) {
    ...AccountFragment
  }
}

mutation updateAccount($where: AccountWhereUniqueInput!, $data: AccountUpdateInput!) {
  updateAccount(data: $data, where: $where) {
    ...AccountFragment
  }
}

如何在80种类型的项目中生成这些代码。 任何帮助请

0 个答案:

没有答案