lexicographicSortSchema.mjs 6.2 KB

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