ApolloServer constructor modules combination with schemaDirectives, schemaDirectives does not work

here is my code:

class hasServiceDirective extends SchemaDirectiveVisitor {

  visitFieldDefinition(field) {

      const { resolve = defaultFieldResolver } = field;

      // reading directive args

      console.log(this.visitedType.name);

      const { service } = this.args;

      console.log("Inside FieldDefinition function");

      field.resolve = async (...args) => {

        const context = args[2];

        const services = context.services;

          if(services.includes(service)) {

            return await resolve.apply(this, args);

          } else  {

            return "";

          }

      }

  }

}
const server = new ApolloServer({

   modules: [blogModule],

   schemaDirectives: {

    hasService: hasServiceDirective,

  },

  plugins: [ loggingPlugin ],

  context: ({event}) => {

    const headers = event.headers;

    const userId = headers.userid || null;

    const services = headers.services || null;

    return { 

      userId,

      services,

      prisma

     };

  },

   playground: {

    //endpoint: "/dev/graphql"

    settings: {

      'schema.polling.enable': false,

      'schema.polling.interval': 5000,

    }

   }

});
const books = [

  {

    title: 'The Awakening',

    author: 'Kate Chopin',

  },

  {

    title: 'City of Glass',

    author: 'Paul Auster',

  },

];

const users = [

  {

    name: 'Manish',

    password: 'manish@123',

  },

  {

    name: 'Monu',

    password: 'monu@123',

  },

];

If i user typeDefs, resolver property in apolloServer, my custom Directive is working but when i use modules, it isn’t working. I will have many typeDefs and resolvers in future so I m using modules property of ApolloServer constructor to call schemas.

What version of Apollo Server are you using?

Hi @abernix sorry for late reply.
I was using apollo server v3.0…I found that apollo server 3.0 does not have documentation for customDirective. Now I m on 2.xx.x version. Now it is working.

I assume apollo server v3.0 does not support Custom Directives?

This is covered in the migration guide for v3.

Ah yes, and we’ve restored the custom directives article in the 3.x documentation with updated instructions.

1 Like

Thanks @abernix and @StephenBarlow for the update.

1 Like