无法读取未定义的属性“上下文”-GraphQL

FBS*_*BSO 4 express graphql typegraphql

我正在使用 Typescript、Express、TypeORM、GraphQL 和 TypeGraphQL 来构建一个允许用户登录的小应用程序。

但是,当我bye在 GraphQL 操场上点击我的测试查询时,我得到:

Cannot read property 'context' of undefined
"TypeError: Cannot read property 'context' of undefined",
            "    at new exports.isAuth // isAuth is a JS file I wrote
Run Code Online (Sandbox Code Playgroud)

我的上下文.js

import { Request, Response } from "express";

export interface MyContext {
  req: Request;
  res: Response;
  payload?: { userId: string };
}
Run Code Online (Sandbox Code Playgroud)

isAuth.js

import { MiddlewareFn } from "type-graphql";
import { verify } from "jsonwebtoken";
import { MyContext } from "./MyContext";

export const isAuth: MiddlewareFn<MyContext> = ({ context }, next) => {
  const authorization = context.req.headers["authorization"];

  if (!authorization) {
    throw new Error("not authorized");
  }
...
Run Code Online (Sandbox Code Playgroud)

用户解析器

  @Query(() => String)
  @UseMiddleware(isAuth)
  bye(@Ctx() { payload }: MyContext) {
    console.log(payload);
    return `your user id is: ${payload!.userId}`;
  }
Run Code Online (Sandbox Code Playgroud)

我不确定为什么context文件中未定义isAuth.js

FBS*_*BSO 5

解决感谢:https : //github.com/MichalLytek/type-graphql/issues/433

1)进入 ./tsconfig.json

2)"target": "es5"改为"target": "es6"