1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.lexicographicSortSchema = lexicographicSortSchema;
|
7 |
|
8 | var _objectValues = _interopRequireDefault(require("../polyfills/objectValues.js"));
|
9 |
|
10 | var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
|
11 |
|
12 | var _invariant = _interopRequireDefault(require("../jsutils/invariant.js"));
|
13 |
|
14 | var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap.js"));
|
15 |
|
16 | var _naturalCompare = _interopRequireDefault(require("../jsutils/naturalCompare.js"));
|
17 |
|
18 | var _schema = require("../type/schema.js");
|
19 |
|
20 | var _directives = require("../type/directives.js");
|
21 |
|
22 | var _introspection = require("../type/introspection.js");
|
23 |
|
24 | var _definition = require("../type/definition.js");
|
25 |
|
26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
27 |
|
28 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
29 |
|
30 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
31 |
|
32 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function lexicographicSortSchema(schema) {
|
40 | var schemaConfig = schema.toConfig();
|
41 | var typeMap = (0, _keyValMap.default)(sortByName(schemaConfig.types), function (type) {
|
42 | return type.name;
|
43 | }, sortNamedType);
|
44 | return new _schema.GraphQLSchema(_objectSpread(_objectSpread({}, schemaConfig), {}, {
|
45 | types: (0, _objectValues.default)(typeMap),
|
46 | directives: sortByName(schemaConfig.directives).map(sortDirective),
|
47 | query: replaceMaybeType(schemaConfig.query),
|
48 | mutation: replaceMaybeType(schemaConfig.mutation),
|
49 | subscription: replaceMaybeType(schemaConfig.subscription)
|
50 | }));
|
51 |
|
52 | function replaceType(type) {
|
53 | if ((0, _definition.isListType)(type)) {
|
54 |
|
55 | return new _definition.GraphQLList(replaceType(type.ofType));
|
56 | } else if ((0, _definition.isNonNullType)(type)) {
|
57 |
|
58 | return new _definition.GraphQLNonNull(replaceType(type.ofType));
|
59 | }
|
60 |
|
61 | return replaceNamedType(type);
|
62 | }
|
63 |
|
64 | function replaceNamedType(type) {
|
65 | return typeMap[type.name];
|
66 | }
|
67 |
|
68 | function replaceMaybeType(maybeType) {
|
69 | return maybeType && replaceNamedType(maybeType);
|
70 | }
|
71 |
|
72 | function sortDirective(directive) {
|
73 | var config = directive.toConfig();
|
74 | return new _directives.GraphQLDirective(_objectSpread(_objectSpread({}, config), {}, {
|
75 | locations: sortBy(config.locations, function (x) {
|
76 | return x;
|
77 | }),
|
78 | args: sortArgs(config.args)
|
79 | }));
|
80 | }
|
81 |
|
82 | function sortArgs(args) {
|
83 | return sortObjMap(args, function (arg) {
|
84 | return _objectSpread(_objectSpread({}, arg), {}, {
|
85 | type: replaceType(arg.type)
|
86 | });
|
87 | });
|
88 | }
|
89 |
|
90 | function sortFields(fieldsMap) {
|
91 | return sortObjMap(fieldsMap, function (field) {
|
92 | return _objectSpread(_objectSpread({}, field), {}, {
|
93 | type: replaceType(field.type),
|
94 | args: sortArgs(field.args)
|
95 | });
|
96 | });
|
97 | }
|
98 |
|
99 | function sortInputFields(fieldsMap) {
|
100 | return sortObjMap(fieldsMap, function (field) {
|
101 | return _objectSpread(_objectSpread({}, field), {}, {
|
102 | type: replaceType(field.type)
|
103 | });
|
104 | });
|
105 | }
|
106 |
|
107 | function sortTypes(arr) {
|
108 | return sortByName(arr).map(replaceNamedType);
|
109 | }
|
110 |
|
111 | function sortNamedType(type) {
|
112 | if ((0, _definition.isScalarType)(type) || (0, _introspection.isIntrospectionType)(type)) {
|
113 | return type;
|
114 | }
|
115 |
|
116 | if ((0, _definition.isObjectType)(type)) {
|
117 | var config = type.toConfig();
|
118 | return new _definition.GraphQLObjectType(_objectSpread(_objectSpread({}, config), {}, {
|
119 | interfaces: function interfaces() {
|
120 | return sortTypes(config.interfaces);
|
121 | },
|
122 | fields: function fields() {
|
123 | return sortFields(config.fields);
|
124 | }
|
125 | }));
|
126 | }
|
127 |
|
128 | if ((0, _definition.isInterfaceType)(type)) {
|
129 | var _config = type.toConfig();
|
130 |
|
131 | return new _definition.GraphQLInterfaceType(_objectSpread(_objectSpread({}, _config), {}, {
|
132 | interfaces: function interfaces() {
|
133 | return sortTypes(_config.interfaces);
|
134 | },
|
135 | fields: function fields() {
|
136 | return sortFields(_config.fields);
|
137 | }
|
138 | }));
|
139 | }
|
140 |
|
141 | if ((0, _definition.isUnionType)(type)) {
|
142 | var _config2 = type.toConfig();
|
143 |
|
144 | return new _definition.GraphQLUnionType(_objectSpread(_objectSpread({}, _config2), {}, {
|
145 | types: function types() {
|
146 | return sortTypes(_config2.types);
|
147 | }
|
148 | }));
|
149 | }
|
150 |
|
151 | if ((0, _definition.isEnumType)(type)) {
|
152 | var _config3 = type.toConfig();
|
153 |
|
154 | return new _definition.GraphQLEnumType(_objectSpread(_objectSpread({}, _config3), {}, {
|
155 | values: sortObjMap(_config3.values)
|
156 | }));
|
157 | }
|
158 |
|
159 |
|
160 | if ((0, _definition.isInputObjectType)(type)) {
|
161 | var _config4 = type.toConfig();
|
162 |
|
163 | return new _definition.GraphQLInputObjectType(_objectSpread(_objectSpread({}, _config4), {}, {
|
164 | fields: function fields() {
|
165 | return sortInputFields(_config4.fields);
|
166 | }
|
167 | }));
|
168 | }
|
169 |
|
170 |
|
171 | false || (0, _invariant.default)(0, 'Unexpected type: ' + (0, _inspect.default)(type));
|
172 | }
|
173 | }
|
174 |
|
175 | function sortObjMap(map, sortValueFn) {
|
176 | var sortedMap = Object.create(null);
|
177 | var sortedKeys = sortBy(Object.keys(map), function (x) {
|
178 | return x;
|
179 | });
|
180 |
|
181 | for (var _i2 = 0; _i2 < sortedKeys.length; _i2++) {
|
182 | var key = sortedKeys[_i2];
|
183 | var value = map[key];
|
184 | sortedMap[key] = sortValueFn ? sortValueFn(value) : value;
|
185 | }
|
186 |
|
187 | return sortedMap;
|
188 | }
|
189 |
|
190 | function sortByName(array) {
|
191 | return sortBy(array, function (obj) {
|
192 | return obj.name;
|
193 | });
|
194 | }
|
195 |
|
196 | function sortBy(array, mapToKey) {
|
197 | return array.slice().sort(function (obj1, obj2) {
|
198 | var key1 = mapToKey(obj1);
|
199 | var key2 = mapToKey(obj2);
|
200 | return (0, _naturalCompare.default)(key1, key2);
|
201 | });
|
202 | }
|