Use named arrow functions for all middlewares, including GraphiQL.

As originally reported in [0], this should make debugging performance issues
easier, since the name of the function will be properly displayed/shown in
flame-graphs.

[0]: https://github.com/apollographql/apollo-server/pull/827
This commit is contained in:
Jesse Rosenberger 2018-04-04 22:56:07 +03:00
parent e5e543b125
commit f8ca710279
7 changed files with 65 additions and 14 deletions

View file

@ -25,7 +25,8 @@ export function graphqlAdonis(
`Apollo Server expects exactly one argument, got ${arguments.length}`,
);
}
return (ctx: AdonisContext): Promise<void> => {
const graphqlHandler = (ctx: AdonisContext): Promise<void> => {
const { request, response } = ctx;
const method = request.method();
const query = method === 'POST' ? request.post() : request.get();
@ -51,6 +52,8 @@ export function graphqlAdonis(
},
);
};
return graphqlHandler;
}
export interface AdonisGraphiQLOptionsFunction {
@ -60,7 +63,7 @@ export interface AdonisGraphiQLOptionsFunction {
export function graphiqlAdonis(
options: GraphiQL.GraphiQLData | AdonisGraphiQLOptionsFunction,
) {
return (ctx: AdonisContext): Promise<void> => {
const graphiqlHandler = (ctx: AdonisContext): Promise<void> => {
const { request, response } = ctx;
const query = request.get();
return GraphiQL.resolveGraphiQLString(query, options, ctx).then(
@ -72,4 +75,6 @@ export function graphiqlAdonis(
},
);
};
return graphiqlHandler;
}

View file

@ -50,7 +50,10 @@ export function graphqlAzureFunctions(
);
}
return (httpContext: IHttpContext, request: IFunctionRequest) => {
const graphqlHandler = (
httpContext: IHttpContext,
request: IFunctionRequest,
) => {
const queryRequest = {
method: request.method,
options: options,
@ -85,6 +88,8 @@ export function graphqlAzureFunctions(
httpContext.done(null, result);
});
};
return graphqlHandler;
}
/* This Azure Functions Handler returns the html for the GraphiQL interactive query UI
@ -101,7 +106,10 @@ export function graphqlAzureFunctions(
export function graphiqlAzureFunctions(
options: GraphiQLData | AzureFunctionsGraphiQLOptionsFunction,
) {
return (httpContext: IHttpContext, request: IFunctionRequest) => {
const graphiqlHandler = (
httpContext: IHttpContext,
request: IFunctionRequest,
) => {
const query = request.query;
resolveGraphiQLString(query, options, httpContext, request).then(
@ -127,4 +135,6 @@ export function graphiqlAzureFunctions(
},
);
};
return graphiqlHandler;
}

View file

@ -36,7 +36,11 @@ export function graphqlExpress(
);
}
return function graphql(req: express.Request, res: express.Response, next): void {
const graphqlHandler = (
req: express.Request,
res: express.Response,
next,
): void => {
runHttpQuery([req, res], {
method: req.method,
options: options,
@ -68,6 +72,8 @@ export function graphqlExpress(
},
);
};
return graphqlHandler;
}
export interface ExpressGraphiQLOptionsFunction {
@ -90,7 +96,11 @@ export interface ExpressGraphiQLOptionsFunction {
export function graphiqlExpress(
options: GraphiQL.GraphiQLData | ExpressGraphiQLOptionsFunction,
) {
return (req: express.Request, res: express.Response, next) => {
const graphiqlHandler = (
req: express.Request,
res: express.Response,
next,
) => {
const query = req.url && url.parse(req.url, true).query;
GraphiQL.resolveGraphiQLString(query, options, req).then(
graphiqlString => {
@ -101,4 +111,6 @@ export function graphiqlExpress(
error => next(error),
);
};
return graphiqlHandler;
}

View file

@ -27,7 +27,7 @@ export function graphqlKoa(
);
}
return (ctx: koa.Context): Promise<void> => {
const graphqlHandler = (ctx: koa.Context): Promise<void> => {
return runHttpQuery([ctx], {
method: ctx.request.method,
options: options,
@ -54,6 +54,8 @@ export function graphqlKoa(
},
);
};
return graphqlHandler;
}
export interface KoaGraphiQLOptionsFunction {
@ -63,7 +65,7 @@ export interface KoaGraphiQLOptionsFunction {
export function graphiqlKoa(
options: GraphiQL.GraphiQLData | KoaGraphiQLOptionsFunction,
) {
return (ctx: koa.Context) => {
const graphiqlHandler = (ctx: koa.Context) => {
const query = ctx.request.query;
return GraphiQL.resolveGraphiQLString(query, options, ctx).then(
graphiqlString => {
@ -76,4 +78,6 @@ export function graphiqlKoa(
},
);
};
return graphiqlHandler;
}

View file

@ -34,7 +34,7 @@ export function graphqlLambda(
);
}
return async (
const graphqlHandler = async (
event,
lambdaContext: lambda.Context,
callback: lambda.Callback,
@ -73,6 +73,8 @@ export function graphqlLambda(
});
}
};
return graphqlHandler;
}
export interface LambdaGraphiQLOptionsFunction {
@ -95,7 +97,11 @@ export interface LambdaGraphiQLOptionsFunction {
export function graphiqlLambda(
options: GraphiQL.GraphiQLData | LambdaGraphiQLOptionsFunction,
) {
return (event, lambdaContext: lambda.Context, callback: lambda.Callback) => {
const graphiqlHandler = (
event,
lambdaContext: lambda.Context,
callback: lambda.Callback,
) => {
const query = event.queryStringParameters;
GraphiQL.resolveGraphiQLString(query, options, event, lambdaContext).then(
graphiqlString => {
@ -115,4 +121,6 @@ export function graphiqlLambda(
},
);
};
return graphiqlHandler;
}

View file

@ -25,7 +25,7 @@ export function microGraphql(
);
}
return async function(req: IncomingMessage, res: ServerResponse) {
const graphqlHandler = async (req: IncomingMessage, res: ServerResponse) => {
let query;
if (req.method === 'POST') {
try {
@ -62,6 +62,8 @@ export function microGraphql(
throw error;
}
};
return graphqlHandler;
}
export interface MicroGraphiQLOptionsFunction {
@ -73,7 +75,7 @@ export interface MicroGraphiQLOptionsFunction {
export function microGraphiql(
options: GraphiQL.GraphiQLData | MicroGraphiQLOptionsFunction,
): RequestHandler {
return (req: IncomingMessage, res: ServerResponse) => {
const graphiqlHandler = (req: IncomingMessage, res: ServerResponse) => {
const query = (req.url && url.parse(req.url, true).query) || {};
return GraphiQL.resolveGraphiQLString(query, options, req).then(
graphiqlString => {
@ -88,4 +90,6 @@ export function microGraphiql(
},
);
};
return graphiqlHandler;
}

View file

@ -35,7 +35,7 @@ export function graphqlRestify(
);
}
return (
const graphqlHandler = (
req: restify.Request,
res: restify.Response,
next: restify.Next,
@ -69,6 +69,8 @@ export function graphqlRestify(
},
);
};
return graphqlHandler;
}
export interface RestifyGraphiQLOptionsFunction {
@ -91,7 +93,11 @@ export interface RestifyGraphiQLOptionsFunction {
export function graphiqlRestify(
options: GraphiQL.GraphiQLData | RestifyGraphiQLOptionsFunction,
) {
return (req: restify.Request, res: restify.Response, next: restify.Next) => {
const graphiqlHandler = (
req: restify.Request,
res: restify.Response,
next: restify.Next,
) => {
const query = (req.url && url.parse(req.url, true).query) || {};
GraphiQL.resolveGraphiQLString(query, options, req).then(
graphiqlString => {
@ -108,4 +114,6 @@ export function graphiqlRestify(
},
);
};
return graphiqlHandler;
}