Skip to content

Commit be7cb3a

Browse files
authored
Performance work: resolvers plugins, documents loading (#7452)
* perf(resolvers-visitor): improve performance for config without `mappers` * perf(codegen): improve caching for output specific documents * Create witty-carrots-clap.md * update changeset * perf(resolvers-visitor): use cache for `shouldMapType()` result
1 parent c8ef37a commit be7cb3a

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

‎.changeset/witty-carrots-clap.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-codegen/cli": patch
3+
"@graphql-codegen/visitor-plugin-common": patch
4+
---
5+
6+
Performance work: resolvers plugins, documents loading

‎packages/graphql-codegen-cli/src/codegen.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,17 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
257257
task: wrapTask(async () => {
258258
debugLog(`[CLI] Loading Documents`);
259259

260-
const allDocuments = [...rootDocuments, ...outputSpecificDocuments];
260+
// get different cache for shared docs and output specific docs
261+
const results = await Promise.all(
262+
[rootDocuments, outputSpecificDocuments].map(docs => {
263+
const hash = JSON.stringify(docs);
264+
return documentsLoadingCache.load(hash);
265+
})
266+
);
267+
268+
const documents: Types.DocumentFile[] = [];
261269

262-
const hash = JSON.stringify(allDocuments);
263-
const result = await documentsLoadingCache.load(hash);
264-
const documents: Types.DocumentFile[] = result.documents;
270+
results.forEach(source => documents.push(...source.documents));
265271

266272
if (documents.length > 0) {
267273
outputDocuments.push(...documents);

‎packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ export class BaseResolversVisitor<
376376
protected _hasFederation = false;
377377
protected _fieldContextTypeMap: FieldContextTypeMap;
378378
private _directiveResolverMappings: Record<string, string>;
379+
private _shouldMapType: { [typeName: string]: boolean } = {};
379380

380381
constructor(
381382
rawConfig: TRawConfig,
@@ -438,15 +439,7 @@ export class BaseResolversVisitor<
438439
return `export type ResolverTypeWrapper<T> = ${this.config.resolverTypeWrapperSignature};`;
439440
}
440441

441-
protected shouldMapType(
442-
type: GraphQLNamedType,
443-
checkedBefore: { [typeName: string]: boolean } = {},
444-
duringCheck: string[] = []
445-
): boolean {
446-
if (checkedBefore[type.name] !== undefined) {
447-
return checkedBefore[type.name];
448-
}
449-
442+
protected shouldMapType(type: GraphQLNamedType, duringCheck: string[] = []): boolean {
450443
if (type.name.startsWith('__') || this.config.scalars[type.name]) {
451444
return false;
452445
}
@@ -469,16 +462,16 @@ export class BaseResolversVisitor<
469462
const field = fields[fieldName];
470463
const fieldType = getBaseType(field.type);
471464

472-
if (checkedBefore[fieldType.name] !== undefined) {
473-
return checkedBefore[fieldType.name];
465+
if (this._shouldMapType[fieldType.name] !== undefined) {
466+
return this._shouldMapType[fieldType.name];
474467
}
475468

476469
if (this.config.mappers[type.name]) {
477470
return true;
478471
}
479472

480473
duringCheck.push(type.name);
481-
const innerResult = this.shouldMapType(fieldType, checkedBefore, duringCheck);
474+
const innerResult = this.shouldMapType(fieldType, duringCheck);
482475

483476
return innerResult;
484477
});
@@ -507,13 +500,17 @@ export class BaseResolversVisitor<
507500
shouldInclude?: (type: GraphQLNamedType) => boolean
508501
): ResolverTypes {
509502
const allSchemaTypes = this._schema.getTypeMap();
510-
const nestedMapping: { [typeName: string]: boolean } = {};
511503
const typeNames = this._federation.filterTypeNames(Object.keys(allSchemaTypes));
512504

513-
typeNames.forEach(typeName => {
514-
const schemaType = allSchemaTypes[typeName];
515-
nestedMapping[typeName] = this.shouldMapType(schemaType, nestedMapping);
516-
});
505+
// avoid checking all types recursively if we have no `mappers` defined
506+
if (Object.keys(this.config.mappers).length > 0) {
507+
typeNames.forEach(typeName => {
508+
if (this._shouldMapType[typeName] === undefined) {
509+
const schemaType = allSchemaTypes[typeName];
510+
this._shouldMapType[typeName] = this.shouldMapType(schemaType);
511+
}
512+
});
513+
}
517514

518515
return typeNames.reduce((prev: ResolverTypes, typeName: string) => {
519516
const schemaType = allSchemaTypes[typeName];
@@ -594,7 +591,7 @@ export class BaseResolversVisitor<
594591
const baseType = getBaseType(field.type);
595592
const isUnion = isUnionType(baseType);
596593

597-
if (!this.config.mappers[baseType.name] && !isUnion && !nestedMapping[baseType.name]) {
594+
if (!this.config.mappers[baseType.name] && !isUnion && !this._shouldMapType[baseType.name]) {
598595
return null;
599596
}
600597

0 commit comments

Comments
 (0)