Skip to content

Instantly share code, notes, and snippets.

@vassalloandrea
Created January 18, 2021 06:16
Show Gist options
  • Save vassalloandrea/e920271c71fd80fababdd442fca46a41 to your computer and use it in GitHub Desktop.
Save vassalloandrea/e920271c71fd80fababdd442fca46a41 to your computer and use it in GitHub Desktop.
Medium Morgan Winston Example - morganMiddleware GraphQL TS
import morgan, { StreamOptions } from "morgan";
import { IncomingMessage } from "http";
import Logger from "../lib/logger";
interface Request extends IncomingMessage {
body: {
query: String;
};
}
const stream: StreamOptions = {
write: (message) =>
Logger.http(message.substring(0, message.lastIndexOf("\n"))),
};
const skip = () => {
const env = process.env.NODE_ENV || "development";
return env !== "development";
};
const registerGraphQLToken = () => {
morgan.token("graphql-query", (req: Request) => `GraphQL ${req.body.query}`);
};
registerGraphQLToken();
const morganMiddleware = morgan(
":method :url :status :res[content-length] - :response-time ms\n:graphql-query",
{ stream, skip }
);
export default morganMiddleware;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment