Type-graphql: Middleware: next is not a function

Created on 11 Feb 2019  路  4Comments  路  Source: MichalLytek/type-graphql

Hi,

I have a middleware which was previously working, but now fails for some unknown reason. Here's the middleware:

export const DataLoaderMiddleware: MiddlewareFn<any> = ({ context = {} }, next) => {
  if (!context.isDataLoaderAttached) {
    context.isDataLoaderAttached = true;
    context.loaders = {};

    context.loaders.technologyViews = technologyViewsDataloader();
  }

  return next();
};

It's loaded as a globalMiddlewares:

  const schema = await TypeGraphQL.buildSchema({
    globalMiddlewares: [DataLoaderMiddleware],
    resolvers: [`${__dirname}/**/*.resolver.ts`]
  });

And here's the resulting error:

TypeError: next is not a function
    at new exports.DataLoaderMiddleware (/base/src/services/reports-mr-data/src/middleware/dataloader.middleware.ts:23:12)
    at ContainerInstance.getServiceValue (/base/node_modules/typedi/ContainerInstance.js:195:21)
    at ContainerInstance.get (/base/node_modules/typedi/ContainerInstance.js:51:21)
    at Function.Container.get (/base/node_modules/typedi/Container.js:38:36)
    at Function.getInstance (/base/node_modules/type-graphql/utils/container.js:58:36)
    at /base/node_modules/type-graphql/resolvers/helpers.js:76:70
    at Generator.next (<anonymous>)
    at /base/node_modules/tslib/tslib.js:107:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/base/node_modules/tslib/tslib.js:103:16)
    at dispatchHandler (/base/node_modules/type-graphql/resolvers/helpers.js:63:22)
    at Object.<anonymous> (/base/node_modules/type-graphql/resolvers/helpers.js:96:12)
    at Generator.next (<anonymous>)
    at /base/node_modules/tslib/tslib.js:107:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/base/node_modules/tslib/tslib.js:103:16)

I just can't figure out why it's all of a sudden stopped working?! 馃

Any help or pointers would be appreciated, thanks!

Community Question Solved

Most helpful comment

Hi,

I figured out the issue. I had my root tsconfig.json set as target: "es5". Changing to target: "es2017" solved my issue. Not sure if that's considered a bug?

Thanks!

All 4 comments

From the log it looks like it treats your DataLoaderMiddleware function as a class, so your DI container tries to create an instance of it.

I have to debug it, so please create a repository with a minimal reproducible code example.

Hi,

I figured out the issue. I had my root tsconfig.json set as target: "es5". Changing to target: "es2017" solved my issue. Not sure if that's considered a bug?

Thanks!

Makes sense! It was changed by mistake when we moved the project into a monorepo (all project inherit from a root config). Thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

itsgracian picture itsgracian  路  3Comments

reilem picture reilem  路  3Comments

maplesteve picture maplesteve  路  3Comments

avkonst picture avkonst  路  3Comments

MichalLytek picture MichalLytek  路  3Comments