I am currently in the process of developing a NestJS API utilizing GraphQL, PostgreSQL, and Docker. However, I encountered an error when attempting to build my API:
/app/node_modules/@nestjs/graphql/dist/schema-builder/factories/output-type.factory.js:19
stocks-api-api-1
throw new cannot_determine_output_type_error_1.CannotDetermineOutputTypeError(hostType);
stocks-api-api-1 Error: Cannot determine a GraphQL output type for the "createStock". Make sure your class is decorated with an appropriate decorator.
Below is a snippet of the code causing the issue:
create-stock.dto:
import { Field, InputType, ObjectType } from "@nestjs/graphql";
import { IsNotEmpty } from "class-validator";
@InputType()
export class CreateStockDto {
@IsNotEmpty()
@Field()
ticker: string;
@IsNotEmpty()
@Field()
price : number;
@IsNotEmpty()
@Field()
timestamp : Date;
}
stocks.entity.ts:
import { Field, ObjectType, ID } from "type-graphql";
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
@Entity()
@ObjectType()
export class Stock extends BaseEntity {
@PrimaryGeneratedColumn()
@Field(type => ID)
id: string;
@Column()
@Field()
ticker: string;
@Column()
@Field()
price: number;
@Column()
@Field()
timestamp: Date;
}
stocks.resolver.ts
import { Args, Mutation, Resolver, Query, ObjectType } from '@nestjs/graphql';
import { Stock } from '../entities/stocks.entity';
import { CreateStockDto } from '../dtos/create-stock.dto';
import { StocksService } from '../stocks.service';
@Resolver()
@ObjectType()
export class StocksResolver {
constructor(private stocksService: StocksService) {}
@Query(() => [Stock])
async getStocks(): Promise<Stock[]> {
return this.stocksService.getStocks();
}
@Mutation(() => Stock)
async createStock(
@Args('stock') createTaskDto: CreateStockDto
): Promise<Stock> {
return this.stocksService.createStock(createTaskDto);
}
}
stock.module.ts
import { Module } from '@nestjs/common';
import { StocksService } from './stocks.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { StocksRepository } from './stocks.repository';
import { StocksResolver } from './resolvers/stocks.resolver';
@Module({
imports: [TypeOrmModule.forFeature([StocksRepository])],
providers: [StocksResolver,StocksService],
})
export class StocksModule {}
stocks.repository.ts:
import { Repository } from "typeorm";
import { Stock } from "./entities/stocks.entity";
import { CreateStockDto } from "./dtos/create-stock.dto";
import { Injectable, InternalServerErrorException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
@Injectable()
export class StocksRepository {
constructor(
@InjectRepository(Stock)
private repository: Repository<Stock>,
) {}
async getStocks(): Promise<Stock[]> {
const query = this.repository.createQueryBuilder('stock');
const stocks = await query.getMany();
return stocks;
}
async createStock({ ticker, price, timestamp }: CreateStockDto): Promise<Stock> {
const stock = this.repository.create({
ticker,
price,
timestamp
});
try {
await this.repository.save(stock);
return stock;
} catch (error) {
throw new InternalServerErrorException();
}
}
}
stocks.service.ts:
import { Injectable } from '@nestjs/common';
import { StocksRepository } from './stocks.repository';
import { CreateStockDto } from './dtos/create-stock.dto';
import { Stock } from './entities/stocks.entity';
import { InjectRepository } from '@nestjs/typeorm';
@Injectable()
export class StocksService {
constructor(
@InjectRepository(StocksRepository)
private stocksRepository: StocksRepository,
) {}
async getStocks(): Promise<Stock[]> {
return await this.stocksRepository.getStocks();
}
async createStock(createStockDto: CreateStockDto): Promise<Stock> {
return await this.stocksRepository.createStock(createStockDto);
}
}
I have been stuck on this issue for about four hours now and I still can't seem to figure it out. Everything seems properly decorated in the files provided. Can anyone point out what I might be missing here?