Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,31 @@ export interface RawTypesConfig extends RawConfig {
* ```
*/
directiveArgumentAndInputFieldMappingTypeSuffix?: string;
/**
* @description When a GraphQL interface is used for a field, this flag will use the implementing types, instead of the interface itself.
* @default false
*
* @exampleMarkdown
* ## Override all definition types
*
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli'
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* useImplementingTypes: true
* }
* }
* }
* }
* export default config
* ```
*/
useImplementingTypes?: boolean;
}

const onlyUnderscoresPattern = /^_+$/;
Expand Down Expand Up @@ -788,14 +813,19 @@ export class BaseTypesVisitor<

getInterfaceTypeDeclarationBlock(
node: InterfaceTypeDefinitionNode,
_originalNode: InterfaceTypeDefinitionNode
originalNode: InterfaceTypeDefinitionNode
): DeclarationBlock {
const declarationBlock = new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind(this._parsedConfig.declarationKind.interface)
.withName(this.convertName(node))
.withComment(node.description?.value);

if (!this._parsedConfig.useImplementingTypes) {
const interfacesNames = originalNode.interfaces ? originalNode.interfaces.map(i => this.convertName(i)) : [];
declarationBlock.withContent(this.mergeInterfaces(interfacesNames, node.fields.length > 0));
}

return declarationBlock.withBlock(node.fields.join('\n'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface ParsedConfig {
importExtension: '' | `.${string}`;
printFieldsOnNewLines: boolean;
includeExternalFragments: boolean;
useImplementingTypes: boolean;
}

export interface RawConfig {
Expand Down
25 changes: 0 additions & 25 deletions packages/plugins/typescript/typescript/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,31 +411,6 @@ export interface TypeScriptPluginConfig extends RawTypesConfig {
* ```
*/
disableDescriptions?: boolean;
/**
* @description When a GraphQL interface is used for a field, this flag will use the implementing types, instead of the interface itself.
* @default false
*
* @exampleMarkdown
* ## Override all definition types
*
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli'
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* useImplementingTypes: true
* }
* }
* }
* }
* export default config
* ```
*/
useImplementingTypes?: boolean;
/**
* @name wrapEntireFieldDefinitions
* @type boolean
Expand Down
25 changes: 25 additions & 0 deletions packages/plugins/typescript/typescript/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3950,6 +3950,31 @@ describe('TypeScript', () => {
`);
});

it('should list parent interface on an intermediate interface - issue #10515', async () => {
const testSchema = buildSchema(/* GraphQL */ `
interface TopLevel {
topLevelField: Boolean
}

interface MidLevel implements TopLevel {
topLevelField: Boolean
midLevelField: Int
}

type BottomLevel implements MidLevel & TopLevel {
topLevelField: Boolean
midLevelField: Int
bottomLevelField: String
}
`);

const output = (await plugin(testSchema, [], {} as any, { outputFile: 'graphql.ts' })) as Types.ComplexPluginOutput;

expect(output.content).toBeSimilarStringTo(`
type MidLevel = TopLevel & {
`);
});

it('should use implementing types as node type - issue #5126', async () => {
const testSchema = buildSchema(/* GraphQL */ `
type Matrix {
Expand Down