lexicographicSortSchema.mjs 5.7 KB

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