download-introspection-schema.ts 2.9 KB

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