lexicographicSortSchema.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.lexicographicSortSchema = lexicographicSortSchema;
  6. var _objectValues = _interopRequireDefault(require("../polyfills/objectValues.js"));
  7. var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
  8. var _invariant = _interopRequireDefault(require("../jsutils/invariant.js"));
  9. var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap.js"));
  10. var _naturalCompare = _interopRequireDefault(require("../jsutils/naturalCompare.js"));
  11. var _schema = require("../type/schema.js");
  12. var _directives = require("../type/directives.js");
  13. var _introspection = require("../type/introspection.js");
  14. var _definition = require("../type/definition.js");
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. 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; }
  17. 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; }
  18. 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; }
  19. /**
  20. * Sort GraphQLSchema.
  21. *
  22. * This function returns a sorted copy of the given GraphQLSchema.
  23. */
  24. function lexicographicSortSchema(schema) {
  25. var schemaConfig = schema.toConfig();
  26. var typeMap = (0, _keyValMap.default)(sortByName(schemaConfig.types), function (type) {
  27. return type.name;
  28. }, sortNamedType);
  29. return new _schema.GraphQLSchema(_objectSpread(_objectSpread({}, schemaConfig), {}, {
  30. types: (0, _objectValues.default)(typeMap),
  31. directives: sortByName(schemaConfig.directives).map(sortDirective),
  32. query: replaceMaybeType(schemaConfig.query),
  33. mutation: replaceMaybeType(schemaConfig.mutation),
  34. subscription: replaceMaybeType(schemaConfig.subscription)
  35. }));
  36. function replaceType(type) {
  37. if ((0, _definition.isListType)(type)) {
  38. // $FlowFixMe[incompatible-return]
  39. return new _definition.GraphQLList(replaceType(type.ofType));
  40. } else if ((0, _definition.isNonNullType)(type)) {
  41. // $FlowFixMe[incompatible-return]
  42. return new _definition.GraphQLNonNull(replaceType(type.ofType));
  43. }
  44. return replaceNamedType(type);
  45. }
  46. function replaceNamedType(type) {
  47. return typeMap[type.name];
  48. }
  49. function replaceMaybeType(maybeType) {
  50. return maybeType && replaceNamedType(maybeType);
  51. }
  52. function sortDirective(directive) {
  53. var config = directive.toConfig();
  54. return new _directives.GraphQLDirective(_objectSpread(_objectSpread({}, config), {}, {
  55. locations: sortBy(config.locations, function (x) {
  56. return x;
  57. }),
  58. args: sortArgs(config.args)
  59. }));
  60. }
  61. function sortArgs(args) {
  62. return sortObjMap(args, function (arg) {
  63. return _objectSpread(_objectSpread({}, arg), {}, {
  64. type: replaceType(arg.type)
  65. });
  66. });
  67. }
  68. function sortFields(fieldsMap) {
  69. return sortObjMap(fieldsMap, function (field) {
  70. return _objectSpread(_objectSpread({}, field), {}, {
  71. type: replaceType(field.type),
  72. args: sortArgs(field.args)
  73. });
  74. });
  75. }
  76. function sortInputFields(fieldsMap) {
  77. return sortObjMap(fieldsMap, function (field) {
  78. return _objectSpread(_objectSpread({}, field), {}, {
  79. type: replaceType(field.type)
  80. });
  81. });
  82. }
  83. function sortTypes(arr) {
  84. return sortByName(arr).map(replaceNamedType);
  85. }
  86. function sortNamedType(type) {
  87. if ((0, _definition.isScalarType)(type) || (0, _introspection.isIntrospectionType)(type)) {
  88. return type;
  89. }
  90. if ((0, _definition.isObjectType)(type)) {
  91. var config = type.toConfig();
  92. return new _definition.GraphQLObjectType(_objectSpread(_objectSpread({}, config), {}, {
  93. interfaces: function interfaces() {
  94. return sortTypes(config.interfaces);
  95. },
  96. fields: function fields() {
  97. return sortFields(config.fields);
  98. }
  99. }));
  100. }
  101. if ((0, _definition.isInterfaceType)(type)) {
  102. var _config = type.toConfig();
  103. return new _definition.GraphQLInterfaceType(_objectSpread(_objectSpread({}, _config), {}, {
  104. interfaces: function interfaces() {
  105. return sortTypes(_config.interfaces);
  106. },
  107. fields: function fields() {
  108. return sortFields(_config.fields);
  109. }
  110. }));
  111. }
  112. if ((0, _definition.isUnionType)(type)) {
  113. var _config2 = type.toConfig();
  114. return new _definition.GraphQLUnionType(_objectSpread(_objectSpread({}, _config2), {}, {
  115. types: function types() {
  116. return sortTypes(_config2.types);
  117. }
  118. }));
  119. }
  120. if ((0, _definition.isEnumType)(type)) {
  121. var _config3 = type.toConfig();
  122. return new _definition.GraphQLEnumType(_objectSpread(_objectSpread({}, _config3), {}, {
  123. values: sortObjMap(_config3.values)
  124. }));
  125. } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
  126. if ((0, _definition.isInputObjectType)(type)) {
  127. var _config4 = type.toConfig();
  128. return new _definition.GraphQLInputObjectType(_objectSpread(_objectSpread({}, _config4), {}, {
  129. fields: function fields() {
  130. return sortInputFields(_config4.fields);
  131. }
  132. }));
  133. } // istanbul ignore next (Not reachable. All possible types have been considered)
  134. false || (0, _invariant.default)(0, 'Unexpected type: ' + (0, _inspect.default)(type));
  135. }
  136. }
  137. function sortObjMap(map, sortValueFn) {
  138. var sortedMap = Object.create(null);
  139. var sortedKeys = sortBy(Object.keys(map), function (x) {
  140. return x;
  141. });
  142. for (var _i2 = 0; _i2 < sortedKeys.length; _i2++) {
  143. var key = sortedKeys[_i2];
  144. var value = map[key];
  145. sortedMap[key] = sortValueFn ? sortValueFn(value) : value;
  146. }
  147. return sortedMap;
  148. }
  149. function sortByName(array) {
  150. return sortBy(array, function (obj) {
  151. return obj.name;
  152. });
  153. }
  154. function sortBy(array, mapToKey) {
  155. return array.slice().sort(function (obj1, obj2) {
  156. var key1 = mapToKey(obj1);
  157. var key2 = mapToKey(obj2);
  158. return (0, _naturalCompare.default)(key1, key2);
  159. });
  160. }