Skip to content

Commit

Permalink
Backport "API Generator: Add list option to @CrudGenerator()" to …
Browse files Browse the repository at this point in the history
…v6 (#2154)

See #1971

---------

Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
  • Loading branch information
dkarnutsch and johnnyomair authored Jun 10, 2024
1 parent dcf3f70 commit 2a5e00b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/cyan-ladybugs-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@comet/cms-api": minor
---

API Generator: Add `list` option to `@CrudGenerator()` to allow disabling the list query

Related DTO classes will still be generated as they might be useful for application code.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface CrudGeneratorOptions {
create?: boolean;
update?: boolean;
delete?: boolean;
list?: boolean;
}

export function CrudGenerator({
Expand All @@ -12,10 +13,15 @@ export function CrudGenerator({
create = true,
update = true,
delete: deleteMutation = true,
list = true,
}: CrudGeneratorOptions): ClassDecorator {
// eslint-disable-next-line @typescript-eslint/ban-types
return function (target: Function) {
Reflect.defineMetadata(`data:crudGeneratorOptions`, { targetDirectory, requiredPermission, create, update, delete: deleteMutation }, target);
Reflect.defineMetadata(
`data:crudGeneratorOptions`,
{ targetDirectory, requiredPermission, create, update, delete: deleteMutation, list },
target,
);
};
}

Expand Down
16 changes: 11 additions & 5 deletions packages/api/cms-api/src/generator/generate-crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,11 +781,14 @@ function generateResolver({ generatorOptions, metadata }: { generatorOptions: Cr
: ""
}
${
generatorOptions.list
? `
@Query(() => Paginated${classNamePlural})
async ${instanceNameSingular != instanceNamePlural ? instanceNamePlural : `${instanceNamePlural}List`}(
@Args() { ${scopeProp ? `scope, ` : ""}${hasSearchArg ? `search, ` : ""}${hasFilterArg ? `filter, ` : ""}${
hasSortArg ? `sort, ` : ""
}offset, limit }: ${argsClassName}${hasOutputRelations ? `, @Info() info: GraphQLResolveInfo` : ""}
hasSortArg ? `sort, ` : ""
}offset, limit }: ${argsClassName}${hasOutputRelations ? `, @Info() info: GraphQLResolveInfo` : ""}
): Promise<Paginated${classNamePlural}> {
const where${
hasSearchArg || hasFilterArg
Expand All @@ -811,8 +814,8 @@ function generateResolver({ generatorOptions, metadata }: { generatorOptions: Cr
${hasOutputRelations ? `// eslint-disable-next-line @typescript-eslint/no-explicit-any` : ""}
const options: FindOptions<${metadata.className}${hasOutputRelations ? `, any` : ""}> = { offset, limit${
hasOutputRelations ? `, populate` : ""
}};
hasOutputRelations ? `, populate` : ""
}};
${
hasSortArg
Expand All @@ -828,7 +831,9 @@ function generateResolver({ generatorOptions, metadata }: { generatorOptions: Cr
const [entities, totalCount] = await this.repository.findAndCount(where, options);
return new Paginated${classNamePlural}(entities, totalCount);
}
`
: ""
}
${
Expand Down Expand Up @@ -938,6 +943,7 @@ export async function generateCrud(generatorOptionsParam: CrudGeneratorOptions,
create: generatorOptionsParam.create ?? true,
update: generatorOptionsParam.update ?? true,
delete: generatorOptionsParam.delete ?? true,
list: generatorOptionsParam.list ?? true,
};

const generatedFiles: GeneratedFile[] = [];
Expand Down

0 comments on commit 2a5e00b

Please sign in to comment.