Skip to content

Commit fedd71c

Browse files
authored
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
1 parent 46f7530 commit fedd71c

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

.changeset/strange-moose-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/schema-ast': minor
3+
---
4+
5+
support gql file extension for schema-ast output

packages/plugins/other/schema-ast/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ export const validate: PluginValidateFn<any> = async (
130130
) => {
131131
const singlePlugin = allPlugins.length === 1;
132132

133-
if (singlePlugin && extname(outputFile) !== '.graphql') {
134-
throw new Error(`Plugin "schema-ast" requires extension to be ".graphql"!`);
133+
const allowedExtensions = ['.graphql', '.gql'];
134+
const isAllowedExtension = allowedExtensions.includes(extname(outputFile));
135+
136+
if (singlePlugin && !isAllowedExtension) {
137+
const allowedExtensionsOutput = allowedExtensions.map(extension => `"${extension}"`).join(' or ');
138+
throw new Error(`Plugin "schema-ast" requires extension to be ${allowedExtensionsOutput}!`);
135139
}
136140
};
137141

packages/plugins/other/schema-ast/tests/schema-ast.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Schema AST', () => {
2323
throw new Error(SHOULD_THROW_ERROR);
2424
} catch (e) {
2525
expect(e.message).not.toBe(SHOULD_THROW_ERROR);
26-
expect(e.message).toBe('Plugin "schema-ast" requires extension to be ".graphql"!');
26+
expect(e.message).toBe('Plugin "schema-ast" requires extension to be ".graphql" or ".gql"!');
2727
}
2828
});
2929

@@ -45,7 +45,7 @@ describe('Schema AST', () => {
4545
}
4646
});
4747

48-
it('Should allow graphql extension when its the only plugin', async () => {
48+
it('Should allow .graphql extension when its the only plugin', async () => {
4949
const fileName = 'output.graphql';
5050
const plugins: Types.ConfiguredPlugin[] = [
5151
{
@@ -59,6 +59,21 @@ describe('Schema AST', () => {
5959
expect(true).toBeFalsy();
6060
}
6161
});
62+
63+
it('Should allow .gql extension when its the only plugin', async () => {
64+
const fileName = 'output.gql';
65+
const plugins: Types.ConfiguredPlugin[] = [
66+
{
67+
'schema-ast': {},
68+
},
69+
];
70+
71+
try {
72+
await validate(null, null, null, fileName, plugins);
73+
} catch (e) {
74+
expect(true).toBeFalsy();
75+
}
76+
});
6277
});
6378
describe('Output', () => {
6479
const typeDefs = /* GraphQL */ `

0 commit comments

Comments
 (0)