UNPKG

2.64 kBJavaScriptView Raw
1#!/usr/bin/env node
2// TODO: DRY monorepo-utils/bin/monorepo-babel-node-runner.js and relay-compiler
3// This is here to make this `bin` available directly from our monorepo without transpiling it.
4"use strict";
5
6var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
7
8var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
10var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
12require('@babel/register')({
13 ignore: [/node_modules\/(?!@kiwicom)/],
14 rootMode: 'upward'
15});
16
17var fs = require('fs');
18
19var path = require('path');
20
21var program = require('commander');
22
23var fetch = require('@kiwicom/fetch').default;
24
25var logger = require('@kiwicom/logger').default;
26
27var SignedSource = require('@kiwicom/signed-source').default;
28
29var _require = require('graphql'),
30 lexicographicSortSchema = _require.lexicographicSortSchema;
31
32var _require2 = require('graphql/utilities'),
33 buildClientSchema = _require2.buildClientSchema,
34 getIntrospectionQuery = _require2.getIntrospectionQuery,
35 printSchema = _require2.printSchema;
36
37program.option('--resource <url>', undefined, 'https://graphql.kiwi.com/').option('--filename <path>', undefined, 'schema.graphql').parse(process.argv);
38var filename = path.join(process.cwd(), program.filename);
39(0, _asyncToGenerator2.default)(
40/*#__PURE__*/
41_regenerator.default.mark(function _callee() {
42 var response, schemaJSON, clientSchema, schema;
43 return _regenerator.default.wrap(function _callee$(_context) {
44 while (1) {
45 switch (_context.prev = _context.next) {
46 case 0:
47 _context.next = 2;
48 return fetch(program.resource, {
49 method: 'POST',
50 headers: {
51 Accept: 'application/json',
52 'Content-Type': 'application/json',
53 'X-Client': '@kiwicom/relay schema fetcher'
54 },
55 body: JSON.stringify({
56 query: getIntrospectionQuery()
57 })
58 });
59
60 case 2:
61 response = _context.sent;
62 _context.next = 5;
63 return response.json();
64
65 case 5:
66 schemaJSON = _context.sent;
67 clientSchema = printSchema(lexicographicSortSchema(buildClientSchema(schemaJSON.data)));
68 schema = SignedSource.signFile("# ".concat(SignedSource.getSigningToken(), "\n\n").concat(clientSchema, "\n"));
69 fs.writeFileSync(filename, schema);
70 logger.log('GraphQL schema saved to: %s', filename);
71
72 case 10:
73 case "end":
74 return _context.stop();
75 }
76 }
77 }, _callee);
78}))();
\No newline at end of file