maticzav / graphql-middleware

Split up your GraphQL resolvers in middleware functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Easier way to apply multiple middlewares to specific resolvers

omar-rubix opened this issue · comments

I'm not sure if I'm missing something but as far as I can tell, the only way to apply multiple middlewares to a specific resolver is to do the following as per this article

const middleware1 = {
  Query: {
    hello: logInput,
    bye: logInput,
  },
}

const middleware2 = {
  Query: {
    bye: logResult,
  },
}

const middlewares = [middleware1, middleware2]

Wouldn't it be better to do it this way instead? I feel like the above method would get out of hand pretty fast.

const loggingMiddleware = {
  Query: {
    hello: logInput,
    bye: {logInput, logResult} //OR [logInput, logResult]
  },
}

const middlewares = [loggingMiddleware]

I tried to check if this was possible but I got the following error:

Error: Expected Query.hello to be a function but found object

I don't mean to complain or anything but I also feel like the documentation needs to be a little better. It's incomplete and a bit unclear.

Thanks!

@omar-rubix thank you for the input. In what way does documentation seem incomplete or unclear?

@maticzav Thanks for your reply! In the documentation, the syntax of applying middleware to specific resolvers is unclear unless you've read this article.

Having said that, how to apply multiple middlewares to the same resolver is also unclear. You can either use separate objects or a function like in graphql-shield.

The section on middleware generators might benefit from a clearer explanation. Maybe a use case or an example, I had to dig into the source code of graphql-shield to understand how it worked.

The documentMiddleware part is kinda vague as well.

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.