Skip to content

Commit

Permalink
simplify lexicographicSortSchema with mapSchemaConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Nov 26, 2024
1 parent 4cfcdaa commit 408f48e
Showing 1 changed file with 39 additions and 150 deletions.
189 changes: 39 additions & 150 deletions src/utilities/lexicographicSortSchema.ts
Original file line number Diff line number Diff line change
@@ -1,173 +1,62 @@
import { inspect } from '../jsutils/inspect.js';
import { invariant } from '../jsutils/invariant.js';
import type { Maybe } from '../jsutils/Maybe.js';
import { naturalCompare } from '../jsutils/naturalCompare.js';
import type { ObjMap } from '../jsutils/ObjMap.js';

import type {
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLInputFieldConfigMap,
GraphQLNamedType,
GraphQLType,
} from '../type/definition.js';
import {
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLInterfaceType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLUnionType,
isEnumType,
isInputObjectType,
isInterfaceType,
isListType,
isNonNullType,
isObjectType,
isScalarType,
isUnionType,
} from '../type/definition.js';
import { GraphQLDirective } from '../type/directives.js';
import { isIntrospectionType } from '../type/introspection.js';
import { GraphQLSchema } from '../type/schema.js';

import { mapSchemaConfig, SchemaElementKind } from './mapSchemaConfig.js';

/**
* Sort GraphQLSchema.
*
* This function returns a sorted copy of the given GraphQLSchema.
*/
export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema {
const schemaConfig = schema.toConfig();
const typeMap = new Map<string, GraphQLNamedType>(
sortByName(schemaConfig.types).map((type) => [
type.name,
sortNamedType(type),
]),
);

return new GraphQLSchema({
...schemaConfig,
types: Array.from(typeMap.values()),
directives: sortByName(schemaConfig.directives).map(sortDirective),
query: replaceMaybeType(schemaConfig.query),
mutation: replaceMaybeType(schemaConfig.mutation),
subscription: replaceMaybeType(schemaConfig.subscription),
});

function replaceType<T extends GraphQLType>(type: T): T {
if (isListType(type)) {
// @ts-expect-error
return new GraphQLList(replaceType(type.ofType));
} else if (isNonNullType(type)) {
// @ts-expect-error
return new GraphQLNonNull(replaceType(type.ofType));
}
// @ts-expect-error FIXME: TS Conversion
return replaceNamedType<GraphQLNamedType>(type);
}

function replaceNamedType<T extends GraphQLNamedType>(type: T): T {
return typeMap.get(type.name) as T;
}

function replaceMaybeType<T extends GraphQLNamedType>(
maybeType: Maybe<T>,
): Maybe<T> {
return maybeType && replaceNamedType(maybeType);
}

function sortDirective(directive: GraphQLDirective) {
const config = directive.toConfig();
return new GraphQLDirective({
...config,
locations: sortBy(config.locations, (x) => x),
args: sortArgs(config.args),
});
}

function sortArgs(args: GraphQLFieldConfigArgumentMap) {
return sortObjMap(args, (arg) => ({
...arg,
type: replaceType(arg.type),
}));
}

function sortFields(fieldsMap: GraphQLFieldConfigMap<unknown, unknown>) {
return sortObjMap(fieldsMap, (field) => ({
...field,
type: replaceType(field.type),
args: field.args && sortArgs(field.args),
}));
}

function sortInputFields(fieldsMap: GraphQLInputFieldConfigMap) {
return sortObjMap(fieldsMap, (field) => ({
...field,
type: replaceType(field.type),
}));
}

function sortTypes<T extends GraphQLNamedType>(
array: ReadonlyArray<T>,
): Array<T> {
return sortByName(array).map(replaceNamedType);
}

function sortNamedType(type: GraphQLNamedType): GraphQLNamedType {
if (isScalarType(type) || isIntrospectionType(type)) {
return type;
}
if (isObjectType(type)) {
const config = type.toConfig();
return new GraphQLObjectType({
return new GraphQLSchema(
mapSchemaConfig(schema.toConfig(), () => ({
[SchemaElementKind.OBJECT]: (config) => ({
...config,
interfaces: () => sortTypes(config.interfaces),
fields: () => sortFields(config.fields),
});
}
if (isInterfaceType(type)) {
const config = type.toConfig();
return new GraphQLInterfaceType({
interfaces: () => sortByName(config.interfaces()),
fields: () => sortObjMap(config.fields()),
}),
[SchemaElementKind.FIELD]: (config) => ({
...config,
interfaces: () => sortTypes(config.interfaces),
fields: () => sortFields(config.fields),
});
}
if (isUnionType(type)) {
const config = type.toConfig();
return new GraphQLUnionType({
args: sortObjMap(config.args),
}),
[SchemaElementKind.INTERFACE]: (config) => ({
...config,
types: () => sortTypes(config.types),
});
}
if (isEnumType(type)) {
const config = type.toConfig();
return new GraphQLEnumType({
interfaces: () => sortByName(config.interfaces()),
fields: () => sortObjMap(config.fields()),
}),
[SchemaElementKind.UNION]: (config) => ({
...config,
values: sortObjMap(config.values, (value) => value),
});
}
if (isInputObjectType(type)) {
const config = type.toConfig();
return new GraphQLInputObjectType({
types: () => sortByName(config.types()),
}),
[SchemaElementKind.ENUM]: (config) => ({
...config,
fields: () => sortInputFields(config.fields),
});
}
/* c8 ignore next 3 */
// Not reachable, all possible types have been considered.
invariant(false, 'Unexpected type: ' + inspect(type));
}
values: () => sortObjMap(config.values()),
}),
[SchemaElementKind.INPUT_OBJECT]: (config) => ({
...config,
fields: () => sortObjMap(config.fields()),
}),
[SchemaElementKind.DIRECTIVE]: (config) => ({
...config,
locations: sortBy(config.locations, (x) => x),
args: sortObjMap(config.args),
}),
[SchemaElementKind.SCHEMA]: (config) => ({
...config,
types: sortByName(config.types),
directives: sortByName(config.directives),
}),
})),
);
}

function sortObjMap<T, R>(
map: ObjMap<T>,
sortValueFn: (value: T) => R,
): ObjMap<R> {
function sortObjMap<T, R>(map: ObjMap<T>): ObjMap<R> {
const sortedMap = Object.create(null);
for (const key of Object.keys(map).sort(naturalCompare)) {
sortedMap[key] = sortValueFn(map[key]);
sortedMap[key] = map[key];
}
return sortedMap;
}
Expand Down

0 comments on commit 408f48e

Please sign in to comment.