【问题标题】:GraphQL Schema not updated with NestJS (code-first approach)GraphQL Schema 未使用 NestJS 更新(代码优先方法)
【发布时间】:2020-08-17 14:11:18
【问题描述】:

GraphQL 新手,我在使用最新版本的 NestJS 时遇到问题,我目前正在尝试向解析器添加突变,但在服务器运行时不会在操场上显示。

看起来 GraphQL 架构在服务器启动时没有更新。

createUser 变异在 GraphQL 操场中显示并正常工作,但 getUsers 变异(为测试目的而创建)没有显示。

如果能提供任何有关如何解决此问题的提示,我将不胜感激。

在 app.module 中导入 GraphQLMOdule

import { Module } from '@nestjs/common';
// Libraries
import { TypeOrmModule } from '@nestjs/typeorm';
import { GraphQLModule } from '@nestjs/graphql';
// App modules
import { MealModule } from './meal/meal.module';
import { AuthModule } from './auth/auth.module';
// Entities
import { MealEntity } from './meal/meal.entity';
import { UserEntity } from './auth/user.entity';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mongodb',
      url: 'mongodb://localhost/sideproject',
      synchronize: true,
      useUnifiedTopology: true,
      entities: [MealEntity, UserEntity],
    }),
    GraphQLModule.forRoot({
      autoSchemaFile: true,
      debug: true,
      playground: true
    }),
    MealModule,
    AuthModule,
  ],
})
export class AppModule {}

以下是我遇到困难的用户模块的类型:

import { ObjectType, Field, ID } from '@nestjs/graphql';

@ObjectType('User')
export class UserType {
  @Field(() => ID)
  id: string;

  @Field()
  username: string;

  @Field()
  email: string;

  @Field()
  password: string;
}

相关的解析器:

import { Resolver, Mutation, Args, Query } from '@nestjs/graphql';
import { UserType } from './types/user.types';
import { CreateUserInputType } from './inputs/create-user.input';
import { UserEntity } from './user.entity';
import { AuthService } from './auth.service';

@Resolver(of => UserType)
export class AuthResolver {
  constructor(private authService: AuthService) {}

  @Mutation(returns => UserType)
  signUp(
    @Args('createUserInput') createUserInput: CreateUserInputType,
  ): Promise<UserEntity> {
    return this.authService.signUp(createUserInput);
  }

  @Query(returns => [UserType])
  getUsers(): Promise<UserEntity[]> {
    return this.authService.getUsers()
  }
}

服务:

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateUserInputType } from './inputs/create-user.input';
import { UserRepository } from './user.repository';
import { UserEntity } from './user.entity';

@Injectable()
export class AuthService {
  constructor(
    @InjectRepository(UserRepository)
    private userRepository: UserRepository,
  ) {}

  signUp(createUserInput: CreateUserInputType): Promise<UserEntity> {
    return this.userRepository.signUp(createUserInput);
  }

  async getUsers(): Promise<UserEntity[]> {
    return await this.userRepository.find();
  }
}

最后是用户模块的存储库:

import { Repository, EntityRepository } from 'typeorm';
import { UserEntity } from './user.entity';
import { InternalServerErrorException } from '@nestjs/common';
import { CreateUserInputType } from './inputs/create-user.input';

import { v4 as uuid } from 'uuid';
import * as bcrypt from 'bcryptjs';

@EntityRepository(UserEntity)
export class UserRepository extends Repository<UserEntity> {
  async signUp(createUserInput: CreateUserInputType): Promise<UserEntity> {
    const { username, email, password } = createUserInput;

    const user = this.create();
    user.id = uuid();
    user.username = username;
    user.email = email;
    user.password = bcrypt.hashSync(password, bcrypt.genSaltSync(12));
    try {
      return await this.save(user);
    } catch (err) {
      console.log(err);
      throw new InternalServerErrorException();
    }
  }
}

非常感谢!

【问题讨论】:

  • 尝试删除schema.gql文件并重新运行服务器。您在此处附加的代码不相关。请附上GraphQLModule的导入代码,在app.module
  • 感谢您的回答。我用帖子顶部的导入更新了我的第一篇帖子。如您所见,我使用的是 { autoSchemaFile: true } 选项,因此在启动时不会创建 schema.gql 文件。
  • 这很糟糕,但我尝试重新创建一个项目并使用完全相同的配置复制粘贴了我的所有文件,它终于奏效了。谜团……
  • 是同样的问题,删除dist文件夹后重启项目就OK了。
  • 不应该那样发生。

标签: graphql nestjs


【解决方案1】:

我认为您需要将解析器添加到提供程序集合中才能使解析器可用。

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 2020-07-14
    • 2022-07-29
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多