lexicographicSortSchema.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(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. function lexicographicSortSchema(schema) {
  22. var schemaConfig = schema.toConfig();
  23. var typeMap = (0, _keyValMap.default)(sortByName(schemaConfig.types), function (type) {
  24. return type.name;
  25. }, sortNamedType);
  26. return new _schema.GraphQLSchema(_objectSpread({}, schemaConfig, {
  27. types: (0, _objectValues.default)(typeMap),
  28. directives: sortByName(schemaConfig.directives).map(sortDirective),
  29. query: replaceMaybeType(schemaConfig.query),
  30. mutation: replaceMaybeType(schemaConfig.mutation),
  31. subscription: replaceMaybeType(schemaConfig.subscription)
  32. }));
  33. function replaceType(type) {
  34. if ((0, _definition.isListType)(type)) {
  35. return new _definition.GraphQLList(replaceType(type.ofType));
  36. } else if ((0, _definition.isNonNullType)(type)) {
  37. return new _definition.GraphQLNonNull(replaceType(type.ofType));
  38. }
  39. return replaceNamedType(type);
  40. }
  41. function replaceNamedType(type) {
  42. return typeMap[type.name];
  43. }
  44. function replaceMaybeType(maybeType) {
  45. return maybeType && replaceNamedType(maybeType);
  46. }
  47. function sortDirective(directive) {
  48. var config = directive.toConfig();
  49. return new _directives.GraphQLDirective(_objectSpread({}, config, {
  50. locations: sortBy(config.locations, function (x) {
  51. return x;
  52. }),
  53. args: sortArgs(config.args)
  54. }));
  55. }
  56. function sortArgs(args) {
  57. return sortObjMap(args, function (arg) {
  58. return _objectSpread({}, arg, {
  59. type: replaceType(arg.type)
  60. });
  61. });
  62. }
  63. function sortFields(fieldsMap) {
  64. return sortObjMap(fieldsMap, function (field) {
  65. return _objectSpread({}, field, {
  66. type: replaceType(field.type),
  67. args: sortArgs(field.args)
  68. });
  69. });
  70. }
  71. function sortInputFields(fieldsMap) {
  72. return sortObjMap(fieldsMap, function (field) {
  73. return _objectSpread({}, field, {
  74. type: replaceType(field.type)
  75. });
  76. });
  77. }
  78. function sortTypes(arr) {
  79. return sortByName(arr).map(replaceNamedType);
  80. }
  81. function sortNamedType(type) {
  82. if ((0, _definition.isScalarType)(type) || (0, _introspection.isIntrospectionType)(type)) {
  83. return type;
  84. } else if ((0, _definition.isObjectType)(type)) {
  85. var config = type.toConfig();
  86. return new _definition.GraphQLObjectType(_objectSpread({}, config, {
  87. interfaces: function interfaces() {
  88. return sortTypes(config.interfaces);
  89. },
  90. fields: function fields() {
  91. return sortFields(config.fields);
  92. }
  93. }));
  94. } else if ((0, _definition.isInterfaceType)(type)) {
  95. var _config = type.toConfig();
  96. return new _definition.GraphQLInterfaceType(_objectSpread({}, _config, {
  97. fields: function fields() {
  98. return sortFields(_config.fields);
  99. }
  100. }));
  101. } else if ((0, _definition.isUnionType)(type)) {
  102. var _config2 = type.toConfig();
  103. return new _definition.GraphQLUnionType(_objectSpread({}, _config2, {
  104. types: function types() {
  105. return sortTypes(_config2.types);
  106. }
  107. }));
  108. } else if ((0, _definition.isEnumType)(type)) {
  109. var _config3 = type.toConfig();
  110. return new _definition.GraphQLEnumType(_objectSpread({}, _config3, {
  111. values: sortObjMap(_config3.values)
  112. }));
  113. } else if ((0, _definition.isInputObjectType)(type)) {
  114. var _config4 = type.toConfig();
  115. return new _definition.GraphQLInputObjectType(_objectSpread({}, _config4, {
  116. fields: function fields() {
  117. return sortInputFields(_config4.fields);
  118. }
  119. }));
  120. } // Not reachable. All possible types have been considered.
  121. /* istanbul ignore next */
  122. (0, _invariant.default)(false, 'Unexpected type: ' + (0, _inspect.default)(type));
  123. }
  124. }
  125. function sortObjMap(map, sortValueFn) {
  126. var sortedMap = Object.create(null);
  127. var sortedKeys = sortBy(Object.keys(map), function (x) {
  128. return x;
  129. });
  130. for (var _i2 = 0; _i2 < sortedKeys.length; _i2++) {
  131. var key = sortedKeys[_i2];
  132. var value = map[key];
  133. sortedMap[key] = sortValueFn ? sortValueFn(value) : value;
  134. }
  135. return sortedMap;
  136. }
  137. function sortByName(array) {
  138. return sortBy(array, function (obj) {
  139. return obj.name;
  140. });
  141. }
  142. function sortBy(array, mapToKey) {
  143. return array.slice().sort(function (obj1, obj2) {
  144. var key1 = mapToKey(obj1);
  145. var key2 = mapToKey(obj2);
  146. return key1.localeCompare(key2);
  147. });
  148. }