node.js - 尝试创建 GraphQL 指令时出现 TypeError
问题描述
我正在使用 Apollo-Server/TypeScript 并利用 graphql-toolmakeExecutableSchema()
来设置模式/指令。
我目前在尝试添加准系统简单的 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-tools 指令解析器(而不是基于类的 SchemaDirectiveVisitor 方式)实现指令的方式。文档在这里
推荐阅读
- javascript - 如何使用 Quasar 构建微前端?
- wix - VS2019 中的 WiX 项目“不兼容”
- python - 如何使用一些条件(正则表达式?)修改熊猫中的一列字符串
- apache-spark - 预处理数据的最佳方法
- vb.net - VB.Net 表单身份验证 - Roles.CreateRole()
- python - 从txt文件python创建字典
- paypal-sandbox - 无法获取特定订阅的交易
- javascript - 为什么三级嵌套的 SortableJS 拖放不起作用?
- excel - Vlookup 正在返回应该显示值的空白单元格
- http - 纯 sh + telnet 中的 HTTP POST 文件:没有 curl,没有 wget