UNPKG

1.87 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 * @format
9 */
10'use strict';
11
12function writeForSchema(schema, licenseHeader, codegenDir, moduleName) {
13 var typeMap = schema.getTypeMap();
14 var stableTypeNames = Object.keys(typeMap).sort();
15 var types = [];
16 var _iteratorNormalCompletion = true;
17 var _didIteratorError = false;
18 var _iteratorError = undefined;
19
20 try {
21 for (var _iterator = stableTypeNames[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
22 var name = _step.value;
23 var type = typeMap[name];
24
25 if (type instanceof require("graphql").GraphQLEnumType) {
26 var values = type.getValues().map(function (_ref) {
27 var value = _ref.value;
28 return value;
29 });
30 values.sort();
31 types.push("export type ".concat(name, " =\n | '") + values.join("'\n | '") + "'\n | '%future added value';");
32 }
33 }
34 } catch (err) {
35 _didIteratorError = true;
36 _iteratorError = err;
37 } finally {
38 try {
39 if (!_iteratorNormalCompletion && _iterator["return"] != null) {
40 _iterator["return"]();
41 }
42 } finally {
43 if (_didIteratorError) {
44 throw _iteratorError;
45 }
46 }
47 }
48
49 var content = '/**\n' + licenseHeader.map(function (line) {
50 return " * ".concat(line, "\n");
51 }).join('') + ' *\n' + " * ".concat(require("signedsource").getSigningToken(), "\n") + ' * @flow\n' + ' */\n' + '\n' + // use Flow comment to avoid long Babel compile times
52 '/*::\n' + types.join('\n\n') + '\n*/\n';
53 codegenDir.writeFile(moduleName + '.js', require("signedsource").signFile(content));
54}
55
56module.exports = {
57 writeForSchema: writeForSchema
58};
\No newline at end of file