Skip to content

Commit

Permalink
Performance work: resolvers plugins, documents loading (#7452)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
charlypoly committed Feb 2, 2022
1 parent c8ef37a commit be7cb3a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
6 changes: 6 additions & 0 deletions .changeset/witty-carrots-clap.md
@@ -0,0 +1,6 @@
---
"@graphql-codegen/cli": patch
"@graphql-codegen/visitor-plugin-common": patch
---

Performance work: resolvers plugins, documents loading
14 changes: 10 additions & 4 deletions packages/graphql-codegen-cli/src/codegen.ts
Expand Up @@ -257,11 +257,17 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
task: wrapTask(async () => {
debugLog(`[CLI] Loading Documents`);

const allDocuments = [...rootDocuments, ...outputSpecificDocuments];
// get different cache for shared docs and output specific docs
const results = await Promise.all(
[rootDocuments, outputSpecificDocuments].map(docs => {
const hash = JSON.stringify(docs);
return documentsLoadingCache.load(hash);
})
);

const documents: Types.DocumentFile[] = [];

const hash = JSON.stringify(allDocuments);
const result = await documentsLoadingCache.load(hash);
const documents: Types.DocumentFile[] = result.documents;
results.forEach(source => documents.push(...source.documents));

if (documents.length > 0) {
outputDocuments.push(...documents);
Expand Down
Expand Up @@ -376,6 +376,7 @@ export class BaseResolversVisitor<
protected _hasFederation = false;
protected _fieldContextTypeMap: FieldContextTypeMap;
private _directiveResolverMappings: Record<string, string>;
private _shouldMapType: { [typeName: string]: boolean } = {};

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

protected shouldMapType(
type: GraphQLNamedType,
checkedBefore: { [typeName: string]: boolean } = {},
duringCheck: string[] = []
): boolean {
if (checkedBefore[type.name] !== undefined) {
return checkedBefore[type.name];
}

protected shouldMapType(type: GraphQLNamedType, duringCheck: string[] = []): boolean {
if (type.name.startsWith('__') || this.config.scalars[type.name]) {
return false;
}
Expand All @@ -469,16 +462,16 @@ export class BaseResolversVisitor<
const field = fields[fieldName];
const fieldType = getBaseType(field.type);

if (checkedBefore[fieldType.name] !== undefined) {
return checkedBefore[fieldType.name];
if (this._shouldMapType[fieldType.name] !== undefined) {
return this._shouldMapType[fieldType.name];
}

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

duringCheck.push(type.name);
const innerResult = this.shouldMapType(fieldType, checkedBefore, duringCheck);
const innerResult = this.shouldMapType(fieldType, duringCheck);

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

typeNames.forEach(typeName => {
const schemaType = allSchemaTypes[typeName];
nestedMapping[typeName] = this.shouldMapType(schemaType, nestedMapping);
});
// avoid checking all types recursively if we have no `mappers` defined
if (Object.keys(this.config.mappers).length > 0) {
typeNames.forEach(typeName => {
if (this._shouldMapType[typeName] === undefined) {
const schemaType = allSchemaTypes[typeName];
this._shouldMapType[typeName] = this.shouldMapType(schemaType);
}
});
}

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

if (!this.config.mappers[baseType.name] && !isUnion && !nestedMapping[baseType.name]) {
if (!this.config.mappers[baseType.name] && !isUnion && !this._shouldMapType[baseType.name]) {
return null;
}

Expand Down

1 comment on commit be7cb3a

@vercel
Copy link

@vercel vercel bot commented on be7cb3a Feb 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.