Browse Source

refactor(cli): Extract getProps to EntityRef

Michael Bromley 1 year ago
parent
commit
409d1015bc

+ 6 - 25
packages/cli/src/commands/add/api-extension/add-api-extension.ts

@@ -258,13 +258,10 @@ function createCrudApiExtension(project: Project, plugin: VendurePluginRef, serv
                             writer.writeLine(`    id: ID!`);
                             writer.writeLine(`    id: ID!`);
                             writer.writeLine(`    createdAt: DateTime!`);
                             writer.writeLine(`    createdAt: DateTime!`);
                             writer.writeLine(`    updatedAt: DateTime!`);
                             writer.writeLine(`    updatedAt: DateTime!`);
-                            for (const prop of entityRef.classDeclaration.getProperties()) {
-                                const { type, nullable } = getEntityPropType(prop.getType());
+                            for (const { name, type, nullable } of entityRef.getProps()) {
                                 const graphQlType = getGraphQLType(type);
                                 const graphQlType = getGraphQLType(type);
                                 if (graphQlType) {
                                 if (graphQlType) {
-                                    writer.writeLine(
-                                        `  ${prop.getName()}: ${graphQlType}${nullable ? '' : '!'}`,
-                                    );
+                                    writer.writeLine(`  ${name}: ${graphQlType}${nullable ? '' : '!'}`);
                                 }
                                 }
                             }
                             }
                             writer.writeLine(`  }`);
                             writer.writeLine(`  }`);
@@ -297,13 +294,10 @@ function createCrudApiExtension(project: Project, plugin: VendurePluginRef, serv
 
 
                             if (serviceRef.features.create) {
                             if (serviceRef.features.create) {
                                 writer.writeLine(`  input Create${entityRef.name}Input {`);
                                 writer.writeLine(`  input Create${entityRef.name}Input {`);
-                                for (const prop of entityRef.classDeclaration.getProperties()) {
-                                    const { type, nullable } = getEntityPropType(prop.getType());
+                                for (const { name, type, nullable } of entityRef.getProps()) {
                                     const graphQlType = getGraphQLType(type);
                                     const graphQlType = getGraphQLType(type);
                                     if (graphQlType) {
                                     if (graphQlType) {
-                                        writer.writeLine(
-                                            `    ${prop.getName()}: ${graphQlType}${nullable ? '' : '!'}`,
-                                        );
+                                        writer.writeLine(`    ${name}: ${graphQlType}${nullable ? '' : '!'}`);
                                     }
                                     }
                                 }
                                 }
                                 writer.writeLine(`  }`);
                                 writer.writeLine(`  }`);
@@ -313,11 +307,10 @@ function createCrudApiExtension(project: Project, plugin: VendurePluginRef, serv
                             if (serviceRef.features.update) {
                             if (serviceRef.features.update) {
                                 writer.writeLine(`  input Update${entityRef.name}Input {`);
                                 writer.writeLine(`  input Update${entityRef.name}Input {`);
                                 writer.writeLine(`    id: ID!`);
                                 writer.writeLine(`    id: ID!`);
-                                for (const prop of entityRef.classDeclaration.getProperties()) {
-                                    const { type } = getEntityPropType(prop.getType());
+                                for (const { name, type } of entityRef.getProps()) {
                                     const graphQlType = getGraphQLType(type);
                                     const graphQlType = getGraphQLType(type);
                                     if (graphQlType) {
                                     if (graphQlType) {
-                                        writer.writeLine(`    ${prop.getName()}: ${graphQlType}`);
+                                        writer.writeLine(`    ${name}: ${graphQlType}`);
                                     }
                                     }
                                 }
                                 }
                                 writer.writeLine(`  }`);
                                 writer.writeLine(`  }`);
@@ -366,18 +359,6 @@ function createCrudApiExtension(project: Project, plugin: VendurePluginRef, serv
     return adminApiExtensions;
     return adminApiExtensions;
 }
 }
 
 
-function getEntityPropType(propType: Type): { type: Type; nullable: boolean } {
-    if (propType.isUnion()) {
-        // get the non-null part of the union
-        const nonNullType = propType.getUnionTypes().find(t => !t.isNull() && !t.isUndefined());
-        if (!nonNullType) {
-            throw new Error('Could not find non-null type in union');
-        }
-        return { type: nonNullType, nullable: true };
-    }
-    return { type: propType, nullable: false };
-}
-
 function getGraphQLType(type: Type): string | undefined {
 function getGraphQLType(type: Type): string | undefined {
     if (type.isString()) {
     if (type.isString()) {
         return 'String';
         return 'String';

+ 17 - 1
packages/cli/src/shared/entity-ref.ts

@@ -1,4 +1,4 @@
-import { ClassDeclaration, Node, SyntaxKind } from 'ts-morph';
+import { ClassDeclaration, Node, SyntaxKind, Type } from 'ts-morph';
 
 
 export class EntityRef {
 export class EntityRef {
     constructor(public classDeclaration: ClassDeclaration) {}
     constructor(public classDeclaration: ClassDeclaration) {}
@@ -23,6 +23,22 @@ export class EntityRef {
         return this.classDeclaration.getImplements().some(i => i.getText() === 'HasCustomFields');
         return this.classDeclaration.getImplements().some(i => i.getText() === 'HasCustomFields');
     }
     }
 
 
+    getProps(): Array<{ name: string; type: Type; nullable: boolean }> {
+        return this.classDeclaration.getProperties().map(prop => {
+            const propType = prop.getType();
+            const name = prop.getName();
+            if (propType.isUnion()) {
+                // get the non-null part of the union
+                const nonNullType = propType.getUnionTypes().find(t => !t.isNull() && !t.isUndefined());
+                if (!nonNullType) {
+                    throw new Error('Could not find non-null type in union');
+                }
+                return { name, type: nonNullType, nullable: true };
+            }
+            return { name, type: propType, nullable: false };
+        });
+    }
+
     getTranslationClass(): ClassDeclaration | undefined {
     getTranslationClass(): ClassDeclaration | undefined {
         if (!this.isTranslatable()) {
         if (!this.isTranslatable()) {
             return;
             return;