lexicographicSortSchema.mjs 6.3 KB

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