【问题标题】:TypeGraphQL @Arg decorator fails with parser error when transpiling with webpack and babel-loader使用 webpack 和 babel-loader 进行编译时,TypeGraphQL @Arg 装饰器失败并出现解析器错误
【发布时间】:2020-05-17 22:22:38
【问题描述】:

我正在使用 typegraphql 从带有 @Resolver 注释的 typescript 类生成一个 graphql 解析器。只要我不添加 @Arg 装饰器来为我的解析器方法定义参数,我就可以使用 webpack 和 babel-loader 转换/捆绑我的源代码。

以下是我的 webpack 配置(至少是我相关的部分):

loader: "babel-loader",
options: {
    presets: ["@babel/preset-env", "@babel/preset-typescript"],
    plugins: [
        "@babel/plugin-transform-runtime",
        ["@babel/plugin-proposal-decorators", { legacy: true }],
        ["@babel/plugin-proposal-class-properties", { loose: true }],
    ],
},

以下是我使用 @Arg 装饰器的解析器:

@Resolver()
export default class TimesheetResolver implements TimesheetQuery {
    @Query(() => [Timesheet])
    async findAllEmployeeTimesheets(@Arg("employeeId") employeeId: string): Promise<[Timesheet]> {
        ...
    }
}

以下是我的 tsconfig 文件:

{
    "compilerOptions": {
      /* Basic Options */
      "target": "es2016",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
      "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
      "lib": ["es2016", "esnext.asynciterable"],
      "declaration": true,                      /* Generates corresponding '.d.ts' file. */
      "outDir": "lib",                          /* Redirect output structure to the directory. */
      /* Strict Type-Checking Options */
      "strict": true,                           /* Enable all strict type-checking options. */
      "allowSyntheticDefaultImports": true,     /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
      "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      /* Experimental Options */
      "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
      "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
    }
  }

Webpack 错误:

ERROR in ./src/api/timesheet/TimesheetQuery.ts 28:48
Module parse failed: Unexpected character '@' (28:48)
File was processed with these loaders:
 * ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|       var _findAllEmployeeTimesheets = _asyncToGenerator(
|       /*#__PURE__*/
>       _regeneratorRuntime.mark(function _callee(@Arg("employeeId")
|       employeeId) {
|         var timesheet, timeEntry, workBreak, clockIn, clockOut;
 @ ./src/graphql/server.ts 5:0-64 21:26-43
 @ ./src/index.ts
 @ multi ./src/index.ts

我的 webpack 配置中是否需要包含插件或其他加载器?

【问题讨论】:

  • 装饰器可能应该通过你的.tsconfig.json 文件来启用,如图所示 [here] 而不是使用 babel 插件。如果您已经在使用 typescript,plugin-proposal-class-properties 也是多余的。
  • 我添加了我的 tsconfig 文件以提供更多上下文。接下来我会考虑你的建议。

标签: typescript webpack apollo-server babel-loader typegraphql


【解决方案1】:

我将babel-plugin-transform-typescript-metadata 添加到我的 webpack.config.js 文件中 as suggested 来自 typegraphql 作者。

添加新插件解决了我的问题。以下是更新后的 webpack.config.js

use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env", "@babel/preset-typescript"],
                        plugins: [
                            "@babel/plugin-transform-runtime",
                            "babel-plugin-transform-typescript-metadata",
                            ["@babel/plugin-proposal-decorators", { legacy: true }],
                            ["@babel/plugin-proposal-class-properties", { loose: true }],
                        ],
                    },
                },

【讨论】:

  • 2020年7月还是这样吗?还是 babel 解决了这个问题?
  • 我还没有检查它是否已修复。这个解决方案仍然存在。自从我实施以来,我没有遇到任何问题。
  • 嘿兄弟我有同样的问题,但我不知道如何编译代码whit babel,你能举个例子吗?如果你这样做,我真的很感激
  • 问题是我试图在服务器上使用 webpack。我正在构建一个 graphql api 服务器并使用 webback 和 babel 加载器。这对于网络来说很好,但对于节点 api 服务器来说就不行了。我切换到 tsc transpile 我的打字稿,现在使用任何类型 orm 注释都没有任何问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-08-04
  • 2018-11-27
  • 2021-12-11
  • 2016-05-18
相关资源
最近更新 更多