Skip to content

Commit

Permalink
Speedup sorting & building/extending schema (#3939)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Jul 25, 2023
1 parent fae5da5 commit 3610786
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/utilities/extendSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AccumulatorMap } from '../jsutils/AccumulatorMap.js';
import { inspect } from '../jsutils/inspect.js';
import { invariant } from '../jsutils/invariant.js';
import { keyMap } from '../jsutils/keyMap.js';
import { mapValue } from '../jsutils/mapValue.js';
import type { Maybe } from '../jsutils/Maybe.js';

Expand Down Expand Up @@ -217,14 +216,13 @@ export function extendSchemaImpl(
return schemaConfig;
}

const typeMap = Object.create(null);
for (const existingType of schemaConfig.types) {
typeMap[existingType.name] = extendNamedType(existingType);
}
const typeMap = new Map<string, GraphQLNamedType>(
schemaConfig.types.map((type) => [type.name, extendNamedType(type)]),
);

for (const typeNode of typeDefs) {
const name = typeNode.name.value;
typeMap[name] = stdTypeMap[name] ?? buildType(typeNode);
typeMap.set(name, stdTypeMap.get(name) ?? buildType(typeNode));
}

const operationTypes = {
Expand All @@ -242,7 +240,7 @@ export function extendSchemaImpl(
return {
description: schemaDef?.description?.value ?? schemaConfig.description,
...operationTypes,
types: Object.values(typeMap),
types: Array.from(typeMap.values()),
directives: [
...schemaConfig.directives.map(replaceDirective),
...directiveDefs.map(buildDirective),
Expand Down Expand Up @@ -273,7 +271,7 @@ export function extendSchemaImpl(
// Note: While this could make early assertions to get the correctly
// typed values, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
return typeMap[type.name];
return typeMap.get(type.name) as T;
}

function replaceDirective(directive: GraphQLDirective): GraphQLDirective {
Expand Down Expand Up @@ -462,7 +460,7 @@ export function extendSchemaImpl(

function getNamedType(node: NamedTypeNode): GraphQLNamedType {
const name = node.name.value;
const type = stdTypeMap[name] ?? typeMap[name];
const type = stdTypeMap.get(name) ?? typeMap.get(name);

if (type === undefined) {
throw new Error(`Unknown type: "${name}".`);
Expand Down Expand Up @@ -704,9 +702,11 @@ export function extendSchemaImpl(
}
}

const stdTypeMap = keyMap(
[...specifiedScalarTypes, ...introspectionTypes],
(type) => type.name,
const stdTypeMap = new Map(
[...specifiedScalarTypes, ...introspectionTypes].map((type) => [
type.name,
type,
]),
);

/**
Expand Down
14 changes: 7 additions & 7 deletions src/utilities/lexicographicSortSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { inspect } from '../jsutils/inspect.js';
import { invariant } from '../jsutils/invariant.js';
import { keyValMap } from '../jsutils/keyValMap.js';
import type { Maybe } from '../jsutils/Maybe.js';
import { naturalCompare } from '../jsutils/naturalCompare.js';
import type { ObjMap } from '../jsutils/ObjMap.js';
Expand Down Expand Up @@ -40,15 +39,16 @@ import { GraphQLSchema } from '../type/schema.js';
*/
export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema {
const schemaConfig = schema.toConfig();
const typeMap = keyValMap(
sortByName(schemaConfig.types),
(type) => type.name,
sortNamedType,
const typeMap = new Map<string, GraphQLNamedType>(
sortByName(schemaConfig.types).map((type) => [
type.name,
sortNamedType(type),
]),
);

return new GraphQLSchema({
...schemaConfig,
types: Object.values(typeMap),
types: Array.from(typeMap.values()),
directives: sortByName(schemaConfig.directives).map(sortDirective),
query: replaceMaybeType(schemaConfig.query),
mutation: replaceMaybeType(schemaConfig.mutation),
Expand All @@ -68,7 +68,7 @@ export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema {
}

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

function replaceMaybeType<T extends GraphQLNamedType>(
Expand Down

0 comments on commit 3610786

Please sign in to comment.