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 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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