Graphql: Fastify registration omits apolloServer.start(), which throws an error at apolloServer.createHandler()

Created on 1 Jun 2021  Â·  7Comments  Â·  Source: nestjs/graphql

I'm submitting a...


[X] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When attempting to start an application that uses @nestjs/[email protected] and @nestjs/[email protected], the Apollo Server throws the following error that prevents the application from starting:

[Path]/node_modules/apollo-server-core/src/ApolloServer.ts:486
      throw new Error(
            ^
Error: You must `await server.start()` before calling `server.createHandler()`
    at ApolloServer.assertStarted ([Path]/node_modules/apollo-server-core/src/ApolloServer.ts:486:13)
    at ApolloServer.createHandler ([Path]/node_modules/apollo-server-fastify/src/ApolloServer.ts:45:10)
    at GraphQLModule.<anonymous> ([Path]/node_modules/@nestjs/graphql/dist/graphql.module.js:150:45)
    at Generator.next (<anonymous>)
    at [Path]/node_modules/tslib/tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter ([Path]/node_modules/tslib/tslib.js:113:16)
    at GraphQLModule.registerFastify ([Path]/node_modules/@nestjs/graphql/dist/graphql.module.js:143:24)
    at GraphQLModule.<anonymous> ([Path]/node_modules/@nestjs/graphql/dist/graphql.module.js:118:28)
    at Generator.next (<anonymous>)

Expected behavior

The application should start without an error and was previously working.

Minimal reproduction of the problem with instructions

Adding await apolloServer.start() to line 228 of lib/graphql.module.ts seems like it will fix the issue, but I'm not sure if that's the appropriate place to put it from an architecture perspective. I did this via yarn patch and the application starts successfully. My repo and patch are available on the dev-nx branch at troncali/nest-vue.

  • git clone --depth=1 -b dev-nx https://github.com/troncali/nest-vue
  • cd nest-vue
  • yarn install
  • Patch is located at ./.yarn/patches/@nestjs/graphql.patch
  • Working on a template repository, so some minimal setup is needed
    -- cp .env-template .env
    -- echo "username" > ./src/docker/secrets/DB_USERNAME
    -- echo "password" > ./src/docker/secrets/DB_PASSWORD
  • yarn nx serve - to test that it's working with the patch; no error in console
  • http://localhost:3001/v1 shows "Hello World!"
  • Stop the application
  • yarn add @nestjs/graphql to revert to the unpatched version
  • yarn nx serve - to show the unpatched error

Environment


Nest version: 7.6.17

For Tooling issues:
- Node version: 16.1.0  
- Platform: Mac 

Others:
- Package Manager: Yarn 2, with PnP enabled
- Build Tools: @nrwl/nx

Most helpful comment

This should be fixed in v7.11.0

All 7 comments

It happened to me as well due to apollo-server-fastify@^3.0.0-preview.0
a temporary fix was for us to add those lignes to package.json

"resolutions": {
    "apollo-server-env": "^2 || 3.0.0-alpha.3",
    "apollo-server-fastify": "^2 || 3.0.0-alpha.3",
    "apollo-server-core": "^2 || 3.0.0-alpha.3"
  },

forcing the resolution of apollo-server packages to the working aplha.3 version instead of the recently published preview.0 one.

forcing the resolution of apollo-server packages to the working aplha.3 version instead of the recently published preview.0 one.

Good to know that works. I tried the same but still had issues. I may not have properly cleared the yarn cache, though.

If you want to use preview.0, another temporary fix is to save this patch file to your repository:

/* Save to something like ./.yarn/patches/@nestjs/graphql.patch */

diff --git a/dist/graphql.module.js b/dist/graphql.module.js
index 959af17fda8c13b9aa8ac580d82d61cacbac2aca..d1d26ae6c8c57df6a01224194d13fd7ec173f531 100644
--- a/dist/graphql.module.js
+++ b/dist/graphql.module.js
@@ -147,6 +147,7 @@ let GraphQLModule = GraphQLModule_1 = class GraphQLModule {
             const path = this.getNormalizedPath(apolloOptions);
             const apolloServer = new ApolloServer(apolloOptions);
             const { disableHealthCheck, onHealthCheck, cors, bodyParserConfig, } = this.options;
+            yield apolloServer.start();
             yield app.register(apolloServer.createHandler({
                 disableHealthCheck,
                 onHealthCheck,

Then update your package.json to use the following:

"@nestjs/graphql": "patch:@nestjs/[email protected]#[YOUR/LOCAL/PATH]/graphql.patch"

I would be careful using the alpha prereleases — because of how npm version resolution with prereleases goes they can pull in unrelated prereleases with larger letters like the lambda prerelease I cut for some ad hoc testing. (It's intentional and relevant that preview sorts after lambda in the alphabet.)

NestJS Documentation recommends using the alpha version when using @nestjs/platform-fastify >= 7.5.0

OK, unfortunately the alpha versions are basically broken at this point because I did not understand when I published 3.0.0-lambda.0 versions that they could be pulled in transitively by ^3.0.0-alpha.0 in the directly depended alphas. The preview releases do not have this problem.

This should be fixed in v7.11.0

FYI, Apollo Server 3 is now released. Wherever you're recommending the 3.x alphas/previews, you can now just suggest v3.x directly! (For now, AS3 is on the next dist-tag rather than latest, so AS2 is still installed by default and is the default doc site; we'll update this in a few weeks once we've got more feedback in.)

Was this page helpful?
0 / 5 - 0 ratings