type-graphql#ContainerType TypeScript Examples
The following examples show how to use
type-graphql#ContainerType.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: resolvers.ts From squid with GNU General Public License v3.0 | 6 votes |
class CustomResolversContainer implements ContainerType {
private transaction: TypeormTransaction
constructor(resolverData: ResolverData<any>) {
let transaction = resolverData.context.openReaderTransaction
assert(typeof transaction?.getEntityManager == 'function', 'expected typeorm transaction in the context')
this.transaction = transaction
}
get(resolverClass: CustomResolverClass): CustomResolverClass {
return new resolverClass(() => this.transaction.getEntityManager())
}
}
Example #2
Source File: prepare-options.service.ts From typegraphql-nestjs with MIT License | 5 votes |
prepareOptions<TOptions extends TypeGraphQLFeatureModuleOptions>(
featureModuleToken: string,
globalMiddlewares: Middleware<any>[] = [],
) {
const globalResolvers = getMetadataStorage().resolverClasses.map(
metadata => metadata.target,
);
const globalMiddlewareClasses = globalMiddlewares.filter(
it => it.prototype,
) as Function[];
const featureModuleOptionsArray: TOptions[] = [];
const resolversClasses: ClassType[] = [];
const providersMetadataMap = new Map<Function, InstanceWrapper<any>>();
for (const module of this.modulesContainer.values()) {
for (const provider of module.providers.values()) {
if (
typeof provider.name === "string" &&
provider.name.includes(featureModuleToken)
) {
featureModuleOptionsArray.push(provider.instance as TOptions);
}
if (globalResolvers.includes(provider.metatype)) {
providersMetadataMap.set(provider.metatype, provider);
resolversClasses.push(provider.metatype as ClassType);
}
if (globalMiddlewareClasses.includes(provider.metatype)) {
providersMetadataMap.set(provider.metatype, provider);
}
}
}
const orphanedTypes = flatten(
featureModuleOptionsArray.map(it => it.orphanedTypes),
);
const container: ContainerType = {
get: (cls, { context }) => {
let contextId = context[REQUEST_CONTEXT_ID];
if (!contextId) {
contextId = ContextIdFactory.create();
context[REQUEST_CONTEXT_ID] = contextId;
}
const providerMetadata = providersMetadataMap.get(cls)!;
if (
providerMetadata.isDependencyTreeStatic() &&
!providerMetadata.isTransient
) {
return this.moduleRef.get(cls, { strict: false });
}
return this.moduleRef.resolve(cls, contextId, { strict: false });
},
};
return {
resolversClasses,
orphanedTypes,
container,
featureModuleOptionsArray,
};
}