0
votes

I'm using Apollo-Server/TypeScript and utilizing graphql-tool's makeExecutableSchema() to setup schema/directives.

I'm currently getting this error when trying to add a barebones simple GraphQL Directive:

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

Here is the setup for the schema:

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;

The AuthDirective file:

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 
`;

I've been following the documentation here. Everything seems to be in order but I could be overlooking something.

What's absurd though is that the error is saying something about line 58 in the AuthDirective file. The file is only 23/24 lines long.

1

1 Answers

0
votes

Fixed the issue. I've changed the way of implementing directives using graphql-tools directive resolvers (instead of the class-based SchemaDirectiveVisitor way). Documentation here