From 36f1421c65d11c9b9593668f5d87ce2cc6cba620 Mon Sep 17 00:00:00 2001 From: Sangmin Yoon Date: Tue, 10 Dec 2019 22:31:10 +0900 Subject: [PATCH] optimize createHandlerResolver --- src/resolvers/create.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/resolvers/create.ts b/src/resolvers/create.ts index 97fa988f6..fc771012a 100644 --- a/src/resolvers/create.ts +++ b/src/resolvers/create.ts @@ -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, @@ -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 = { 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[] = 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); + } }); }; }