From fedd71cbb7f37440a59032d942cb228df78d52e5 Mon Sep 17 00:00:00 2001 From: Daniel Waltz Date: Wed, 7 Dec 2022 15:43:34 -0500 Subject: [PATCH] feat(schema-ast): support gql extension name for schema-ast output (#8712) * feat(schema-ast): support gql extension name for schema-ast output resolve #8711 * chore: add changeset for schema-ast gql extension support * chore: mark schema-ast release as a minor version change --- .changeset/strange-moose-clean.md | 5 +++++ .../plugins/other/schema-ast/src/index.ts | 8 ++++++-- .../other/schema-ast/tests/schema-ast.spec.ts | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 .changeset/strange-moose-clean.md diff --git a/.changeset/strange-moose-clean.md b/.changeset/strange-moose-clean.md new file mode 100644 index 00000000000..b4f5cf48450 --- /dev/null +++ b/.changeset/strange-moose-clean.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/schema-ast': minor +--- + +support gql file extension for schema-ast output diff --git a/packages/plugins/other/schema-ast/src/index.ts b/packages/plugins/other/schema-ast/src/index.ts index 1bc59abf475..999cfc89ccb 100644 --- a/packages/plugins/other/schema-ast/src/index.ts +++ b/packages/plugins/other/schema-ast/src/index.ts @@ -130,8 +130,12 @@ export const validate: PluginValidateFn = async ( ) => { const singlePlugin = allPlugins.length === 1; - if (singlePlugin && extname(outputFile) !== '.graphql') { - throw new Error(`Plugin "schema-ast" requires extension to be ".graphql"!`); + const allowedExtensions = ['.graphql', '.gql']; + const isAllowedExtension = allowedExtensions.includes(extname(outputFile)); + + if (singlePlugin && !isAllowedExtension) { + const allowedExtensionsOutput = allowedExtensions.map(extension => `"${extension}"`).join(' or '); + throw new Error(`Plugin "schema-ast" requires extension to be ${allowedExtensionsOutput}!`); } }; diff --git a/packages/plugins/other/schema-ast/tests/schema-ast.spec.ts b/packages/plugins/other/schema-ast/tests/schema-ast.spec.ts index 78ab612817d..68b843cbd93 100644 --- a/packages/plugins/other/schema-ast/tests/schema-ast.spec.ts +++ b/packages/plugins/other/schema-ast/tests/schema-ast.spec.ts @@ -23,7 +23,7 @@ describe('Schema AST', () => { throw new Error(SHOULD_THROW_ERROR); } catch (e) { expect(e.message).not.toBe(SHOULD_THROW_ERROR); - expect(e.message).toBe('Plugin "schema-ast" requires extension to be ".graphql"!'); + expect(e.message).toBe('Plugin "schema-ast" requires extension to be ".graphql" or ".gql"!'); } }); @@ -45,7 +45,7 @@ describe('Schema AST', () => { } }); - it('Should allow graphql extension when its the only plugin', async () => { + it('Should allow .graphql extension when its the only plugin', async () => { const fileName = 'output.graphql'; const plugins: Types.ConfiguredPlugin[] = [ { @@ -59,6 +59,21 @@ describe('Schema AST', () => { expect(true).toBeFalsy(); } }); + + it('Should allow .gql extension when its the only plugin', async () => { + const fileName = 'output.gql'; + const plugins: Types.ConfiguredPlugin[] = [ + { + 'schema-ast': {}, + }, + ]; + + try { + await validate(null, null, null, fileName, plugins); + } catch (e) { + expect(true).toBeFalsy(); + } + }); }); describe('Output', () => { const typeDefs = /* GraphQL */ `