Skip to content

Commit

Permalink
Fix @OneOf support broken with declaration kind other than type (#8586)
Browse files Browse the repository at this point in the history
Fixes #7938
  • Loading branch information
levrik committed Nov 15, 2022
1 parent a9a1795 commit ef4c2c9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/afraid-maps-complain.md
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/typescript': patch
---

Fix incompatibility between `@oneOf` input types and declaration kind other than `type`
Expand Up @@ -624,9 +624,12 @@ export class BaseTypesVisitor<
}

getInputObjectOneOfDeclarationBlock(node: InputObjectTypeDefinitionNode): DeclarationBlock {
// As multiple fields always result in a union, we have
// to force a declaration kind of `type` in this case
const declarationKind = node.fields.length === 1 ? this._parsedConfig.declarationKind.input : 'type';
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind(this._parsedConfig.declarationKind.input)
.asKind(declarationKind)
.withName(this.convertName(node))
.withComment(node.description as any as string)
.withContent(`\n` + node.fields.join('\n |'));
Expand Down
45 changes: 45 additions & 0 deletions packages/plugins/typescript/typescript/tests/typescript.spec.ts
Expand Up @@ -2635,6 +2635,51 @@ describe('TypeScript', () => {
`);
});

it('respects configured declaration kind with single field', async () => {
const schema = buildSchema(
/* GraphQL */ `
input Input @oneOf {
int: Int
}
type Query {
foo(input: Input!): Boolean!
}
`.concat(oneOfDirectiveDefinition)
);

const result = await plugin(schema, [], { declarationKind: 'interface' }, { outputFile: '' });

expect(result.content).toBeSimilarStringTo(`
export interface Input {
int: Scalars['Int'];
}
`);
});

it('forces declaration kind of type with multiple fields', async () => {
const schema = buildSchema(
/* GraphQL */ `
input Input @oneOf {
int: Int
boolean: Boolean
}
type Query {
foo(input: Input!): Boolean!
}
`.concat(oneOfDirectiveDefinition)
);

const result = await plugin(schema, [], { declarationKind: 'interface' }, { outputFile: '' });

expect(result.content).toBeSimilarStringTo(`
export type Input =
{ int: Scalars['Int']; boolean?: never; }
| { int?: never; boolean: Scalars['Boolean']; };
`);
});

it('raises exception for type with non-optional fields', async () => {
const schema = buildSchema(
/* GraphQL */ `
Expand Down

0 comments on commit ef4c2c9

Please sign in to comment.