Luis
Luis

Reputation: 130

NestJS Graphql Fastify FastifyError: fastify-plugin: fastify-accepts - expected '3.x' fastify version, '4.6.0' is installed

I'm following the NestJS documentation on how to set up a fastify/apollo server with nest but after adding fastify to main.ts I get this error:

FastifyError: fastify-plugin: fastify-accepts - expected '3.x' fastify version, '4.6.0' is installed

I've tried installing fastify 3.x, changing versions of other modules but according to yarn.lock some modules need 3.x and others need 4.x.

These are the docs I'm following:

  1. https://docs.nestjs.com/techniques/performance
  2. https://docs.nestjs.com/graphql/quick-start

Here is my main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter({ logger: true }),
  );

  app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000);
  console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();

Here is my package.json

{
  "name": "app",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "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": {
    "@nestjs/apollo": "^10.1.0",
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/graphql": "^10.1.2",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/platform-fastify": "^9.1.1",
    "apollo-server-fastify": "^3.10.2",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "firebase-admin": "^11.0.1",
    "graphql": "^16.6.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "ts-morph": "^16.0.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "28.1.8",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.1.3",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.8",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.0",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Upvotes: 0

Views: 1493

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70412

@nestjs/common@^9 and @nestjs/platform-fastify@^9 require you to use fastify@^4. However, apllo-server-fastify does not support fastify@^4 and will not until apollo-server v4 comes out. Until then,. you can follow the docs and use mercurius instead.

Your other option would be to downgrade to @nestjs/common@^8 and @nestjs/platform-fastify@^8 along with @nestjs/graphql@^9 which supports fastify@^3

Upvotes: 2

Related Questions