Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix @oneOf support broken with declaration kind other than type #8586

Merged
merged 6 commits into from
Nov 15, 2022
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';
levrik marked this conversation as resolved.
Show resolved Hide resolved
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