@tsed/cli-plugin-typegraphql 6.6.3 → 7.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
2
  import { inject, Module, OnAdd, ProjectPackageJson } from "@tsed/cli-core";
3
- import { Inject } from "@tsed/di";
4
3
  import { TypeGraphqlInitHook } from "./hooks/TypeGraphqlInitHook.js";
5
4
  let TypeGraphqlModule = class TypeGraphqlModule {
6
5
  constructor() {
@@ -1,39 +1,65 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { RootRendererService } from "@tsed/cli";
3
- import { inject, OnExec, ProjectPackageJson } from "@tsed/cli-core";
4
- import { Injectable } from "@tsed/di";
5
- import { TEMPLATE_DIR } from "../utils/templateDir.js";
6
- let TypeGraphqlInitHook = class TypeGraphqlInitHook {
7
- constructor() {
8
- this.packageJson = inject(ProjectPackageJson);
9
- this.rootRenderer = inject(RootRendererService);
10
- }
11
- onExec(ctx) {
1
+ import { exec, ProjectClient } from "@tsed/cli";
2
+ import { createSubTasks } from "@tsed/cli-core";
3
+ import { injectable } from "@tsed/di";
4
+ import { SyntaxKind } from "ts-morph";
5
+ export class TypeGraphqlInitHook {
6
+ async $alterInitSubTasks(tasks, data) {
12
7
  return [
8
+ ...tasks,
13
9
  {
14
- title: "Generate files",
15
- task: () => this.rootRenderer.renderAll([
16
- "/src/datasources/index.ts",
17
- "/src/datasources/MyDataSource.ts",
18
- "/src/resolvers/recipes/Recipe.ts",
19
- "/src/resolvers/recipes/RecipeNotFoundError.ts",
20
- "/src/resolvers/recipes/RecipeResolver.ts",
21
- "/src/resolvers/index.ts",
22
- "/src/services/RecipeService.ts"
23
- ], ctx, {
24
- templateDir: `${TEMPLATE_DIR}/init`
25
- })
10
+ title: "Generate initial resolver",
11
+ enabled: () => !!data.graphql,
12
+ task: createSubTasks(await exec("generate", {
13
+ ...data,
14
+ type: "typegraphql.resolver",
15
+ name: "Recipe"
16
+ }), { ...data, concurrent: false })
26
17
  }
27
18
  ];
28
19
  }
29
- };
30
- __decorate([
31
- OnExec("init"),
32
- __metadata("design:type", Function),
33
- __metadata("design:paramtypes", [Object]),
34
- __metadata("design:returntype", void 0)
35
- ], TypeGraphqlInitHook.prototype, "onExec", null);
36
- TypeGraphqlInitHook = __decorate([
37
- Injectable()
38
- ], TypeGraphqlInitHook);
39
- export { TypeGraphqlInitHook };
20
+ $alterProjectFiles(project, data) {
21
+ if (!data.graphql) {
22
+ return project;
23
+ }
24
+ if (data.commandName !== "init") {
25
+ return project;
26
+ }
27
+ if (project.serverSourceFile) {
28
+ project.serverSourceFile.addImportDeclaration({
29
+ moduleSpecifier: "@tsed/typegraphql"
30
+ });
31
+ project.serverSourceFile.addImportDeclaration({
32
+ moduleSpecifier: "./graphql/datasources/index.js"
33
+ });
34
+ project.serverSourceFile.addImportDeclaration({
35
+ moduleSpecifier: "./graphql/resolvers/index.js"
36
+ });
37
+ }
38
+ const options = project.findConfiguration("config");
39
+ if (!options) {
40
+ return project;
41
+ }
42
+ const graphql = project.getPropertyAssignment(options, {
43
+ name: "graphql",
44
+ kind: SyntaxKind.ObjectLiteralExpression,
45
+ initializer: "{}"
46
+ });
47
+ const defaultGraphql = project.getPropertyAssignment(graphql, {
48
+ name: "default",
49
+ kind: SyntaxKind.ObjectLiteralExpression,
50
+ initializer: "{}"
51
+ });
52
+ project.getPropertyAssignment(defaultGraphql, {
53
+ name: "path",
54
+ kind: SyntaxKind.StringLiteral,
55
+ initializer: '"/graphql"'
56
+ });
57
+ project.getPropertyAssignment(defaultGraphql, {
58
+ name: "buildSchemaOptions",
59
+ kind: SyntaxKind.ObjectLiteralExpression,
60
+ initializer: "{}"
61
+ });
62
+ return project;
63
+ }
64
+ }
65
+ injectable(TypeGraphqlInitHook);
package/lib/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
+ import "./templates/index.js";
1
2
  import { TypeGraphqlModule } from "./TypeGraphqlModule.js";
2
3
  export * from "./hooks/TypeGraphqlInitHook.js";
3
- export * from "./utils/templateDir.js";
4
4
  export default TypeGraphqlModule;
@@ -0,0 +1,27 @@
1
+ import { defineTemplate } from "@tsed/cli";
2
+ export default defineTemplate({
3
+ id: "typegraphql.datasource",
4
+ label: "TypeGraphQL DataSource",
5
+ fileName: "{{symbolName}}.datasource",
6
+ outputDir: "{{srcDir}}/graphql/datasources",
7
+ render(symbolName) {
8
+ return `import {DataSourceService} from "@tsed/typegraphql";
9
+ import {RESTDataSource} from "apollo-datasource-rest";
10
+
11
+ @DataSourceService()
12
+ export class ${symbolName} extends RESTDataSource {
13
+ constructor() {
14
+ super();
15
+ this.baseURL = "http://localhost:8001";
16
+ }
17
+
18
+ willSendRequest(request: any) {
19
+ request.headers.set("Authorization", this.context.token);
20
+ }
21
+
22
+ getMyData(id: string) {
23
+ return this.get(\`/rest/calendars/\${id}\`);
24
+ }
25
+ }`;
26
+ }
27
+ });
@@ -0,0 +1,4 @@
1
+ import "./datasource.template.js";
2
+ import "./resolver.template.js";
3
+ import "./model.template.js";
4
+ import "./service.template.js";
@@ -0,0 +1,21 @@
1
+ import { defineTemplate } from "@tsed/cli";
2
+ export default defineTemplate({
3
+ id: "typegraphql.model",
4
+ label: "TypeGraphQL Model",
5
+ fileName: "{{symbolName}}.model",
6
+ outputDir: "{{srcDir}}/graphql/models",
7
+ render(symbolName) {
8
+ return `import {Field, ID, ObjectType} from "type-graphql";
9
+
10
+ @ObjectType({description: "Object representing cooking ${symbolName}"})
11
+ export class ${symbolName} {
12
+ @Field((type) => ID)
13
+ id: string;
14
+
15
+ constructor(options: Partial<${symbolName}> = {}) {
16
+ options.id && (this.id = options.id);
17
+ }
18
+ }
19
+ `;
20
+ }
21
+ });
@@ -0,0 +1,47 @@
1
+ import { defineTemplate, render } from "@tsed/cli";
2
+ // @ts-ignore
3
+ import { plural } from "pluralize";
4
+ import { camelCase, pascalCase } from "change-case";
5
+ import { relative } from "path";
6
+ import { dirname } from "node:path";
7
+ export default defineTemplate({
8
+ id: "typegraphql.resolver",
9
+ label: "TypeGraphQL Resolver",
10
+ fileName: "{{symbolName}}.resolver",
11
+ outputDir: "{{srcDir}}/graphql/resolvers",
12
+ async render(symbolName, data) {
13
+ const modelInfo = await render("typegraphql.model", {
14
+ name: data.name
15
+ });
16
+ const serviceInfo = await render("typegraphql.service", {
17
+ name: data.name
18
+ });
19
+ const name = pascalCase(data.name);
20
+ const modelName = modelInfo?.symbolName;
21
+ const serviceName = serviceInfo?.symbolName;
22
+ const listName = camelCase(plural(name));
23
+ const getName = camelCase(name);
24
+ const relativeModelPath = relative(dirname(data.symbolPath), String(modelInfo?.symbolPath));
25
+ const relativeServicePath = relative(dirname(data.symbolPath), String(serviceInfo?.symbolPath));
26
+ return `import {ResolverService} from "@tsed/typegraphql";
27
+ import {Arg, Query} from "type-graphql";
28
+ import {${modelName}} from "${relativeModelPath}.js";
29
+ import {${serviceName}} from "${relativeServicePath}.js";
30
+
31
+ @ResolverService(${modelName})
32
+ export class ${symbolName} {
33
+ protected service = inject(${serviceName});
34
+
35
+ @Query((returns) => ${modelName})
36
+ async ${getName}(@Arg("id") id: string) {
37
+ return this.service.getById(id);
38
+ }
39
+
40
+ @Query((returns) => [${modelName}], {description: "Get all items"})
41
+ ${listName}(): Promise<${modelName}[]> {
42
+ return this.service.findAll({});
43
+ }
44
+ }
45
+ `;
46
+ }
47
+ });
@@ -0,0 +1,23 @@
1
+ import { defineTemplate } from "@tsed/cli";
2
+ export default defineTemplate({
3
+ id: "typegraphql.service",
4
+ label: "TypeGraphQL Service",
5
+ fileName: "{{symbolName}}.service",
6
+ outputDir: "{{srcDir}}/services",
7
+ hidden: true,
8
+ render(symbolName) {
9
+ return `import {Injectable} from "@tsed/di";
10
+
11
+ @Injectable()
12
+ export class ${symbolName} {
13
+ getById(id: string) {
14
+ return null;
15
+ }
16
+
17
+ findAll(query: any) {
18
+ return [];
19
+ }
20
+ }
21
+ `;
22
+ }
23
+ });