I have been practicing with nestjs to convert a ReST API to GraphQL, but I keep encountering an error whenever I attempt to fetch data from my GraphQL API using the playground:
"errors": [ { "message": "Right-hand side of 'instanceof' is not an object", "locations": [ { "line": 2, "column": 3 } ], "path": [ "lesson" ], "extensions": { "code": "INTERNAL_SERVER_ERROR", "exception": { "stacktrace": [ "TypeError: Right-hand side of 'instanceof' is not an object", " at MongoEntityManager. (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\typeorm\entity-manager\MongoEntityManager.js:159:51)", " at step (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\tslib\tslib.js:143:27)", " at Object.next (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\tslib\tslib.js:124:57)", " at C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\tslib\tslib.js:117:75", " at new Promise ()", " at Object.__awaiter (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\tslib\tslib.js:113:16)", " at MongoEntityManager.findOne (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\typeorm\entity-manager\MongoEntityManager.js:153:24)", " at MongoRepository.findOne (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\typeorm\repository\MongoRepository.js:57:29)", " at LessonService.getLesson (C:\Users\Oluyinka\Desktop\graphql-mongodb\dist\lesson\lesson.service.js:26:44)", " at LessonResolver.lesson (C:\Users\Oluyinka\Desktop\graphql-mongodb\dist\lesson\lesson.resolver.js:24:35)" ] } } } ], "data": null }
Here is the code snippet:
lesson.entity.ts
import { Column, Entity, ObjectIdColumn, PrimaryColumn } from 'typeorm';
@Entity()
export class Lesson {
@ObjectIdColumn()
_id: string;
@PrimaryColumn()
id: string;
@Column()
name: string;
@Column()
startDate: string;
@Column()
endDate: string;
}
lesson.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Lesson } from './lesson.entity';
import { v4 as uuid } from 'uuid';
@Injectable()
export class LessonService {
constructor(
@InjectRepository(Lesson) private lessonRepository: Repository<Lesson>,
) {}
async getLesson(id: string): Promise<Lesson> {
return await this.lessonRepository.findOne({ id });
}
async getLessons(): Promise<Lesson[]> {
return this.lessonRepository.find();
}
async createLesson(name, startDate, endDate): Promise<Lesson> {
const lesson = this.lessonRepository.create({
id: uuid(),
name,
startDate,
endDate,
});
return await this.lessonRepository.save(lesson);
}
}
lesson.resolver.ts
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { LessonService } from './lesson.service';
import { LessonType } from './lesson.type';
@Resolver((of) => LessonType)
export class LessonResolver {
constructor(private lessonService: LessonService) {}
@Query((returns) => LessonType)
lesson(@Args('id') id: string) {
return this.lessonService.getLesson(id);
}
@Query((returns) => [LessonType])
lessons() {
return this.lessonService.getLessons();
}
@Mutation((returns) => LessonType)
createLesson(
@Args('name') name: string,
@Args('startDate') startDate: string,
@Args('endDate') endDate: string,
) {
return this.lessonService.createLesson(name, startDate, endDate);
}
}
lesson.type.ts
import { Field, ID, ObjectType } from '@nestjs/graphql';
@ObjectType('Lesson')
export class LessonType {
@Field((type) => ID)
id: string;
@Field()
name: string;
@Field()
startDate: string;
@Field()
endDate: string;
}