@@ -13,6 +13,11 @@ import { SelectionSetToObject } from './selection-set-to-object.js';
1313import { NormalizedScalarsMap , CustomDirectivesConfig } from './types.js' ;
1414import { buildScalarsFromConfig , DeclarationBlock , DeclarationBlockConfig , getConfigValue } from './utils.js' ;
1515import { OperationVariablesToObject } from './variables-to-object.js' ;
16+ import {
17+ normalizeOperationDeclarationKind ,
18+ type OperationDeclarationKind ,
19+ type OperationDeclarationKindConfig ,
20+ } from './operation-declaration-kinds.js' ;
1621
1722export interface ParsedDocumentsConfig extends ParsedConfig {
1823 extractAllFieldsToTypes : boolean ;
@@ -30,6 +35,7 @@ export interface ParsedDocumentsConfig extends ParsedConfig {
3035 generateOperationTypes : boolean ;
3136 importSchemaTypesFrom : string ;
3237 namespacedImportName : string | null ;
38+ declarationKind : OperationDeclarationKindConfig ;
3339}
3440
3541export interface RawDocumentsConfig extends RawConfig {
@@ -248,6 +254,52 @@ export interface RawDocumentsConfig extends RawConfig {
248254 * When this option is enabled, `extractAllFieldsToTypes` is automatically enabled as well.
249255 */
250256 extractAllFieldsToTypesCompact ?: boolean ;
257+ /**
258+ * @description Overrides the default output for various GraphQL types.
259+ * @default 'type'
260+ * @exampleMarkdown
261+ * ## Override all declarations
262+ *
263+ * ```ts filename="codegen.ts"
264+ * import type { CodegenConfig } from '@graphql-codegen/cli';
265+ *
266+ * const config: CodegenConfig = {
267+ * // ...
268+ * generates: {
269+ * 'path/to/file': {
270+ * // plugins...
271+ * config: {
272+ * declarationKind: 'interface'
273+ * },
274+ * },
275+ * },
276+ * };
277+ * export default config;
278+ * ```
279+ *
280+ * ## Override only specific declarations
281+ *
282+ * ```ts filename="codegen.ts"
283+ * import type { CodegenConfig } from '@graphql-codegen/cli';
284+ *
285+ * const config: CodegenConfig = {
286+ * // ...
287+ * generates: {
288+ * 'path/to/file': {
289+ * // plugins...
290+ * config: {
291+ * declarationKind: {
292+ * input: 'interface',
293+ * result: 'type'
294+ * }
295+ * },
296+ * },
297+ * },
298+ * };
299+ * export default config;
300+ * ```
301+ */
302+ declarationKind ?: OperationDeclarationKind | Partial < OperationDeclarationKindConfig > ;
251303}
252304
253305export class BaseDocumentsVisitor <
@@ -266,6 +318,17 @@ export class BaseDocumentsVisitor<
266318 defaultScalars : NormalizedScalarsMap = DEFAULT_INPUT_SCALARS
267319 ) {
268320 const importSchemaTypesFrom = getConfigValue ( rawConfig . importSchemaTypesFrom , '' ) ;
321+ const extractAllFieldsToTypes =
322+ getConfigValue ( rawConfig . extractAllFieldsToTypes , false ) ||
323+ getConfigValue ( rawConfig . extractAllFieldsToTypesCompact , false ) ;
324+ const declarationKind = normalizeOperationDeclarationKind ( getConfigValue ( rawConfig . declarationKind , 'type' ) ) ;
325+ if ( extractAllFieldsToTypes ) {
326+ // eslint-disable-next-line no-console
327+ console . warn (
328+ "`declarationKind.result` has been set to `'type'` because `extractAllFieldsToTypes` or `extractAllFieldsToTypesCompact` is true"
329+ ) ;
330+ declarationKind . result = 'type' ;
331+ }
269332
270333 super ( rawConfig , {
271334 exportFragmentSpreadSubTypes : getConfigValue ( rawConfig . exportFragmentSpreadSubTypes , false ) ,
@@ -281,10 +344,9 @@ export class BaseDocumentsVisitor<
281344 generateOperationTypes : getConfigValue ( rawConfig . generateOperationTypes , true ) ,
282345 importSchemaTypesFrom,
283346 namespacedImportName : getConfigValue ( rawConfig . namespacedImportName , importSchemaTypesFrom ? 'Types' : null ) ,
284- extractAllFieldsToTypes :
285- getConfigValue ( rawConfig . extractAllFieldsToTypes , false ) ||
286- getConfigValue ( rawConfig . extractAllFieldsToTypesCompact , false ) ,
347+ extractAllFieldsToTypes,
287348 extractAllFieldsToTypesCompact : getConfigValue ( rawConfig . extractAllFieldsToTypesCompact , false ) ,
349+ declarationKind,
288350 ...( ( additionalConfig || { } ) as any ) ,
289351 } ) ;
290352
@@ -404,7 +466,7 @@ export class BaseDocumentsVisitor<
404466 ? ''
405467 : new DeclarationBlock ( this . _declarationBlockConfig )
406468 . export ( )
407- . asKind ( 'type' )
469+ . asKind ( this . config . declarationKind . result )
408470 . withName ( operationResultName )
409471 . withContent ( selectionSetObjects . mergedTypeString ) . string ;
410472
@@ -413,7 +475,7 @@ export class BaseDocumentsVisitor<
413475 blockTransformer : t => this . applyVariablesWrapper ( t , operationType ) ,
414476 } )
415477 . export ( )
416- . asKind ( 'type' )
478+ . asKind ( 'type' ) // Variables must always be `'type'` because it is an alias of `Exact<Something>`
417479 . withName (
418480 this . convertName ( name , {
419481 suffix : operationTypeSuffix + 'Variables' ,
@@ -426,7 +488,7 @@ export class BaseDocumentsVisitor<
426488 i =>
427489 new DeclarationBlock ( this . _declarationBlockConfig )
428490 . export ( )
429- . asKind ( 'type' )
491+ . asKind ( 'type' ) // dependentTypes must always be `'type'` because they are alias types
430492 . withName ( i . name )
431493 . withContent ( i . content ) . string
432494 )
0 commit comments