Skip to content

Commit

Permalink
feat(schema-ast): support gql extension name for schema-ast output (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
danielwaltz committed Dec 7, 2022
1 parent 46f7530 commit fedd71c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-moose-clean.md
@@ -0,0 +1,5 @@
---
'@graphql-codegen/schema-ast': minor
---

support gql file extension for schema-ast output
8 changes: 6 additions & 2 deletions packages/plugins/other/schema-ast/src/index.ts
Expand Up @@ -130,8 +130,12 @@ export const validate: PluginValidateFn<any> = 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}!`);
}
};

Expand Down
19 changes: 17 additions & 2 deletions packages/plugins/other/schema-ast/tests/schema-ast.spec.ts
Expand Up @@ -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"!');
}
});

Expand All @@ -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[] = [
{
Expand All @@ -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 */ `
Expand Down

0 comments on commit fedd71c

Please sign in to comment.