download-introspection-schema.ts 2.8 KB

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