type-graphql
Advanced tools
Comparing version 0.11.0 to 0.11.1
@@ -1,1 +0,1 @@ | ||
export declare function ArgsType(name?: string): ClassDecorator; | ||
export declare function ArgsType(): ClassDecorator; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const metadata_storage_1 = require("../metadata/metadata-storage"); | ||
function ArgsType(name) { | ||
function ArgsType() { | ||
return target => { | ||
metadata_storage_1.MetadataStorage.collectArgsMetadata({ | ||
name: name || target.name, | ||
name: target.name, | ||
target, | ||
@@ -9,0 +9,0 @@ }); |
@@ -31,8 +31,7 @@ "use strict"; | ||
if (isResolver) { | ||
const methodName = propertyKey; | ||
metadata_storage_1.MetadataStorage.collectFieldResolverMetadata({ | ||
kind: "internal", | ||
methodName, | ||
methodName: propertyKey, | ||
target: prototype.constructor, | ||
handler: isResolverMethod ? prototype[methodName] : undefined, | ||
handler: isResolverMethod ? prototype[propertyKey] : undefined, | ||
}); | ||
@@ -39,0 +38,0 @@ } |
@@ -1,1 +0,3 @@ | ||
export declare function FieldResolver(): MethodDecorator; | ||
import { ReturnTypeFunc, AdvancedOptions } from "../types/decorators"; | ||
export declare function FieldResolver(options?: AdvancedOptions): MethodDecorator; | ||
export declare function FieldResolver(returnTypeFunction?: ReturnTypeFunc, options?: AdvancedOptions): MethodDecorator; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const metadata_storage_1 = require("../metadata/metadata-storage"); | ||
function FieldResolver() { | ||
const errors_1 = require("../errors"); | ||
const decorators_1 = require("../helpers/decorators"); | ||
const findType_1 = require("../helpers/findType"); | ||
function FieldResolver(returnTypeFuncOrOptions, maybeOptions) { | ||
return (prototype, propertyKey) => { | ||
const methodName = propertyKey; | ||
if (typeof propertyKey === "symbol") { | ||
throw new errors_1.SymbolKeysNotSupportedError(); | ||
} | ||
let getType; | ||
let typeOptions; | ||
const { options, returnTypeFunc } = decorators_1.getTypeDecoratorParams(returnTypeFuncOrOptions, maybeOptions); | ||
// try to get return type info | ||
try { | ||
const typeInfo = findType_1.findType({ | ||
metadataKey: "design:returntype", | ||
prototype, | ||
propertyKey, | ||
returnTypeFunc, | ||
typeOptions: options, | ||
}); | ||
typeOptions = typeInfo.typeOptions; | ||
getType = typeInfo.getType; | ||
// tslint:disable-next-line:no-empty | ||
} | ||
catch (_a) { } | ||
metadata_storage_1.MetadataStorage.collectFieldResolverMetadata({ | ||
kind: "external", | ||
methodName, | ||
methodName: propertyKey, | ||
target: prototype.constructor, | ||
handler: prototype[methodName], | ||
handler: prototype[propertyKey], | ||
getType, | ||
typeOptions, | ||
description: options.description, | ||
deprecationReason: options.deprecationReason, | ||
}); | ||
@@ -13,0 +39,0 @@ }; |
@@ -6,3 +6,3 @@ "use strict"; | ||
return target => { | ||
const getParentType = objectTypeOrTypeFunc | ||
const getObjectType = objectTypeOrTypeFunc | ||
? objectTypeOrTypeFunc.prototype | ||
@@ -16,3 +16,3 @@ ? () => objectTypeOrTypeFunc | ||
target, | ||
getParentType, | ||
getObjectType, | ||
}); | ||
@@ -19,0 +19,0 @@ }; |
import { MiddlewareFn } from "../interfaces/Middleware"; | ||
import { AuthChecker } from "../interfaces/auth-checker"; | ||
export declare function AuthMiddleware(authChecker: AuthChecker, roles: string[]): MiddlewareFn; | ||
import { AuthChecker, AuthMode } from "../interfaces/auth-checker"; | ||
export declare function AuthMiddleware(authChecker: AuthChecker, authMode: AuthMode, roles: string[]): MiddlewareFn; |
@@ -12,7 +12,12 @@ "use strict"; | ||
const errors_1 = require("../errors"); | ||
function AuthMiddleware(authChecker, roles) { | ||
function AuthMiddleware(authChecker, authMode, roles) { | ||
return (action, next) => __awaiter(this, void 0, void 0, function* () { | ||
const accessGranted = yield authChecker(action, roles); | ||
if (!accessGranted) { | ||
throw roles.length === 0 ? new errors_1.UnauthorizedError() : new errors_1.ForbiddenError(); | ||
if (authMode === "null") { | ||
return null; | ||
} | ||
else if (authMode === "error") { | ||
throw roles.length === 0 ? new errors_1.UnauthorizedError() : new errors_1.ForbiddenError(); | ||
} | ||
} | ||
@@ -19,0 +24,0 @@ return next(); |
import { ReturnTypeFunc, TypeOptions, TypeValueThunk } from "../types/decorators"; | ||
export declare type MetadataKey = "design:type" | "design:returntype" | "design:paramtypes"; | ||
export interface TypeInfo { | ||
@@ -7,3 +8,3 @@ getType: TypeValueThunk; | ||
export interface GetTypeParams { | ||
metadataKey: "design:type" | "design:returntype" | "design:paramtypes"; | ||
metadataKey: MetadataKey; | ||
prototype: Object; | ||
@@ -10,0 +11,0 @@ propertyKey: string; |
@@ -48,3 +48,7 @@ "use strict"; | ||
} | ||
// skip simple types | ||
// skip converting scalars (object scalar mostly) | ||
if (Target instanceof graphql_1.GraphQLScalarType) { | ||
return data; | ||
} | ||
// skip converting simple types | ||
if (simpleTypes.includes(data.constructor)) { | ||
@@ -51,0 +55,0 @@ return data; |
import { ActionData } from "../types/action-data"; | ||
export declare type AuthChecker<ContextType = {}> = (actionData: ActionData<ContextType>, roles: string[]) => boolean | Promise<boolean>; | ||
export declare type AuthMode = "error" | "null"; |
export interface EnumMetadata { | ||
enumObj: object; | ||
name: string; | ||
description?: string; | ||
description: string | undefined; | ||
} |
@@ -9,7 +9,7 @@ import { ParamMetadata } from "./param-metadata"; | ||
typeOptions: TypeOptions; | ||
description: string | undefined; | ||
deprecationReason: string | undefined; | ||
params?: ParamMetadata[]; | ||
description?: string; | ||
deprecationReason?: string; | ||
roles?: string[]; | ||
middlewares?: Array<Middleware<any>>; | ||
} |
@@ -17,8 +17,8 @@ import { ValidatorOptions } from "class-validator"; | ||
kind: "context"; | ||
propertyName?: string; | ||
propertyName: string | undefined; | ||
} | ||
export interface RootParamMetadata extends BasicParamMetadata { | ||
kind: "root"; | ||
propertyName?: string; | ||
getType?: TypeValueThunk; | ||
propertyName: string | undefined; | ||
getType: TypeValueThunk | undefined; | ||
} | ||
@@ -28,3 +28,3 @@ export interface CommonArgMetadata extends BasicParamMetadata { | ||
typeOptions: TypeOptions; | ||
validate?: boolean | ValidatorOptions; | ||
validate: boolean | ValidatorOptions | undefined; | ||
} | ||
@@ -34,3 +34,3 @@ export interface ArgParamMetadata extends CommonArgMetadata { | ||
name: string; | ||
description?: string; | ||
description: string | undefined; | ||
} | ||
@@ -37,0 +37,0 @@ export interface ArgsParamMetadata extends CommonArgMetadata { |
@@ -7,3 +7,3 @@ import { TypeValueThunk, TypeOptions, ClassTypeResolver, SubscriptionFilterFunc } from "../../types/decorators"; | ||
target: Function; | ||
handler?: Function; | ||
handler: Function | undefined; | ||
params?: ParamMetadata[]; | ||
@@ -22,12 +22,15 @@ roles?: string[]; | ||
kind: "internal" | "external"; | ||
handler?: Function; | ||
getParentType?: ClassTypeResolver; | ||
description?: string; | ||
deprecationReason?: string; | ||
getType?: TypeValueThunk; | ||
typeOptions?: TypeOptions; | ||
getObjectType?: ClassTypeResolver; | ||
} | ||
export interface SubscriptionResolverMetadata extends ResolverMetadata { | ||
topics: string[]; | ||
filter?: SubscriptionFilterFunc; | ||
filter: SubscriptionFilterFunc | undefined; | ||
} | ||
export interface ResolverClassMetadata { | ||
target: Function; | ||
getParentType: ClassTypeResolver; | ||
getObjectType: ClassTypeResolver; | ||
} |
@@ -39,3 +39,3 @@ import { ResolverMetadata, ClassMetadata, FieldMetadata, ParamMetadata, FieldResolverMetadata, AuthorizedMetadata, EnumMetadata, UnionMetadata, UnionMetadataWithSymbol, ResolverClassMetadata, SubscriptionResolverMetadata, MiddlewareMetadata } from "./definitions"; | ||
private static findFieldRoles(target, fieldName); | ||
private static mapMetadatasToMiddlewares(metadata); | ||
private static mapMetadataToMiddlewares(metadata); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const errors_1 = require("../errors"); | ||
class MetadataStorage { | ||
@@ -53,3 +54,2 @@ static collectQueryHandlerMetadata(definition) { | ||
// TODO: disable next build attempts | ||
this.buildFieldResolverMetadata(this.fieldResolvers); | ||
this.buildClassMetadata(this.objectTypes); | ||
@@ -59,2 +59,3 @@ this.buildClassMetadata(this.inputTypes); | ||
this.buildClassMetadata(this.interfaceTypes); | ||
this.buildFieldResolverMetadata(this.fieldResolvers); | ||
this.buildResolversMetadata(this.queries); | ||
@@ -87,10 +88,3 @@ this.buildResolversMetadata(this.mutations); | ||
field.params = this.params.filter(param => param.target === field.target && field.name === param.methodName); | ||
field.middlewares = this.mapMetadatasToMiddlewares(this.middlewares.filter(middleware => middleware.target === field.target && middleware.fieldName === field.name)); | ||
if (field.params.length === 0) { | ||
// no params = try to get params from field resolver | ||
const fieldResolver = this.fieldResolvers.find(resolver => resolver.methodName === field.name && resolver.getParentType() === field.target); | ||
if (fieldResolver) { | ||
field.params = fieldResolver.params; | ||
} | ||
} | ||
field.middlewares = this.mapMetadataToMiddlewares(this.middlewares.filter(middleware => middleware.target === field.target && middleware.fieldName === field.name)); | ||
}); | ||
@@ -104,3 +98,3 @@ def.fields = fields; | ||
def.roles = this.findFieldRoles(def.target, def.methodName); | ||
def.middlewares = this.mapMetadatasToMiddlewares(this.middlewares.filter(middleware => middleware.target === def.target && def.methodName === middleware.fieldName)); | ||
def.middlewares = this.mapMetadataToMiddlewares(this.middlewares.filter(middleware => middleware.target === def.target && def.methodName === middleware.fieldName)); | ||
}); | ||
@@ -111,7 +105,42 @@ } | ||
definitions.forEach(def => { | ||
def.roles = def.roles || this.fields.find(field => field.name === def.methodName).roles; | ||
def.getParentType = | ||
def.roles = this.findFieldRoles(def.target, def.methodName); | ||
def.getObjectType = | ||
def.kind === "external" | ||
? this.resolvers.find(resolver => resolver.target === def.target).getParentType | ||
? this.resolvers.find(resolver => resolver.target === def.target).getObjectType | ||
: () => def.target; | ||
if (def.kind === "external") { | ||
const objectTypeCls = this.resolvers.find(resolver => resolver.target === def.target) | ||
.getObjectType(); | ||
const objectType = this.objectTypes.find(objTypeDef => objTypeDef.target === objectTypeCls); | ||
const objectTypeField = objectType.fields.find(fieldDef => fieldDef.name === def.methodName); | ||
if (!objectTypeField) { | ||
if (!def.getType || !def.typeOptions) { | ||
throw new errors_1.NoExplicitTypeError(def.target.name, def.methodName); | ||
} | ||
const fieldMetadata = { | ||
name: def.methodName, | ||
getType: def.getType, | ||
target: objectTypeCls, | ||
typeOptions: def.typeOptions, | ||
deprecationReason: def.deprecationReason, | ||
description: def.description, | ||
roles: def.roles, | ||
middlewares: def.middlewares, | ||
params: def.params, | ||
}; | ||
this.collectClassFieldMetadata(fieldMetadata); | ||
objectType.fields.push(fieldMetadata); | ||
} | ||
else { | ||
if (objectTypeField.params.length === 0) { | ||
objectTypeField.params = def.params; | ||
} | ||
if (def.roles) { | ||
objectTypeField.roles = def.roles; | ||
} | ||
else if (objectTypeField.roles) { | ||
def.roles = objectTypeField.roles; | ||
} | ||
} | ||
} | ||
}); | ||
@@ -126,3 +155,3 @@ } | ||
} | ||
static mapMetadatasToMiddlewares(metadata) { | ||
static mapMetadataToMiddlewares(metadata) { | ||
return metadata | ||
@@ -129,0 +158,0 @@ .map(m => m.middlewares) |
{ | ||
"name": "type-graphql", | ||
"version": "0.11.0", | ||
"version": "0.11.1", | ||
"main": "./index.js", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -17,5 +17,5 @@ "use strict"; | ||
const targetInstance = container_1.IOCContainer.getInstance(resolverMetadata.target); | ||
const { validate: globalValidate, authChecker, pubSub, globalMiddlewares } = build_context_1.BuildContext; | ||
const { validate: globalValidate, authChecker, authMode, pubSub, globalMiddlewares, } = build_context_1.BuildContext; | ||
const middlewares = globalMiddlewares.concat(resolverMetadata.middlewares); | ||
helpers_1.applyAuthChecker(middlewares, authChecker, resolverMetadata.roles); | ||
helpers_1.applyAuthChecker(middlewares, authMode, authChecker, resolverMetadata.roles); | ||
return (root, args, context, info) => __awaiter(this, void 0, void 0, function* () { | ||
@@ -34,6 +34,6 @@ const actionData = { root, args, context, info }; | ||
} | ||
const targetType = fieldResolverMetadata.getParentType(); | ||
const { validate: globalValidate, authChecker, pubSub, globalMiddlewares } = build_context_1.BuildContext; | ||
const targetType = fieldResolverMetadata.getObjectType(); | ||
const { validate: globalValidate, authChecker, authMode, pubSub, globalMiddlewares, } = build_context_1.BuildContext; | ||
const middlewares = globalMiddlewares.concat(fieldResolverMetadata.middlewares); | ||
helpers_1.applyAuthChecker(middlewares, authChecker, fieldResolverMetadata.roles); | ||
helpers_1.applyAuthChecker(middlewares, authMode, authChecker, fieldResolverMetadata.roles); | ||
return (root, args, context, info) => __awaiter(this, void 0, void 0, function* () { | ||
@@ -55,5 +55,5 @@ const actionData = { root, args, context, info }; | ||
function createSimpleFieldResolver(fieldMetadata) { | ||
const { authChecker, globalMiddlewares } = build_context_1.BuildContext; | ||
const { authChecker, authMode, globalMiddlewares } = build_context_1.BuildContext; | ||
const middlewares = globalMiddlewares.concat(fieldMetadata.middlewares); | ||
helpers_1.applyAuthChecker(middlewares, authChecker, fieldMetadata.roles); | ||
helpers_1.applyAuthChecker(middlewares, authMode, authChecker, fieldMetadata.roles); | ||
return (root, args, context, info) => __awaiter(this, void 0, void 0, function* () { | ||
@@ -60,0 +60,0 @@ const actionData = { root, args, context, info }; |
import { PubSubEngine } from "graphql-subscriptions"; | ||
import { ValidatorOptions } from "class-validator"; | ||
import { ParamMetadata } from "../metadata/definitions"; | ||
import { AuthChecker } from "../interfaces"; | ||
import { AuthChecker, AuthMode } from "../interfaces/auth-checker"; | ||
import { ActionData } from "../types"; | ||
import { Middleware } from "../interfaces/Middleware"; | ||
export declare function getParams(params: ParamMetadata[], {root, args, context, info}: ActionData<any>, globalValidate: boolean | ValidatorOptions, pubSub: PubSubEngine): Promise<any[]>; | ||
export declare function applyAuthChecker(middlewares: Array<Middleware<any>>, authChecker?: AuthChecker, roles?: string[]): void; | ||
export declare function applyAuthChecker(middlewares: Array<Middleware<any>>, authMode: AuthMode, authChecker?: AuthChecker, roles?: string[]): void; | ||
export declare function applyMiddlewares(actionData: ActionData<any>, middlewares: Array<Middleware<any>>, resolverHandlerFunction: () => any): Promise<any>; |
@@ -46,5 +46,5 @@ "use strict"; | ||
exports.getParams = getParams; | ||
function applyAuthChecker(middlewares, authChecker, roles) { | ||
function applyAuthChecker(middlewares, authMode, authChecker, roles) { | ||
if (authChecker && roles) { | ||
middlewares.unshift(auth_middleware_1.AuthMiddleware(authChecker, roles)); | ||
middlewares.unshift(auth_middleware_1.AuthMiddleware(authChecker, authMode, roles)); | ||
} | ||
@@ -51,0 +51,0 @@ } |
import { GraphQLScalarType } from "graphql"; | ||
import { ValidatorOptions } from "class-validator"; | ||
import { PubSubEngine, PubSubOptions } from "graphql-subscriptions"; | ||
import { AuthChecker } from "../interfaces/auth-checker"; | ||
import { AuthChecker, AuthMode } from "../interfaces/auth-checker"; | ||
import { Middleware } from "../interfaces/Middleware"; | ||
@@ -20,2 +20,3 @@ export declare type DateScalarMode = "isoDate" | "timestamp"; | ||
authChecker?: AuthChecker; | ||
authMode?: AuthMode; | ||
pubSub?: PubSubEngine | PubSubOptions; | ||
@@ -29,2 +30,3 @@ globalMiddlewares?: Array<Middleware<any>>; | ||
static authChecker?: AuthChecker<any>; | ||
static authMode: AuthMode; | ||
static pubSub: PubSubEngine; | ||
@@ -31,0 +33,0 @@ static globalMiddlewares: Array<Middleware<any>>; |
@@ -21,2 +21,5 @@ "use strict"; | ||
} | ||
if (options.authMode !== undefined) { | ||
this.authMode = options.authMode; | ||
} | ||
if (options.pubSub !== undefined) { | ||
@@ -42,2 +45,3 @@ if ("eventEmitter" in options.pubSub) { | ||
this.authChecker = undefined; | ||
this.authMode = "error"; | ||
this.pubSub = new graphql_subscriptions_1.PubSub(); | ||
@@ -44,0 +48,0 @@ this.globalMiddlewares = []; |
@@ -155,3 +155,3 @@ "use strict"; | ||
let fields = objectType.fields.reduce((fieldsMap, field) => { | ||
const fieldResolverMetadata = metadata_storage_1.MetadataStorage.fieldResolvers.find(resolver => resolver.getParentType() === objectType.target && | ||
const fieldResolverMetadata = metadata_storage_1.MetadataStorage.fieldResolvers.find(resolver => resolver.getObjectType() === objectType.target && | ||
resolver.methodName === field.name); | ||
@@ -158,0 +158,0 @@ fieldsMap[field.name] = { |
import { GraphQLResolveInfo } from "graphql"; | ||
export interface ArgsDictionary { | ||
[argName: string]: any; | ||
} | ||
export interface ActionData<ContextType = {}> { | ||
root: any; | ||
args: { | ||
[argName: string]: any; | ||
}; | ||
args: ArgsDictionary; | ||
context: ContextType; | ||
info: GraphQLResolveInfo; | ||
} |
import { GraphQLScalarType } from "graphql"; | ||
import { ValidatorOptions } from "class-validator"; | ||
import { FilterActionData } from "./index"; | ||
import { FilterActionData } from "./filter-action-data"; | ||
export declare type TypeValue = ClassType | GraphQLScalarType | Function | object | symbol; | ||
@@ -5,0 +5,0 @@ export declare type ReturnTypeFuncValue = TypeValue | [TypeValue]; |
import { GraphQLResolveInfo } from "graphql"; | ||
export interface ArgsDictionary { | ||
[argName: string]: any; | ||
} | ||
import { ArgsDictionary } from "./action-data"; | ||
export interface FilterActionData<TPayload = any, TArgs = ArgsDictionary, TContext = {}> { | ||
@@ -6,0 +4,0 @@ payload: TPayload; |
@@ -1,2 +0,2 @@ | ||
export * from "./action-data"; | ||
export * from "./filter-action-data"; | ||
export { ActionData } from "./action-data"; | ||
export { FilterActionData } from "./filter-action-data"; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115462
2401