download-introspection-schema.ts 1.5 KB

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