0
votes

I was wondering if there is anyway to configure a directive. I have some options I load during the startup I would like to pass to the directive.

Edit: I known that I can pass options from the schema, but that's not what I'm looking for.

new ApolloServer({
typeDefs: [schema, constraintDirectiveTypeDefs],
schemaDirectives: { myDirect: Mydirective}
});

or

SchemaDirectiveVisitor.visitSchemaDirectives(schema, {
constraint: Mydirective
});

Something like :

export class ConstraintDirective extends SchemaDirectiveVisitor {

constuctor(options) {
}

visitInputFieldDefinition(field: GraphQLInputField): void {
    
}

Regards

1

1 Answers

1
votes

What you pass to ApolloServer is a class, not its instance. At best, you could dynamically create the class:

const server = new ApolloServer({
  ...
  schemaDirectives: {
    foo: createFooDirective(SOME_VALUE)
  },
})

const createFooDirective = (bar) => {
  return class FooDirective extends SchemaDirectiveVisitor {
    visitFieldDefinition(field) {
      // SOME_VALUE is now available as bar
    }
  }
}