0
I’m currently working on a small Nest.js application where I want to create a GraphQL query to retrieve an array of books. However, I’m encountering an issue where I’m not getting the expected response. I’ve provided the relevant code snippets below. Can someone please help me identify what I might be doing wrong?
src/books/books.dto.ts
export interface CreateBookDto {
id: number;
title: string;
author: string;
}
////////////////////////////////////
src/books/book.resolver.ts
import { CreateBookDto } from './books.dto';
import { Query, Resolver } from '@nestjs/graphql';
import { BooksService } from './books.service';
@Resolver()
export class BooksResolver {
constructor(private readonly bookService: BooksService) {}
@Query(() => [CreateBookDto])
getAllBooks(): CreateBookDto[] {
return this.bookService.getAllBooks();
}
}
//////////////////
src/books/schema.graphql
type createBooks {
id: ID!
title: String!
author: String!
}
type Query {
getAllBooks: [createBooks!]!
}
////////////////////////////
src/books/books.module.ts
import { Module } from '@nestjs/common';
import { BooksService } from './books.service';
import { BooksResolver } from './books.resolver';
import { BooksController } from './books.controller';
@Module({
controllers: [BooksController],
providers: [BooksService, BooksResolver]
})
export class BooksModule {}
/////////////////////////
src/app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { BasicController } from './basic/basic.controller';
import { BooksModule } from './books/books.module';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
@Module({
imports: [
GraphQLModule.forRoot({
autoSchemaFile: join(process.cwd(), 'src/books/schema.gql')
}),
BooksModule
],
controllers: [AppController, BasicController],
providers: [AppService],
})
export class AppModule {}
The above code is a simplified version of my application structure. The goal is to create a GraphQL query to retrieve an array of books. However, when I test the query, I’m receiving an error related to missing query definitions. Specifically, I get the error:
"Query.getAllBooks" was defined in resolvers, but not in schema.
I’ve checked my resolver and schema definitions, and they seem to match. Could someone please help me identify what might be causing this issue? Thank you!