download-introspection-schema.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* eslint-disable no-console */
  2. import { INestApplication } from '@nestjs/common';
  3. import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
  4. import { bootstrap, VendureConfig } from '@vendure/core';
  5. import { DashboardPlugin, DbIndexingStrategy, DbSearchStrategy } 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: [
  31. AdminUiPlugin,
  32. DashboardPlugin.init({
  33. searchStrategy: new DbSearchStrategy(),
  34. indexingStrategy: new DbIndexingStrategy(),
  35. }),
  36. ],
  37. };
  38. let appPromise: Promise<INestApplication>;
  39. /**
  40. * Bootstraps the Vendure server with the AdminUiPlugin.
  41. * Starting up a dedicated server instance ensures that we don't
  42. * generate any types containing any custom plugin or
  43. * custom field types.
  44. */
  45. export async function bootstrapApp() {
  46. if (appPromise) {
  47. return appPromise;
  48. }
  49. appPromise = bootstrap(config);
  50. return appPromise;
  51. }
  52. /**
  53. * Makes an introspection query to the Vendure server and writes the result to a
  54. * schema.json file.
  55. *
  56. * If there is an error connecting to the server, the promise resolves to false.
  57. */
  58. export async function downloadIntrospectionSchema(apiPath: string, outputFilePath: string): Promise<boolean> {
  59. const body = JSON.stringify({ query: getIntrospectionQuery({ inputValueDeprecation: true }) });
  60. const app = await bootstrapApp();
  61. return new Promise<boolean>((resolve, reject) => {
  62. const request = http.request(
  63. {
  64. method: 'post',
  65. host: 'localhost',
  66. port: config.apiOptions.port,
  67. path: '/' + apiPath,
  68. headers: {
  69. 'Content-Type': 'application/json',
  70. 'Content-Length': Buffer.byteLength(body),
  71. },
  72. },
  73. response => {
  74. const outputFile = fs.createWriteStream(outputFilePath);
  75. response.pipe(outputFile);
  76. response.on('end', () => resolve(true));
  77. response.on('error', reject);
  78. },
  79. );
  80. request.write(body);
  81. request.end();
  82. request.on('error', (err: any) => {
  83. if (err.code === 'ECONNREFUSED') {
  84. console.error(
  85. `ERROR: Could not connect to the Vendure server at http://localhost:${config.apiOptions.port}/${apiPath}`,
  86. );
  87. resolve(false);
  88. }
  89. reject(err);
  90. });
  91. }).finally(() => app.close());
  92. }