DEV Community

Brian Nguyen
Brian Nguyen

Posted on

Getting started with NestJS and GraphQL

Quick demo video: https://youtu.be/9KKrkOEhd-I

Install nest-cli if needed

npm i -g @nestjs/cli
Enter fullscreen mode Exit fullscreen mode

Create new nest project

nest new nest-graphql-example
Enter fullscreen mode Exit fullscreen mode

Add first resolver

@Resolver()
class UserResolver {

  @Query(() => GraphQLString)
  hello() {
    return 'World!';
  }
}
Enter fullscreen mode Exit fullscreen mode

Start server

yarn start:dev
Enter fullscreen mode Exit fullscreen mode

or

npm run start:dev
Enter fullscreen mode Exit fullscreen mode

First query

{
  hello
}
Enter fullscreen mode Exit fullscreen mode

User Object Type

@ObjectType()
class User {
  @Field()
  id: string;

  @Field()
  password: string;

  @Field()
  name: string;

  @Field()
  email: string;
}
Enter fullscreen mode Exit fullscreen mode

CreateUserInput

@InputType()
class CreateUserInput {
  @Field()
  password: string;

  @Field()
  name: string;

  @Field()
  email: string;
}
Enter fullscreen mode Exit fullscreen mode

UpdateUserInput

@InputType()
class UpdateUserInput {
  @Field()
  id: string;

  @Field({ nullable: true })
  password: string;

  @Field({ nullable: true })
  name: string;

  @Field({ nullable: true })
  email: string;
}
Enter fullscreen mode Exit fullscreen mode

UserService

@Injectable()
class UserService {
  private users: User[] = [];

  public createUser(input: CreateUserInput) {
    const user = {
      id: Date.now().toString(),
      ...input,
    };
    this.users.push(user);
    return user;
  }

  public updateUser(input: UpdateUserInput) {
    const user = this.users.find((user) => user.id === input.id);
    if (!user) {
      throw new NotFoundException('User not found');
    }
    if (user) {
      Object.assign(user, input);
    }
    return user;
  }

  public getUser(id: string) {
    const user = this.users.find((user) => user.id === id);
    if (!user) {
      throw new NotFoundException('User not found');
    }
    return user;
  }

  public getUsers() {
    return this.users;
  }
}
Enter fullscreen mode Exit fullscreen mode

Final UserResolver

@Resolver()
class UserResolver {
  constructor(private readonly userService: UserService) {}

  @Query(() => User)
  user(@Args('id') id: string) {
    return this.userService.getUser(id);
  }

  @Mutation(() => User)
  createUser(@Args('input') input: CreateUserInput) {
    return this.userService.createUser(input);
  }

  @Mutation(() => User)
  updateUser(@Args('input') input: UpdateUserInput) {
    return this.userService.updateUser(input);
  }

  @Query(() => [User])
  users() {
    return this.userService.getUsers();
  }
}
Enter fullscreen mode Exit fullscreen mode

Final AppModule

@Module({
  imports: [
    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      playground: true,
      autoSchemaFile: true,
    }),
  ],
  controllers: [],
  providers: [UserResolver, UserService],
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Final code:
https://gist.github.com/vanpho93/a2649b7a0deb63c35675362de0999b3b

Top comments (0)