type-graphql#ClassType TypeScript Examples
The following examples show how to use
type-graphql#ClassType.
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: typegraphql-options.factory.ts From typegraphql-nestjs with MIT License | 6 votes |
async createGqlOptions(): Promise<GqlModuleOptions> {
const { globalMiddlewares } = this.rootModuleOptions;
const { resolversClasses, container, orphanedTypes } =
this.optionsPreparatorService.prepareOptions<TypeGraphQLFeatureModuleOptions>(
TYPEGRAPHQL_FEATURE_MODULE_OPTIONS,
globalMiddlewares,
);
const schema = await buildSchema({
...this.rootModuleOptions,
resolvers: resolversClasses as NonEmptyArray<ClassType>,
orphanedTypes,
container,
});
return {
...this.rootModuleOptions,
schema,
};
}
Example #2
Source File: getTypes.ts From liferay-grow with MIT License | 6 votes |
function getTypes(
suffix: string,
entity: any,
inputTypes: {
create: ClassType;
update: ClassType;
filter: ClassType;
},
): GetTypes {
@ObjectType(`PaginateObject${suffix}`)
class PaginateObjectType {
@Field(() => Pagination)
pagination: Pagination;
@Field(() => [entity])
rows: typeof InputType[];
}
@InputType(`getAllInput${suffix}`)
class getAllInput extends RetrievePaginationAndFilter {
@Field(() => inputTypes.filter, { nullable: true })
find?: typeof inputTypes.filter;
}
return {
PaginateObjectType,
getAllInput,
};
}
Example #3
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,
};
}
Example #4
Source File: typegraphql-options-federation.factory.ts From typegraphql-nestjs with MIT License | 5 votes |
async createGqlOptions(): Promise<GqlModuleOptions> {
const { globalMiddlewares } = this.rootModuleOptions;
const {
resolversClasses,
container,
orphanedTypes,
featureModuleOptionsArray,
} = this.optionsPreparatorService.prepareOptions<TypeGraphQLFeatureFedarationModuleOptions>(
TYPEGRAPHQL_FEATURE_FEDERATION_MODULE_OPTIONS,
globalMiddlewares,
);
const referenceResolversArray = [...featureModuleOptionsArray].filter(
it => it.referenceResolvers,
);
const referenceResolvers =
referenceResolversArray.length > 0
? Object.fromEntries(
referenceResolversArray.flatMap(it =>
Object.entries(it.referenceResolvers!),
),
)
: undefined;
const baseSchema = await buildSchema({
...this.rootModuleOptions,
directives: [...specifiedDirectives, ...federationDirectives],
resolvers: resolversClasses as NonEmptyArray<ClassType>,
orphanedTypes,
container,
});
const schema = buildFederatedSchema({
typeDefs: gql(printSchema(baseSchema)),
resolvers: createResolversMap(baseSchema) as GraphQLResolverMap<any>,
});
if (referenceResolvers) {
addResolversToSchema(schema, referenceResolvers);
}
return {
...this.rootModuleOptions,
schema,
};
}
Example #5
Source File: createBaseResolver.ts From liferay-grow with MIT License | 5 votes |
/**
* @param suffix Suffix is used on queryNames, example suffix: getAllUser
* @param entity TypeORM Entity
* @param inputTypes object with create, update and optionally update inputTypes
* @param middlewares optional middlewares to be applied in useMiddlewares
*/
export function createBaseResolver<Entity>(
suffix: string,
entity: any,
inputTypes: {
create: ClassType;
update: ClassType;
filter: ClassType;
},
relations: string[] = [],
) {
const { getAllInput, PaginateObjectType } = getTypes(
suffix,
entity,
inputTypes,
);
@Resolver({ isAbstract: true })
abstract class BaseResolver {
@Authorized()
@Query(() => PaginateObjectType, { name: `getAll${suffix}` })
async getAll(
@Arg('data', () => getAllInput, { defaultValue: {}, nullable: true })
data: any,
): Promise<any> {
return getAllPagination(entity, data, relations);
}
@Authorized()
@Query(() => entity, { name: `get${suffix}` })
async getOne(@Arg('id', () => String) id: string): Promise<Entity | Error> {
const content = await entity.findOne({ relations, where: { id } });
if (!content) {
throw new Error(`${suffix} not found`);
}
return content;
}
@Authorized()
@Mutation(() => entity, { name: `create${suffix}` })
async create(
@Arg('data', () => inputTypes.create) data: any,
): Promise<Entity | Error> {
const { id } = await entity.create(data).save();
return this.getOne(id);
}
@Authorized()
@Mutation(() => entity, { name: `updateBy${suffix}ID` })
async updateByID(
@Arg('data', () => inputTypes.update) data: any,
@Arg('id') id: string,
): Promise<Entity | Error> {
await entity.update(id, data);
return this.getOne(id);
}
@Authorized()
@Mutation(() => Boolean, { name: `deleteBy${suffix}ID` })
async deleteByID(@Arg('id', () => String) id: string): Promise<boolean> {
const _entity = await this.getOne(id);
if (!_entity) {
throw new Error(`No data found on Entity: ${suffix}, ID: ${id}`);
}
const data = await entity.remove(_entity);
return !!data;
}
}
return BaseResolver;
}