maticzav / graphql-middleware

Split up your GraphQL resolvers in middleware functions
MIT License
1.15k stars 58 forks source link

graphql-middleware with custom directives, breaks directives #563

Closed mashaalmemon closed 1 year ago

mashaalmemon commented 1 year ago

Hi There,

Prior to using graphql-middleware when initializing our apollo server I was simply passing our schema and typeDefs directly to the apollo server upon instantiation.

// instantiate apollo server
this._gServer = new ApolloServer({
      typeDefs: GraphqlTypeDefs.getTypeDefs(),
      resolvers: GraphqlResolvers.getMap(),
      schemaDirectives: {
        directive1: Directive1, 
        ...
      }
     ...
    });

However I am using the makeExecutableSchema and applyMiddleware functions as prescribed, and while the middleware itself is being executed as expected, our custom schema directives are no longer being executed. The code I'm using to initialize the server is as follows:

// make an executable schema
const schema: GraphQLSchema = makeExecutableSchema({
      typeDefs: GraphqlTypeDefs.getTypeDefs(),
      resolvers: GraphqlResolvers.getMap()
    });

// apply middlewares to the schema
const schemaWithMiddleware: GraphQLSchemaWithFragmentReplacements =
      applyMiddleware(schema, GraphqlPrePostApiEndpointMiddleware.execute);

// instantiate apollo server
this._gServer = new ApolloServer({
      schema: schemaWithMiddleware, // not working with directives
      schemaDirectives: {
        directive1: Directive1, 
        ...
      }
     ...
    });

Can advise how I might get directives working with graphql-middleware?

mashaalmemon commented 1 year ago

I just realized, after poking around, that applyMiddleware also takes schemaDirectives as an option, and passing them there fixed my issues. Apologies.