-
Notifications
You must be signed in to change notification settings - Fork 537
[Proposal] Split into multiple middlewares #113
Comments
I think something like this could be useful. What are the important use cases here? For performance logging it does seem useful to be able to insert some middleware after the query is parsed, but before it is executed. You might also want to do things like rejecting a query that was too complicated at the same point. It does seem like it is useful to mount graphiql separately but that doesn't seem like it should work like What is the benefit of separating |
This looks like a good and robust approach. IMO Logging & GraphiQL should be separated For |
Sorry for the late reply, but I think this is an interesting idea as long as it's optional to not break the existing API. |
For those who would like to approach this, consider unfolding one middleware out at a time for PRs rather than one huge PR that attempts to refactor everything at once ;) |
FYI, just closed a couple of other related issues in the interests of centralizing the discussion here. For context, this is the comment I made on #178:
Let's carry on from here. |
I think the most conservative option for this refactoring is to take advantage of Node's own API for HTTP stuff middleware(req: http.ClientRequest, res: http.ServerResponse)
: Promise<http.ServerResponse> I could work on a draft in the next days. |
I like @chentsulin proposal. Just add my 5cents: export function parser(opts) {
return (req, res, next) {
// ...
next();
}
}
export function validate(opts) {
return (req, res, next) {
// ...
next();
}
}
// ... other middlewares ...
// and the default middleware with callback hell 😈
export default function graphqlHTTP(opts) {
const parserMW = parser(opts_for_parser);
const validateMW = validate(opts_for_validate);
return (req, res, next) => {
parserMW(req, res, () => {
validateMW(req, res, () => {
executeMW(req, res, () => {
errorHanderMW(req, res, () => {
next(); // if needed
});
});
});
});
}
}; |
@nodkz seems reasonable. But I think it should be done in the new Express adapter. This package should just provide a toolset for such adapters. |
Any progress on this? |
Bumping with a 👍 |
Any new progress here? |
If it provides some insight, in the end for import Koa from 'koa'
import bodyParser from 'koa-bodyparser'
import { errorHandler, execute } from 'graphql-api-koa'
import schema from './schema'
const app = new Koa()
.use(errorHandler())
.use(bodyParser())
.use(execute({ schema })) |
from the issue i just closed as a dupe of this:
|
I would definitely love something like this to help handle permissions. Currently, I do the following:
I have to do permissions directly in the resolvers since we have some pretty complicated permissions going on, and it's a lot easier since I have the arguments object available for each field or query. If I could put some middleware between the resolver and what parses the query string into a javascript object it would cut down on a lot of code re-use |
Just a idea, but I think we should embrace express middleware system.
Maybe something like below:
And then we can use middlewares at any points to log and measure performance, even have more control on input, output and errors.
Eventually, more reasonable, extendable middlewares will born and enrich the whole graphql ecosystem.
Relative issue: #101 #102 #107
The text was updated successfully, but these errors were encountered: