Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/afraid-maps-complain.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
---

Fix incompatibility between `@oneOf` input types and declaration kind other than `type`
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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