Skip to content

Commit

Permalink
optimize getParams
Browse files Browse the repository at this point in the history
  • Loading branch information
sixmen committed Dec 10, 2019
1 parent e238e0a commit 4e5113a
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions src/resolvers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,59 @@ import { Middleware, MiddlewareFn, MiddlewareClass } from "../interfaces/Middlew
import { IOCContainer } from "../utils/container";
import { AuthMiddleware } from "../helpers/auth-middleware";
import { convertArgsToInstance, convertArgToInstance } from "./convert-args";
import { isPromise } from "../utils/isPromise";

export async function getParams(
export function getParams(
params: ParamMetadata[],
resolverData: ResolverData<any>,
globalValidate: boolean | ValidatorOptions,
pubSub: PubSubEngine,
): Promise<any[]> {
return Promise.all(
params
.sort((a, b) => a.index - b.index)
.map(async paramInfo => {
switch (paramInfo.kind) {
case "args":
return await validateArg(
convertArgsToInstance(paramInfo, resolverData.args),
globalValidate,
paramInfo.validate,
);
case "arg":
return await validateArg(
convertArgToInstance(paramInfo, resolverData.args),
globalValidate,
paramInfo.validate,
);
case "context":
if (paramInfo.propertyName) {
return resolverData.context[paramInfo.propertyName];
}
return resolverData.context;
case "root":
const rootValue = paramInfo.propertyName
? resolverData.root[paramInfo.propertyName]
: resolverData.root;
if (!paramInfo.getType) {
return rootValue;
}
return convertToType(paramInfo.getType(), rootValue);
case "info":
return resolverData.info;
case "pubSub":
if (paramInfo.triggerKey) {
return (payload: any) => pubSub.publish(paramInfo.triggerKey!, payload);
}
return pubSub;
case "custom":
return await paramInfo.resolver(resolverData);
}
}),
);
): Promise<any[]> | any[] {
const paramValues = params
.sort((a, b) => a.index - b.index)
.map(paramInfo => {
switch (paramInfo.kind) {
case "args":
return validateArg(
convertArgsToInstance(paramInfo, resolverData.args),
globalValidate,
paramInfo.validate,
);
case "arg":
return validateArg(
convertArgToInstance(paramInfo, resolverData.args),
globalValidate,
paramInfo.validate,
);
case "context":
if (paramInfo.propertyName) {
return resolverData.context[paramInfo.propertyName];
}
return resolverData.context;
case "root":
const rootValue = paramInfo.propertyName
? resolverData.root[paramInfo.propertyName]
: resolverData.root;
if (!paramInfo.getType) {
return rootValue;
}
return convertToType(paramInfo.getType(), rootValue);
case "info":
return resolverData.info;
case "pubSub":
if (paramInfo.triggerKey) {
return (payload: any) => pubSub.publish(paramInfo.triggerKey!, payload);
}
return pubSub;
case "custom":
return paramInfo.resolver(resolverData);
}
});
if (paramValues.some(isPromise)) {
return Promise.all(paramValues);
} else {
return paramValues;
}
}

export function applyAuthChecker(
Expand Down

0 comments on commit 4e5113a

Please sign in to comment.