使用NestJS、Prisma及GraphQL运行npm run start:dev时遭遇Missing 'driver' option错误的解决咨询
Hey there! That error you're hitting is because NestJS GraphQL v10 introduced a breaking change—the driver configuration option is now required, since the module no longer defaults to Apollo Server under the hood. Let's walk through how to fix this step by step.
Step 1: Install Required Dependencies
First, make sure you have the Apollo driver package installed (since you're using Apollo's landing page plugin, we'll stick with Apollo):
npm install @nestjs/apollo @apollo/server graphql
Step 2: Update Your app.module.ts
You need to import the ApolloDriver and ApolloDriverConfig from @nestjs/apollo, then add the mandatory driver property to your GraphQLModule.forRoot configuration. Here's the full corrected code:
import { Module } from '@nestjs/common'; import { GraphQLModule } from '@nestjs/graphql'; // Add these Apollo-specific imports import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default'; import { DonationsModule } from './donations/donations.module'; @Module({ imports: [ GraphQLModule.forRoot<ApolloDriverConfig>({ // Required driver property to specify Apollo as the GraphQL server driver: ApolloDriver, // Keep your existing plugin and type path settings plugins: [ApolloServerPluginLandingPageLocalDefault()], typePaths: ['./**/*.graphql'], // Keep playground disabled since you're using Apollo's landing page playground: false, }), DonationsModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
Key Changes Explained:
- Added imports for
ApolloDriverandApolloDriverConfigto enable Apollo Server integration - Added the
driver: ApolloDriverproperty (the missing required config) - Used
ApolloDriverConfigas the generic type forforRootto get proper TypeScript type checking - Updated the import path for the Apollo landing page plugin (it moved in newer Apollo Server versions)
Step 3: Verify Your generate-typings.ts
Your existing generate-typings.ts should work as-is with the updated setup, since it only generates TypeScript types from your .graphql files. If you run into issues here, double-check that all your GraphQL dependencies are on compatible versions (v10 of @nestjs/graphql pairs seamlessly with the latest @nestjs/apollo).
Test the Fix
Now fire up your dev server again:
npm run start:dev
The "Missing 'driver' option" error should be resolved, and your GraphQL server should start up with the Apollo landing page accessible at your server's GraphQL endpoint.
内容的提问来源于stack exchange,提问作者yuturo

