diff options
author | Minteck <contact@minteck.org> | 2022-10-18 08:59:09 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-10-18 08:59:09 +0200 |
commit | 2c4ae43e688a9873e86211ea0e7aeb9ba770dd77 (patch) | |
tree | 17848d95522dab25d3cdeb9c4a6450e2a234861f /alarm/node_modules/graphql/utilities/lexicographicSortSchema.mjs | |
parent | 108525534c28013cfe1897c30e4565f9893f3766 (diff) | |
download | pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.gz pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.bz2 pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.zip |
Update
Diffstat (limited to 'alarm/node_modules/graphql/utilities/lexicographicSortSchema.mjs')
-rw-r--r-- | alarm/node_modules/graphql/utilities/lexicographicSortSchema.mjs | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/alarm/node_modules/graphql/utilities/lexicographicSortSchema.mjs b/alarm/node_modules/graphql/utilities/lexicographicSortSchema.mjs new file mode 100644 index 0000000..cc9643f --- /dev/null +++ b/alarm/node_modules/graphql/utilities/lexicographicSortSchema.mjs @@ -0,0 +1,185 @@ +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; } + +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; } + +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; } + +import objectValues from "../polyfills/objectValues.mjs"; +import inspect from "../jsutils/inspect.mjs"; +import invariant from "../jsutils/invariant.mjs"; +import keyValMap from "../jsutils/keyValMap.mjs"; +import naturalCompare from "../jsutils/naturalCompare.mjs"; +import { GraphQLSchema } from "../type/schema.mjs"; +import { GraphQLDirective } from "../type/directives.mjs"; +import { isIntrospectionType } from "../type/introspection.mjs"; +import { GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, isListType, isNonNullType, isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType } from "../type/definition.mjs"; +/** + * Sort GraphQLSchema. + * + * This function returns a sorted copy of the given GraphQLSchema. + */ + +export function lexicographicSortSchema(schema) { + var schemaConfig = schema.toConfig(); + var typeMap = keyValMap(sortByName(schemaConfig.types), function (type) { + return type.name; + }, sortNamedType); + return new GraphQLSchema(_objectSpread(_objectSpread({}, schemaConfig), {}, { + types: objectValues(typeMap), + directives: sortByName(schemaConfig.directives).map(sortDirective), + query: replaceMaybeType(schemaConfig.query), + mutation: replaceMaybeType(schemaConfig.mutation), + subscription: replaceMaybeType(schemaConfig.subscription) + })); + + function replaceType(type) { + if (isListType(type)) { + // $FlowFixMe[incompatible-return] + return new GraphQLList(replaceType(type.ofType)); + } else if (isNonNullType(type)) { + // $FlowFixMe[incompatible-return] + return new GraphQLNonNull(replaceType(type.ofType)); + } + + return replaceNamedType(type); + } + + function replaceNamedType(type) { + return typeMap[type.name]; + } + + function replaceMaybeType(maybeType) { + return maybeType && replaceNamedType(maybeType); + } + + function sortDirective(directive) { + var config = directive.toConfig(); + return new GraphQLDirective(_objectSpread(_objectSpread({}, config), {}, { + locations: sortBy(config.locations, function (x) { + return x; + }), + args: sortArgs(config.args) + })); + } + + function sortArgs(args) { + return sortObjMap(args, function (arg) { + return _objectSpread(_objectSpread({}, arg), {}, { + type: replaceType(arg.type) + }); + }); + } + + function sortFields(fieldsMap) { + return sortObjMap(fieldsMap, function (field) { + return _objectSpread(_objectSpread({}, field), {}, { + type: replaceType(field.type), + args: sortArgs(field.args) + }); + }); + } + + function sortInputFields(fieldsMap) { + return sortObjMap(fieldsMap, function (field) { + return _objectSpread(_objectSpread({}, field), {}, { + type: replaceType(field.type) + }); + }); + } + + function sortTypes(arr) { + return sortByName(arr).map(replaceNamedType); + } + + function sortNamedType(type) { + if (isScalarType(type) || isIntrospectionType(type)) { + return type; + } + + if (isObjectType(type)) { + var config = type.toConfig(); + return new GraphQLObjectType(_objectSpread(_objectSpread({}, config), {}, { + interfaces: function interfaces() { + return sortTypes(config.interfaces); + }, + fields: function fields() { + return sortFields(config.fields); + } + })); + } + + if (isInterfaceType(type)) { + var _config = type.toConfig(); + + return new GraphQLInterfaceType(_objectSpread(_objectSpread({}, _config), {}, { + interfaces: function interfaces() { + return sortTypes(_config.interfaces); + }, + fields: function fields() { + return sortFields(_config.fields); + } + })); + } + + if (isUnionType(type)) { + var _config2 = type.toConfig(); + + return new GraphQLUnionType(_objectSpread(_objectSpread({}, _config2), {}, { + types: function types() { + return sortTypes(_config2.types); + } + })); + } + + if (isEnumType(type)) { + var _config3 = type.toConfig(); + + return new GraphQLEnumType(_objectSpread(_objectSpread({}, _config3), {}, { + values: sortObjMap(_config3.values) + })); + } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') + + + if (isInputObjectType(type)) { + var _config4 = type.toConfig(); + + return new GraphQLInputObjectType(_objectSpread(_objectSpread({}, _config4), {}, { + fields: function fields() { + return sortInputFields(_config4.fields); + } + })); + } // istanbul ignore next (Not reachable. All possible types have been considered) + + + false || invariant(0, 'Unexpected type: ' + inspect(type)); + } +} + +function sortObjMap(map, sortValueFn) { + var sortedMap = Object.create(null); + var sortedKeys = sortBy(Object.keys(map), function (x) { + return x; + }); + + for (var _i2 = 0; _i2 < sortedKeys.length; _i2++) { + var key = sortedKeys[_i2]; + var value = map[key]; + sortedMap[key] = sortValueFn ? sortValueFn(value) : value; + } + + return sortedMap; +} + +function sortByName(array) { + return sortBy(array, function (obj) { + return obj.name; + }); +} + +function sortBy(array, mapToKey) { + return array.slice().sort(function (obj1, obj2) { + var key1 = mapToKey(obj1); + var key2 = mapToKey(obj2); + return naturalCompare(key1, key2); + }); +} |