download-introspection-schema.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import fs from 'fs';
  2. import { getIntrospectionQuery } from 'graphql';
  3. import http from 'http';
  4. import { ADMIN_API_PATH, API_PORT } from '../../packages/common/src/shared-constants';
  5. /* eslint-disable no-console */
  6. /**
  7. * Makes an introspection query to the Vendure server and writes the result to a
  8. * schema.json file.
  9. *
  10. * If there is an error connecting to the server, the promise resolves to false.
  11. */
  12. export function downloadIntrospectionSchema(apiPath: string, outputFilePath: string): Promise<boolean> {
  13. const body = JSON.stringify({ query: getIntrospectionQuery({ inputValueDeprecation: true }) });
  14. return new Promise((resolve, reject) => {
  15. const request = http.request(
  16. {
  17. method: 'post',
  18. host: 'localhost',
  19. port: API_PORT,
  20. path: '/' + apiPath,
  21. headers: {
  22. 'Content-Type': 'application/json',
  23. 'Content-Length': Buffer.byteLength(body),
  24. },
  25. },
  26. response => {
  27. const outputFile = fs.createWriteStream(outputFilePath);
  28. response.pipe(outputFile);
  29. response.on('end', () => resolve(true));
  30. response.on('error', reject);
  31. },
  32. );
  33. request.write(body);
  34. request.end();
  35. request.on('error', (err: any) => {
  36. if (err.code === 'ECONNREFUSED') {
  37. console.error(
  38. `ERROR: Could not connect to the Vendure server at http://localhost:${API_PORT}/${apiPath}`,
  39. );
  40. resolve(false);
  41. }
  42. reject(err);
  43. });
  44. });
  45. }