UNPKG

@onn-software/ddl-to-gql

Version:

Convert a SQL DDL to a GraphQL implementation with all relations.

9 lines (8 loc) 4.38 kB
export declare const baseResolver = "\nimport * as model from './model';\nimport * as repo from './repos';\n\nexport class OnnResolverHooks {\n static buildWrapper : () => {\n before: <T, E = any>(resolverName:string, tableName :string, gqlParams: any) => Promise<T | undefined | null>;\n after: <T, E = any>(resolverName:string, tableName :string, result: T, gqlParams: any) => Promise<T>;\n } = () => ({\n before: async () => null,\n after: async (_,__,result) => result,\n });\n}\n\nexport const mapClauses: <T = any>(clauses?: any[]) => model.Clause<T>[] = clauses => {\n return (clauses ?? []).map(c => ({field: c.field, operator: c.operator, value:\n c.booleanValue ?? c.intValue ?? c.floatValue ?? c.stringValue ?? c.intValues ?? c.floatValues ?? c.stringValues}))\n}\n"; export declare const resolverTemplate = "\n\nexport const __GQL_TYPE___Resolver = {\n __FIELD_GETTER_BLOCK__\n __PAGINATED_FIELD_GETTER_BLOCK__\n};\n"; export declare const getResolverBlock = "\n __RELATION_NAME__: async (parent: any, args: any, context: model.OnnContext | any, info: any) => {\n let gqlParams = { parent, args, context, info };\n const wrapper = OnnResolverHooks.buildWrapper();\n let result = await wrapper.before<model.__FOREIGN_SQL_TYPE__>(\"__RELATION_NAME__\", \"__FOREIGN_TABLE_NAME__\", gqlParams);\n if (!result) { \n const clauses = [{field: \"__SAFE_FOREIGN_FIELD_NAME__\", value: gqlParams.parent[\"__SAFE_FIELD_NAME__\"], operator: model.QueryOperator.EQUALS}, ...mapClauses(gqlParams.args.where)];\n result = await new repo.__FOREIGN_SQL_TYPE___Repo().getBy(gqlParams.context, clauses, gqlParams.args.orderBy);\n }\n return (await wrapper.after(\"__RELATION_NAME__\", \"__FOREIGN_TABLE_NAME__\", result, gqlParams)) as any;\n },\n"; export declare const paginatedResolverBlock = "\n __RELATION_NAME__: async (parent: any, args: any, context: model.OnnContext | any, info: any) => {\n let gqlParams = { parent, args, context, info };\n const wrapper = OnnResolverHooks.buildWrapper();\n let result = await wrapper.before<model.Paginated<model.__FOREIGN_SQL_TYPE__>>(\"__RELATION_NAME__\", \"__FOREIGN_TABLE_NAME__\", gqlParams);\n if (!result) { \n const clauses = [{field: \"__SAFE_FOREIGN_FIELD_NAME__\", value: gqlParams.parent[\"__SAFE_FIELD_NAME__\"], operator: model.QueryOperator.EQUALS}, ...mapClauses(gqlParams.args.where)];\n result = await new repo.__FOREIGN_SQL_TYPE___Repo().getPaginatedBy(gqlParams.context, clauses, gqlParams.args.paginate, gqlParams.args.orderBy, gqlParams.args.distinct);\n }\n return (await wrapper.after(\"__RELATION_NAME__\", \"__FOREIGN_TABLE_NAME__\", result, gqlParams)) as any;\n },\n"; export declare const queryResolverEntry = "\n __TABLE_NAME__: async (parent: any, args: any, context: model.OnnContext | any, info: any) => {\n let gqlParams = { parent, args, context, info };\n const wrapper = OnnResolverHooks.buildWrapper();\n let result = await wrapper.before<model.Paginated<model.__SQL_TYPE__>>(\"__TABLE_NAME__\", \"__TABLE_NAME__\", gqlParams);\n if (!result) { \n result = await new repo.__SQL_TYPE___Repo().getPaginatedBy(gqlParams.context, mapClauses(gqlParams.args.where), gqlParams.args.paginate, gqlParams.args.orderBy, gqlParams.args.distinct);\n }\n return (await wrapper.after(\"__TABLE_NAME__\", \"__TABLE_NAME__\", result, gqlParams)) as any;\n },\n "; export declare const mutationResolverEntry = "\n __MUTATION_TYPE_____TABLE_NAME__: async (parent: any, args: any, context: model.OnnContext | any, info: any) => {\n let gqlParams = { parent, args, context, info };\n const wrapper = OnnResolverHooks.buildWrapper();\n let result = await wrapper.before<model.MutationResult>(\"__MUTATION_TYPE_____TABLE_NAME__\", \"__TABLE_NAME__\", gqlParams);\n if (!result) {\n result = await (new repo.__SQL_TYPE___Repo() as any)[__MUTATION_METHOD__](gqlParams.context, mapClauses(gqlParams.args.where), gqlParams.args.value);\n }\n return (await wrapper.after(\"__MUTATION_TYPE_____TABLE_NAME__\", \"__TABLE_NAME__\", result, gqlParams)) as any;\n },\n "; export declare const queryResolvers = "\nexport const allGqlQueryResolvers = {\n__RESOLVER_ENTRIES__\n};\n"; export declare const mutationResolvers = "\nexport const allGqlMutationResolvers = {\n__MUTATION_ENTRIES__\n};\n";