Browse Source

chore: Improve type generation to not rely on dev server

This ensures that we don't accidentally generate
types with extra custom fields etc.
Michael Bromley 11 months ago
parent
commit
85442918c1
1 changed files with 48 additions and 7 deletions
  1. 48 7
      scripts/codegen/download-introspection-schema.ts

+ 48 - 7
scripts/codegen/download-introspection-schema.ts

@@ -1,10 +1,50 @@
+/* eslint-disable no-console */
+import { INestApplication } from '@nestjs/common';
+import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
+import { bootstrap, VendureConfig } from '@vendure/core';
 import fs from 'fs';
 import { getIntrospectionQuery } from 'graphql';
 import http from 'http';
 
-import { ADMIN_API_PATH, API_PORT } from '../../packages/common/src/shared-constants';
+export const config: VendureConfig = {
+    apiOptions: {
+        port: 3355,
+        adminApiPath: 'admin-api',
+        shopApiPath: 'shop-api',
+    },
+    authOptions: {
+        tokenMethod: ['bearer', 'cookie'],
+        superadminCredentials: {
+            identifier: 'superadmin',
+            password: 'superadmin',
+        },
+    },
+    dbConnectionOptions: {
+        type: 'sqljs',
+        synchronize: true,
+        logging: false,
+    },
+    paymentOptions: {
+        paymentMethodHandlers: [],
+    },
+    plugins: [AdminUiPlugin],
+};
 
-/* eslint-disable no-console */
+let appPromise: Promise<INestApplication>;
+
+/**
+ * Bootstraps the Vendure server with the AdminUiPlugin.
+ * Starting up a dedicated server instance ensures that we don't
+ * generate any types containing any custom plugin or
+ * custom field types.
+ */
+export async function bootstrapApp() {
+    if (appPromise) {
+        return appPromise;
+    }
+    appPromise = bootstrap(config);
+    return appPromise;
+}
 
 /**
  * Makes an introspection query to the Vendure server and writes the result to a
@@ -12,15 +52,16 @@ import { ADMIN_API_PATH, API_PORT } from '../../packages/common/src/shared-const
  *
  * If there is an error connecting to the server, the promise resolves to false.
  */
-export function downloadIntrospectionSchema(apiPath: string, outputFilePath: string): Promise<boolean> {
+export async function downloadIntrospectionSchema(apiPath: string, outputFilePath: string): Promise<boolean> {
     const body = JSON.stringify({ query: getIntrospectionQuery({ inputValueDeprecation: true }) });
+    const app = await bootstrapApp();
 
-    return new Promise((resolve, reject) => {
+    return new Promise<boolean>((resolve, reject) => {
         const request = http.request(
             {
                 method: 'post',
                 host: 'localhost',
-                port: API_PORT,
+                port: config.apiOptions.port,
                 path: '/' + apiPath,
                 headers: {
                     'Content-Type': 'application/json',
@@ -39,11 +80,11 @@ export function downloadIntrospectionSchema(apiPath: string, outputFilePath: str
         request.on('error', (err: any) => {
             if (err.code === 'ECONNREFUSED') {
                 console.error(
-                    `ERROR: Could not connect to the Vendure server at http://localhost:${API_PORT}/${apiPath}`,
+                    `ERROR: Could not connect to the Vendure server at http://localhost:${config.apiOptions.port}/${apiPath}`,
                 );
                 resolve(false);
             }
             reject(err);
         });
-    });
+    }).finally(() => app.close());
 }