'GraphQL Schema not updated with NestJS (code-first approach)

Pretty new to GraphQL, I am facing an issue with the latest version of NestJS where I am currently trying to add a mutation to a resolver that doesn't show in the playground when the server is running.

It looks like the GraphQL schema is not updated on server launch.

The createUser mutation is showing in the GraphQL playground and working but the getUsers one (created for test purposes) is not showing.

I would appreciate any hint on how to tackle this issue.

Importation of the GraphQLMOdule in app.module

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 {}

Here are the types for the user module I am experiencing difficulties with :

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;
}

The associated resolver :

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()
  }
}

The service :

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();
  }
}

And finally the repository for the user module :

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();
    }
  }
}

Thank you very much !



Solution 1:[1]

I think you need to add the resolver to the providers collection for the resolvers to be available.

Solution 2:[2]

Had the same issue and these steps solved the problem:

  1. stop the server
  2. remove the dist folder
  3. restart the server

Solution 3:[3]

The answer to this is : declaring the objectype only will not update the schema. including the objectype as a return type within a resolver is the key.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cramhead
Solution 2 A. Masson
Solution 3 Fahmy Chaabane