Apollo-server: TypeError in 2.9.5

Created on 5 Oct 2019  路  11Comments  路  Source: apollographql/apollo-server

I am experiencing TypeError in [email protected]. I've not had this error in previous version and I can confirm this in PR. Starting to failing the build in circleci when upgrading the apollo packages.

Below is a log.

(node:5777) UnhandledPromiseRejectionWarning: TypeError: _schema.then is not a function
    at new ApolloServerBase (/Users/dooboolab/Github/dooboolab/hackatalk-server/node_modules/apollo-server-express/node_modules/apollo-server-core/src/ApolloServer.ts:365:40)
    at new ApolloServer (/Users/dooboolab/Github/dooboolab/hackatalk-server/node_modules/apollo-server-express/src/ApolloServer.ts:88:5)
    at Object.<anonymous> (/Users/dooboolab/Github/dooboolab/hackatalk-server/src/app.ts:43:18)
    at Generator.next (<anonymous>)
    at /Users/dooboolab/Github/dooboolab/hackatalk-server/node_modules/merge-graphql-schemas/node_modules/apollo-link/node_modules/tslib/tslib.js:107:75
    at new Promise (<anonymous>)
    at __awaiter (/Users/dooboolab/Github/dooboolab/hackatalk-server/node_modules/merge-graphql-schemas/node_modules/apollo-link/node_modules/tslib/tslib.js:103:16)
    at Object.startServer (/Users/dooboolab/Github/dooboolab/hackatalk-server/src/app.ts:27:12)
    at Object.<anonymous> (/Users/dooboolab/Github/dooboolab/hackatalk-server/src/server.ts:3:1)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Module.m._compile (/Users/dooboolab/Github/dooboolab/hackatalk-server/node_modules/ts-node/src/index.ts:493:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/dooboolab/Github/dooboolab/hackatalk-server/node_modules/ts-node/src/index.ts:496:12)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at Object.<anonymous> (/Users/dooboolab/Github/dooboolab/hackatalk-server/node_modules/ts-node/src/bin.ts:158:12)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)

-->

needs-reproduction

Most helpful comment

Perhaps there could be better type checking and error messaging.
In the offending code, apollo server assumes that if it isn't a GraphQLSchema then it is a promise of GraphQLSchema.

// apollo-core/ApolloServer.ts - Line 365
    if (_schema instanceof GraphQLSchema) {
      const derivedData = this.generateSchemaDerivedData(_schema);
      this.schema = derivedData.schema;
      this.schemaDerivedData = Promise.resolve(derivedData);
    } else {
      this.schemaDerivedData = _schema.then(schema =>
        this.generateSchemaDerivedData(schema),
      );
    }

Potentially a fix could be to convert non-promise schemas to a promise, resolve that schema promise, then type check the schema against GraphQLSchema.

I've also seen other projects (e.g. class-validator) warn of multiple versions of the library in node_modules, although this case would be a concern for the graphql library.

All 11 comments

@hyochan Still experiencing this problem? Having the same issue. Found a solution?

Thank you, @hyochan. Can either of you please provide a runnable reproduction?

@trevor-scheer Well, last commit in our project is causing the problem which I've done only the package updates. I think you can easily clone that and test it. We've made our toy project in order to share same experience such as bugs like this.

If this isn't enough, please tell me again. Thank you!

I had the same problem and looks like new release 2.9.6 fixed it

Thanks for the update @Santinell, can you please try the latest and let me know if it resolves the issue for you @hyochan?

I have yet to determine the cause, or why it would resolve itself after a patch release with presumably no targeted fix.

@trevor-scheer As you can see here same error is occuring in 2.9.6. @Santinell Are you sure 2.9.6 fixed yours?

@trevor-scheer To test this you can easily clone our repo and yarn && yarn start then you will see the below error.
image

@hyochan I was also experiencing this error.
I found that this issue was caused by different npm modules using conflicting versions of graphql.
My fix included using yarn resolutions to set the version of graphql that your modules will use.

Snippet from my package.json

{
  ...
  "dependencies":{
    "graphql": "^14.5.8"
    ...
  },
  "resolutions": {
    "type-graphql/**/graphql": "^14.5.8"
  }
}

@henry-young Can't believe I've missed this. Thank you!

Getting the same error using [email protected] and [email protected].

I added the same resolution as @henry-young suggest to my package.json, removed the node_modules folder and run yarn install once more but the error still persists.

Ok, I found the error. I passed in an object called schema instead of typeDefs. So although the error looked like the same it was actually caused by something else.

Wrong code:

const server = new ApolloServer( {
        schema,
        resolvers,
        context: authenticate,
        playground: ( config.env === 'development' ),
        introspection: ( config.env === 'development' )
    } );

Corrected:

const server = new ApolloServer( {
        typeDefs,
        resolvers,
        context: authenticate,
        playground: ( config.env === 'development' ),
        introspection: ( config.env === 'development' )
    } );

Perhaps there could be better type checking and error messaging.
In the offending code, apollo server assumes that if it isn't a GraphQLSchema then it is a promise of GraphQLSchema.

// apollo-core/ApolloServer.ts - Line 365
    if (_schema instanceof GraphQLSchema) {
      const derivedData = this.generateSchemaDerivedData(_schema);
      this.schema = derivedData.schema;
      this.schemaDerivedData = Promise.resolve(derivedData);
    } else {
      this.schemaDerivedData = _schema.then(schema =>
        this.generateSchemaDerivedData(schema),
      );
    }

Potentially a fix could be to convert non-promise schemas to a promise, resolve that schema promise, then type check the schema against GraphQLSchema.

I've also seen other projects (e.g. class-validator) warn of multiple versions of the library in node_modules, although this case would be a concern for the graphql library.

Was this page helpful?
0 / 5 - 0 ratings