kapix-typegraphql-prisma
Version:

120 lines (117 loc) • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateHelpersFile = void 0;
const imports_1 = require("./imports");
function generateHelpersFile(sourceFile, options) {
var _a;
(0, imports_1.generateGraphQLInfoImport)(sourceFile);
(0, imports_1.generateGraphQLFieldsImport)(sourceFile);
sourceFile.addImportDeclaration({
moduleSpecifier: (_a = options.lodashImportName) !== null && _a !== void 0 ? _a : `lodash-es`,
namedImports: ["isArray"],
});
sourceFile.addStatements(/* ts */ `
export function transformInfoIntoPrismaArgs(info: GraphQLResolveInfo): Record<string, any> {
const fields: Record<string, any> = graphqlFields(
// suppress GraphQLResolveInfo types issue
info as any,
{},
{
excludedFields: ['__typename'],
processArguments: true,
}
);
return transformFields(fields);
}
`);
sourceFile.addStatements(/* ts */ `
function transformFields(fields: Record<string, any>): Record<string, any> {
return Object.fromEntries(
Object.entries(fields)
.map<[string, any]>(([key, value]) => {
if (Object.keys(value).length === 0) {
return [key, true];
}
if ("__arguments" in value) {
return [key, Object.fromEntries(
value.__arguments.map((argument: object) => {
const [[key, { value }]] = Object.entries(argument);
return [key, value];
})
)];
}
return [key, transformFields(value)];
}),
);
}
`);
sourceFile.addStatements(/* ts */ `
export function getPrismaFromContext(context: any) {
const prismaClient = context["${options.contextPrismaKey}"];
if (!prismaClient) {
throw new Error("Unable to find Prisma Client in GraphQL context. Please provide it under the \`context[\\"${options.contextPrismaKey}\\"]\` key.");
}
return prismaClient;
}
`);
sourceFile.addStatements(/* ts */ `
export function transformCountFieldIntoSelectRelationsCount(_count: object) {
return {
include: {
_count: {
select: {
...Object.fromEntries(
Object.entries(_count).filter(([_, v]) => v != null)
),
}
},
},
}
}
`);
sourceFile.addStatements(/* ts */ `
export const allQueryNames = ["groupBy", "aggregate", "findUniqueOrThrow", "findUnique", "findMany", "findFirst", "findFirstOrThrow"] as const;
export const allMutationNames = ["create","createMany","update","updateMany","delete","deleteMany","upsert"] as const;
export type MutationInterceptorOperation = (typeof allMutationNames)[number]
export type QueryInterceptorOperation = (typeof allQueryNames)[number]
export type MutationInterceptor = {
onBefore?: (ctx: any, args: any, crudResolver: string, isQuery: boolean, operation: MutationInterceptorOperation | QueryInterceptorOperation) => void | Promise<void>
onAfter?: (ctx: any, args: any, crudResolver: string, isQuery: boolean, operation: MutationInterceptorOperation | QueryInterceptorOperation, result: any) => void | Promise<void>
}
export const _resolversInterceptors = {}
export async function onIntercept(
modelName: string,
crudResolver: string,
method: 'onBefore' | 'onAfter',
operation: MutationInterceptorOperation | QueryInterceptorOperation,
ctx: any,
args: any,
result: any = null
) : Promise<any> {
const interceptor = _resolversInterceptors[modelName]
if (interceptor) {
const isQuery = allQueryNames.includes(operation as any)
if (interceptor._all) {
const globalInterceptorFunction = interceptor._all[method]
if (globalInterceptorFunction) {
if (isArray(args)) {
await Promise.all(args.map(i => globalInterceptorFunction(ctx, i, crudResolver, isQuery, operation)))
} else {
await globalInterceptorFunction(ctx, args, isQuery, operation)
}
}
}
const interceptorFunction = interceptor[crudResolver] && interceptor[crudResolver][method]
if (interceptorFunction) {
if (isArray(args)) {
await Promise.all(args.map(i => interceptorFunction(ctx, i, crudResolver, isQuery, operation)))
} else {
return await interceptorFunction(ctx, args, crudResolver, isQuery, operation, result)
}
}
}
}
`);
}
exports.generateHelpersFile = generateHelpersFile;
//# sourceMappingURL=generate-helpers.js.map