Skip to content

Speedup sorting & building/extending schema #3939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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