download-introspection-schema.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* eslint-disable no-console */
  2. import { INestApplication } from '@nestjs/common';
  3. import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
  4. import { bootstrap, DefaultLogger, LogLevel, VendureConfig } from '@vendure/core';
  5. import { DashboardPlugin } from '@vendure/dashboard-plugin';
  6. import fs from 'fs';
  7. import { getIntrospectionQuery } from 'graphql';
  8. import http from 'http';
  9. export const config: VendureConfig = {
  10. apiOptions: {
  11. port: 3355,
  12. adminApiPath: 'admin-api',
  13. shopApiPath: 'shop-api',
  14. },
  15. authOptions: {
  16. tokenMethod: ['bearer', 'cookie'],
  17. superadminCredentials: {
  18. identifier: 'superadmin',
  19. password: 'superadmin',
  20. },
  21. },
  22. dbConnectionOptions: {
  23. type: 'sqljs',
  24. synchronize: true,
  25. logging: false,
  26. },
  27. paymentOptions: {
  28. paymentMethodHandlers: [],
  29. },
  30. plugins: [AdminUiPlugin, DashboardPlugin],
  31. logger: new DefaultLogger({ level: LogLevel.Verbose }),
  32. };
  33. let appPromise: Promise<INestApplication>;
  34. /**
  35. * Bootstraps the Vendure server with the AdminUiPlugin.
  36. * Starting up a dedicated server instance ensures that we don't
  37. * generate any types containing any custom plugin or
  38. * custom field types.
  39. */
  40. export async function bootstrapApp() {
  41. if (appPromise) {
  42. return appPromise;
  43. }
  44. appPromise = bootstrap(config);
  45. return appPromise;
  46. }
  47. /**
  48. * Makes an introspection query to the Vendure server and writes the result to a
  49. * schema.json file.
  50. *
  51. * If there is an error connecting to the server, the promise resolves to false.
  52. */
  53. export async function downloadIntrospectionSchema(apiPath: string, outputFilePath: string): Promise<boolean> {
  54. const body = JSON.stringify({ query: getIntrospectionQuery({ inputValueDeprecation: true }) });
  55. const app = await bootstrapApp();
  56. return new Promise<boolean>((resolve, reject) => {
  57. const request = http.request(
  58. {
  59. method: 'post',
  60. host: 'localhost',
  61. port: config.apiOptions.port,
  62. path: '/' + apiPath,
  63. headers: {
  64. 'Content-Type': 'application/json',
  65. 'Content-Length': Buffer.byteLength(body),
  66. },
  67. },
  68. response => {
  69. const outputFile = fs.createWriteStream(outputFilePath);
  70. response.pipe(outputFile);
  71. response.on('end', () => resolve(true));
  72. response.on('error', reject);
  73. },
  74. );
  75. request.write(body);
  76. request.end();
  77. request.on('error', (err: any) => {
  78. if (err.code === 'ECONNREFUSED') {
  79. console.error(
  80. `ERROR: Could not connect to the Vendure server at http://localhost:${config.apiOptions.port}/${apiPath}`,
  81. );
  82. resolve(false);
  83. }
  84. reject(err);
  85. });
  86. }).finally(() => app.close());
  87. }