0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【NestJS/GraphQL/DataLoader】Context creation failed: dataloader_1.default is not a constructor

Posted at

環境

条件

以下ような形式でdataloaderimportしている。

import DataLoader from 'dataloader';

エラー内容

サーバは起動するが、GraphQL query を実行した時に、以下のようなエラー(Context creation failed: dataloader_1.default is not a constructor)が出る。

{
  "error": {
    "errors": [
      {
        "message": "Context creation failed: dataloader_1.default is not a constructor",
        "extensions": {
          "code": "INTERNAL_SERVER_ERROR",
          "exception": {
            "stacktrace": [
              "TypeError: Context creation failed: dataloader_1.default is not a constructor",
              "    at CompanyLoader.createLoaders (/nestjs-sample/src/modules/company/company-loader/company.loader.ts:17:34)",
              "    at Object.context (/nestjs-sample/src/app.module.ts:27:41)",
              "    at ApolloServer.targetOptions.context (/nestjs-sample/node_modules/@nestjs/apollo/dist/drivers/apollo-base.driver.js:150:51)",
              "    at ApolloServer.graphQLServerOptions (/nestjs-sample/node_modules/apollo-server-core/src/ApolloServer.ts:914:24)",
              "    at processTicksAndRejections (node:internal/process/task_queues:96:5)"
            ]
          }
        }
      }
    ]
  }
}

回避策 1

tsconfig.json"compilerOptions"で、"esModuleInterop": trueにする。


  "compilerOptions": {
    "module": "commonjs",
      :
    "esModuleInterop": true,

回避策 2

以下の方法でimportする。

import * as DataLoader from 'dataloader';

// instead of
import DataLoader from 'dataloader';
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?