尝试创建GraphQL指令时发生TypeError



我使用Apollo Server/TypeScript并利用graphql工具的makeExecutableSchema()来设置模式/指令。

我目前在尝试添加一个简单的GraphQL指令时遇到了这个错误:

TypeError: Class constructor SchemaDirectiveVisitor cannot be invoked without 'new' at new AuthDirective
(/home/node/app/src/api/directives/AuthDirective.ts:58:42)

以下是模式的设置:

import AuthDirective, { authTypeDefs } from "./directives/AuthDirective";
import { makeExecutableSchema } from "graphql-tools";
const schema = makeExecutableSchema({
resolvers: [...],
typeDefs: [...], // authTypeDefs is included here
schemaDirectives: {
auth: AuthDirective,
},
});
export default schema;

AuthDirective文件:

import { SchemaDirectiveVisitor } from "graphql-tools";
import { defaultFieldResolver } from "graphql";
export default class AuthDirective extends SchemaDirectiveVisitor {
public visitFieldDefinition(field) {
console.log("VISIT FIELD: ", field);
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
return resolve.apply(this, args);
};
}
}
export const authTypeDefs = `
enum AppRole {
USER
ADMIN
}
directive @auth(
requires: AppRole! = USER
) on FIELD_DEFINITION 
`;

我一直在关注这里的文档。一切似乎都井然有序,但我可能忽略了什么。

然而,荒谬的是,这个错误说的是AuthDirective文件中的第58行。该文件只有23/24行长。

修复了该问题。我已经改变了使用graphql工具指令解析器实现指令的方式(而不是基于类的SchemaDirectiveVisitor方式(。此处的文档

最新更新