Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a named function for the middleware over an anonymous #827

Merged
merged 2 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/apollo-server-adonis/src/adonisApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -51,6 +52,8 @@ export function graphqlAdonis(
},
);
};

return graphqlHandler;
}

export interface AdonisGraphiQLOptionsFunction {
Expand All @@ -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(
Expand All @@ -72,4 +75,6 @@ export function graphiqlAdonis(
},
);
};

return graphiqlHandler;
}
14 changes: 12 additions & 2 deletions packages/apollo-server-azure-functions/src/azureFunctionsApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -127,4 +135,6 @@ export function graphiqlAzureFunctions(
},
);
};

return graphiqlHandler;
}
16 changes: 14 additions & 2 deletions packages/apollo-server-express/src/expressApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export function graphqlExpress(
);
}

return (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,
Expand Down Expand Up @@ -68,6 +72,8 @@ export function graphqlExpress(
},
);
};

return graphqlHandler;
}

export interface ExpressGraphiQLOptionsFunction {
Expand All @@ -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 => {
Expand All @@ -101,4 +111,6 @@ export function graphiqlExpress(
error => next(error),
);
};

return graphiqlHandler;
}
8 changes: 6 additions & 2 deletions packages/apollo-server-koa/src/koaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -54,6 +54,8 @@ export function graphqlKoa(
},
);
};

return graphqlHandler;
}

export interface KoaGraphiQLOptionsFunction {
Expand All @@ -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 => {
Expand All @@ -76,4 +78,6 @@ export function graphiqlKoa(
},
);
};

return graphiqlHandler;
}
12 changes: 10 additions & 2 deletions packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function graphqlLambda(
);
}

return async (
const graphqlHandler = async (
event,
lambdaContext: lambda.Context,
callback: lambda.Callback,
Expand Down Expand Up @@ -73,6 +73,8 @@ export function graphqlLambda(
});
}
};

return graphqlHandler;
}

export interface LambdaGraphiQLOptionsFunction {
Expand All @@ -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 => {
Expand All @@ -115,4 +121,6 @@ export function graphiqlLambda(
},
);
};

return graphiqlHandler;
}
8 changes: 6 additions & 2 deletions packages/apollo-server-micro/src/microApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -62,6 +62,8 @@ export function microGraphql(
throw error;
}
};

return graphqlHandler;
}

export interface MicroGraphiQLOptionsFunction {
Expand All @@ -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 => {
Expand All @@ -88,4 +90,6 @@ export function microGraphiql(
},
);
};

return graphiqlHandler;
}
12 changes: 10 additions & 2 deletions packages/apollo-server-restify/src/restifyApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function graphqlRestify(
);
}

return (
const graphqlHandler = (
req: restify.Request,
res: restify.Response,
next: restify.Next,
Expand Down Expand Up @@ -69,6 +69,8 @@ export function graphqlRestify(
},
);
};

return graphqlHandler;
}

export interface RestifyGraphiQLOptionsFunction {
Expand All @@ -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 => {
Expand All @@ -108,4 +114,6 @@ export function graphiqlRestify(
},
);
};

return graphiqlHandler;
}