Skip to content

Commit

Permalink
perf(cli): Improve codegen documents and schema validation (#8548)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
charlypoly and github-actions[bot] committed Nov 1, 2022
1 parent cb73107 commit 516170e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-codegen_core-8548-dependencies.md
@@ -0,0 +1,5 @@
---
"@graphql-codegen/core": patch
---
dependencies updates:
- Updated dependency [`@graphql-tools/utils@9.0.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/9.0.0) (from `^8.8.0`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/giant-ads-sit.md
@@ -0,0 +1,5 @@
---
'@graphql-codegen/core': patch
---

Improve codegen documents and schema validation
2 changes: 1 addition & 1 deletion packages/graphql-codegen-core/package.json
Expand Up @@ -39,7 +39,7 @@
"dependencies": {
"@graphql-codegen/plugin-helpers": "^2.7.2",
"@graphql-tools/schema": "^9.0.0",
"@graphql-tools/utils": "^8.8.0",
"@graphql-tools/utils": "9.0.0",
"tslib": "~2.4.0"
},
"main": "dist/cjs/index.js",
Expand Down
26 changes: 22 additions & 4 deletions packages/graphql-codegen-core/src/codegen.ts
Expand Up @@ -8,7 +8,7 @@ import {
} from '@graphql-codegen/plugin-helpers';
import { visit, DefinitionNode, Kind, print, NameNode, specifiedRules, DocumentNode } from 'graphql';
import { executePlugin } from './execute-plugin.js';
import { checkValidationErrors, validateGraphQlDocuments, Source, asArray } from '@graphql-tools/utils';
import { validateGraphQlDocuments, Source, asArray } from '@graphql-tools/utils';

import { mergeSchemas } from '@graphql-tools/schema';
import {
Expand Down Expand Up @@ -89,7 +89,13 @@ export async function codegen(options: Types.GenerateOptions): Promise<string> {
const schemaHash = extractHashFromSchema(schemaInstance);

if (!schemaHash || !options.cache || documents.some(d => typeof d.hash !== 'string')) {
return validateGraphQlDocuments(schemaInstance, [...documents, ...fragments], rules);
return Promise.resolve(
validateGraphQlDocuments(
schemaInstance,
[...documents.flatMap(d => d.document), ...fragments.flatMap(f => f.document)],
rules
)
);
}

const cacheKey = [schemaHash]
Expand All @@ -98,10 +104,22 @@ export async function codegen(options: Types.GenerateOptions): Promise<string> {
.join(',');

return options.cache('documents-validation', cacheKey, () =>
validateGraphQlDocuments(schemaInstance, [...documents, ...fragments], rules)
Promise.resolve(
validateGraphQlDocuments(
schemaInstance,
[...documents.flatMap(d => d.document), ...fragments.flatMap(f => f.document)],
rules
)
)
);
}, 'Validate documents against schema');
checkValidationErrors(errors);

if (errors.length > 0) {
throw new Error(
`GraphQL Document Validation failed with ${errors.length} errors;
${errors.map((error, index) => `Error ${index}: ${error.stack}`).join('\n\n')}`
);
}
}

const prepend: Set<string> = new Set<string>();
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Expand Up @@ -2017,6 +2017,13 @@
dependencies:
tslib "^2.4.0"

"@graphql-tools/utils@9.0.0":
version "9.0.0"
resolved "https://registry.yarnpkg.com/@graphql-tools/utils/-/utils-9.0.0.tgz#4e75680d29e866b3de293b4bc9fa4a40aeae0085"
integrity sha512-kaCwyWnURxMsYbxzkfylLqFFelu83jKk3BJOOy0GIuxEtgXVS9v7Y/tojljo69Q+jaZ2YxAi3+d8IpM+hx768A==
dependencies:
tslib "^2.4.0"

"@graphql-tools/utils@^6.2.4":
version "6.2.4"
resolved "https://registry.yarnpkg.com/@graphql-tools/utils/-/utils-6.2.4.tgz#38a2314d2e5e229ad4f78cca44e1199e18d55856"
Expand Down

0 comments on commit 516170e

Please sign in to comment.