lexicographicSortSchema.js 6.6 KB

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