download-introspection-schema.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. {
  18. method: 'post',
  19. host: 'localhost',
  20. port: API_PORT,
  21. path: '/' + API_PATH,
  22. headers: {
  23. 'Content-Type': 'application/json',
  24. 'Content-Length': Buffer.byteLength(body),
  25. },
  26. },
  27. response => {
  28. const outputFile = fs.createWriteStream(outputFilePath);
  29. response.pipe(outputFile);
  30. response.on('end', () => resolve(true));
  31. response.on('error', reject);
  32. },
  33. );
  34. request.write(body);
  35. request.end();
  36. request.on('error', (err: any) => {
  37. if (err.code === 'ECONNREFUSED') {
  38. console.error(
  39. `ERROR: Could not connect to the Vendure server at http://localhost:${API_PORT}/${API_PATH}`,
  40. );
  41. resolve(false);
  42. }
  43. reject(err);
  44. });
  45. });
  46. }