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