nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.63k stars 7.62k forks source link

@nestjs/graphql not working in latest versions #14063

Closed leandroluk closed 3 weeks ago

leandroluk commented 3 weeks ago

Is there an existing issue for this?

Current behavior

I am trying to create a project using NestJS + GraphQL and using the documentation and run the project I get an error message Error: Query root type must be provided.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-t8ae72?file=src%2Fapp.module.ts

Steps to reproduce

  1. Create a NestJS application
$ nest new app
  1. Install GraphQL dependencies like tutorial
$ npm i -E @nestjs/graphql@12.2.0 @nestjs/apollo@12.2.0 @apollo/server@4.11.0 graphql@16.9.0

Note: i had forced to install exact versions throwing error before install using -E to grant the same behavior.

  1. Add plugin reference to nest-cli.json:
{
    "$schema": "https://json.schemastore.org/nest-cli",
    "collection": "@nestjs/schematics",
    "sourceRoot": "src",
    "compilerOptions": {
    "deleteOutDir": true,
    "plugins": [{ "name": "@nestjs/graphql" }]
    }
}
  1. Create src/app.resolver.ts file:
import {Query, Resolver} from '@nestjs/graphql';
import {AppService} from './app.service';

@Resolver()
export class AppResolver {
    constructor(private readonly appService: AppService) {}

    @Query(() => String)
    getHello() {
    return this.appService.getHello();
    }
}
  1. Change the src/app.module.ts adding graphql dependencies and the new resolver:
import {ApolloDriver, ApolloDriverConfig} from '@nestjs/apollo';
import {Module} from '@nestjs/common';
import {GraphQLModule} from '@nestjs/graphql';
import {AppController} from './app.controller';    
import {AppResolver} from './app.resolver';
import {AppService} from './app.service';

@Module({
    imports: [GraphQLModule.forRoot<ApolloDriverConfig>({driver: ApolloDriver})],
    controllers: [AppController],
    providers: [AppResolver, AppService]
})
export class AppModule {}
  1. start project and show the error:
$ npm run start:dev

[12:52:28] File change detected. Starting incremental compilation...
[12:52:28] Found 0 errors. Watching for file changes.

[Nest] 17716  - 10/10/2024, 12:52:29     LOG [NestFactory] Starting Nest application...
[Nest] 17716  - 10/10/2024, 12:52:29     LOG [InstanceLoader] AppModule dependencies initialized +12ms
[Nest] 17716  - 10/10/2024, 12:52:29     LOG [InstanceLoader] GraphQLSchemaBuilderModule dependencies initialized +0ms
[Nest] 17716  - 10/10/2024, 12:52:29     LOG [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 17716  - 10/10/2024, 12:52:29     LOG [RoutesResolver] AppController {/}: +2ms
[Nest] 17716  - 10/10/2024, 12:52:29     LOG [RouterExplorer] Mapped {/, GET} route +3ms
C:\dev\leandroluk\app\node_modules\graphql\type\validate.js:59
    throw new Error(errors.map((error) => error.message).join('\n\n'));
        ^

Error: Query root type must be provided.
    at assertValidSchema (C:\dev\leandroluk\app\node_modules\graphql\type\validate.js:59:11)
    at Function.generateSchemaDerivedData (C:\dev\leandroluk\app\node_modules\@apollo\server\src\ApolloServer.ts:734:22)
    at Object.schemaDerivedDataProvider (C:\dev\leandroluk\app\node_modules\@apollo\server\src\ApolloServer.ts:276:28)
    at new SchemaManager (C:\dev\leandroluk\app\node_modules\@apollo\server\src\utils\schemaManager.ts:79:36)
    at new ApolloServer (C:\dev\leandroluk\app\node_modules\@apollo\server\src\ApolloServer.ts:273:26)
    at ApolloDriver.registerExpress (C:\dev\leandroluk\app\node_modules\@nestjs\apollo\dist\drivers\apollo-base.driver.js:84:24)
    at ApolloDriver.registerServer (C:\dev\leandroluk\app\node_modules\@nestjs\apollo\dist\drivers\apollo.driver.js:36:24)
    at ApolloDriver.start (C:\dev\leandroluk\app\node_modules\@nestjs\apollo\dist\drivers\apollo.driver.js:21:20)
    at GraphQLModule.onModuleInit (C:\dev\leandroluk\app\node_modules\@nestjs\graphql\dist\graphql.module.js:123:36)
    at async callModuleInitHook (C:\dev\leandroluk\app\node_modules\@nestjs\core\hooks\on-module-init.hook.js:51:9)

Node.js v20.17.0

Expected behavior

Are expected to project starts having one query called getHello on graphql schema.

Package

Other package

@nestjs/graphql

NestJS version

10.4.4

Packages versions

{ "name": "app", "version": "0.0.1", "description": "", "author": "", "private": true, "license": "UNLICENSED", "scripts": { "build": "nest build", "format": "prettier --write \"src//*.ts\" \"test/*/.ts\"", "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}//.ts\" --fix", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@apollo/server": "4.11.0", "@nestjs/apollo": "12.2.0", "@nestjs/common": "^10.0.0", "@nestjs/core": "^10.4.4", "@nestjs/graphql": "12.2.0", "@nestjs/platform-express": "^10.0.0", "graphql": "16.9.0", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1" }, "devDependencies": { "@nestjs/cli": "^10.0.0", "@nestjs/schematics": "^10.0.0", "@nestjs/testing": "^10.0.0", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", "@types/node": "^20.3.1", "@types/supertest": "^6.0.0", "@typescript-eslint/eslint-plugin": "^8.0.0", "@typescript-eslint/parser": "^8.0.0", "eslint": "^8.42.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", "gts": "^5.3.1", "jest": "^29.5.0", "prettier": "^3.0.0", "source-map-support": "^0.5.21", "supertest": "^7.0.0", "ts-jest": "^29.1.0", "ts-loader": "^9.4.3", "ts-node": "^10.9.1", "tsconfig-paths": "^4.2.0", "typescript": "^5.1.3" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".\.spec\.ts$", "transform": { "^.+\.(t|j)s$": "ts-jest" }, "collectCoverageFrom": [ "*/.(t|j)s" ], "coverageDirectory": "../coverage", "testEnvironment": "node" } }

Node.js version

20.17.0

In which operating systems have you tested?

Other

No response

micalevisk commented 3 weeks ago

could you spot the latest working version?


btw don't you need to add autoSchemaFile: true?

Tony133 commented 3 weeks ago

The error Error: Query root type must be provided., is because autoSchemaFile: true is missing, see also this issue: https://github.com/nestjs/nest/issues/12364

micalevisk commented 3 weeks ago

I wonder if there is any way to prevent this

Tony133 commented 3 weeks ago

We should see in the @nestjs/graphql package if we can return a more intuitive message instead of this error: Error: The root type of the query must be provided, maybe it can help to better understand the error.

kamilmysliwiec commented 3 weeks ago

Please search through some of our old issues on this (this has been discussed several times in the past).