Skip to content

Commit

Permalink
optimize createHandlerResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
sixmen committed Dec 10, 2019
1 parent 4e5113a commit 36f1421
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/resolvers/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getParams, applyMiddlewares, applyAuthChecker } from "./helpers";
import { convertToType } from "../helpers/types";
import { BuildContext } from "../schema/build-context";
import { ResolverData } from "../interfaces";
import { isPromise } from "../utils";

export function createHandlerResolver(
resolverMetadata: BaseResolverMetadata,
Expand All @@ -24,17 +25,23 @@ export function createHandlerResolver(
const middlewares = globalMiddlewares.concat(resolverMetadata.middlewares!);
applyAuthChecker(middlewares, authMode, authChecker, resolverMetadata.roles);

return async (root, args, context, info) => {
return (root, args, context, info) => {
const resolverData: ResolverData<any> = { root, args, context, info };
const targetInstance = container.getInstance(resolverMetadata.target, resolverData);
return applyMiddlewares(container, resolverData, middlewares, async () => {
const params: any[] = await getParams(
return applyMiddlewares(container, resolverData, middlewares, () => {
const params: Promise<any[]> | any[] = getParams(
resolverMetadata.params!,
resolverData,
globalValidate,
pubSub,
);
return targetInstance[resolverMetadata.methodName].apply(targetInstance, params);
if (isPromise(params)) {
return params.then(resolvedParams =>
targetInstance[resolverMetadata.methodName].apply(targetInstance, resolvedParams),
);
} else {
return targetInstance[resolverMetadata.methodName].apply(targetInstance, params);
}
});
};
}
Expand Down

0 comments on commit 36f1421

Please sign in to comment.