Skip to content

Commit

Permalink
Allow to have declarationKind of type: class, interface: interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Mar 7, 2021
1 parent 3cba883 commit e947f8e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/gentle-hairs-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/typescript': patch
---

Allow to have declarationKind of type: class, interface: interface
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export class BaseTypesVisitor<
originalNode: ObjectTypeDefinitionNode
): DeclarationBlock {
const optionalTypename = this.config.nonOptionalTypename ? '__typename' : '__typename?';
const { type } = this._parsedConfig.declarationKind;
const { type, interface: interfacesType } = this._parsedConfig.declarationKind;
const allFields = [
...(this.config.addTypename
? [
Expand All @@ -368,7 +368,9 @@ export class BaseTypesVisitor<

if (type === 'interface' || type === 'class') {
if (interfacesNames.length > 0) {
declarationBlock.withContent('extends ' + interfacesNames.join(', ') + (allFields.length > 0 ? ' ' : ' {}'));
const keyword = interfacesType === 'interface' && type === 'class' ? 'implements' : 'extends';

declarationBlock.withContent(`${keyword} ` + interfacesNames.join(', ') + (allFields.length > 0 ? ' ' : ' {}'));
}

declarationBlock.withBlock(this.mergeAllFields(allFields, false));
Expand Down
31 changes: 31 additions & 0 deletions packages/plugins/typescript/typescript/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,37 @@ describe('TypeScript', () => {
});

describe('Issues', () => {
it('#5643 - Incorrect combinations of declartionKinds leads to syntax error', async () => {
const testSchema = buildSchema(/* GraphQL */ `
interface Base {
id: ID!
}
type MyType implements Base {
id: ID!
}
type Query {
t: MyType!
}
`);

const result = (await plugin(
testSchema,
[],
{
declarationKind: {
type: 'class',
interface: 'interface',
},
},
{ outputFile: '' }
)) as Types.ComplexPluginOutput;
const output = mergeOutputs([result]);
expect(output).not.toContain(`export class MyType extends Base {`);
expect(output).toContain(`export class MyType implements Base {`);
});

it('#4564 - numeric enum values set on schema level', async () => {
const testSchema = new GraphQLSchema({
types: [
Expand Down

0 comments on commit e947f8e

Please sign in to comment.