@@ -13,7 +13,21 @@ import {
1313 wrapTypeWithModifiers ,
1414} from '@graphql-codegen/visitor-plugin-common' ;
1515import autoBind from 'auto-bind' ;
16- import { GraphQLNamedType , GraphQLOutputType , GraphQLSchema , isEnumType , isNonNullType } from 'graphql' ;
16+ import {
17+ type DocumentNode ,
18+ type FragmentDefinitionNode ,
19+ GraphQLEnumType ,
20+ GraphQLInputObjectType ,
21+ type GraphQLNamedInputType ,
22+ type GraphQLNamedType ,
23+ type GraphQLOutputType ,
24+ GraphQLScalarType ,
25+ type GraphQLSchema ,
26+ isEnumType ,
27+ isNonNullType ,
28+ Kind ,
29+ visit ,
30+ } from 'graphql' ;
1731import { TypeScriptDocumentsPluginConfig } from './config.js' ;
1832import { TypeScriptOperationVariablesToObject } from './ts-operation-variables-to-object.js' ;
1933import { TypeScriptSelectionSetProcessor } from './ts-selection-set-processor.js' ;
@@ -27,11 +41,14 @@ export interface TypeScriptDocumentsParsedConfig extends ParsedDocumentsConfig {
2741 allowUndefinedQueryVariables : boolean ;
2842}
2943
44+ type UsedNamedInputTypes = Record < string , GraphQLNamedInputType > ;
45+
3046export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor <
3147 TypeScriptDocumentsPluginConfig ,
3248 TypeScriptDocumentsParsedConfig
3349> {
34- constructor ( schema : GraphQLSchema , config : TypeScriptDocumentsPluginConfig , allFragments : LoadedFragment [ ] ) {
50+ protected _usedNamedInputTypes : UsedNamedInputTypes = { } ;
51+ constructor ( schema : GraphQLSchema , config : TypeScriptDocumentsPluginConfig , documentNode : DocumentNode ) {
3552 super (
3653 config ,
3754 {
@@ -76,6 +93,20 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
7693 return ( this . config . immutableTypes ? `readonly ${ name } ` : name ) + ( optional ? '?' : '' ) ;
7794 } ;
7895
96+ const allFragments : LoadedFragment [ ] = [
97+ ...( documentNode . definitions . filter ( d => d . kind === Kind . FRAGMENT_DEFINITION ) as FragmentDefinitionNode [ ] ) . map (
98+ fragmentDef => ( {
99+ node : fragmentDef ,
100+ name : fragmentDef . name . value ,
101+ onType : fragmentDef . typeCondition . name . value ,
102+ isExternal : false ,
103+ } )
104+ ) ,
105+ ...( config . externalFragments || [ ] ) ,
106+ ] ;
107+
108+ this . _usedNamedInputTypes = this . collectUsedInputTypes ( { schema, documentNode } ) ;
109+
79110 const processorConfig : SelectionSetProcessorConfig = {
80111 namespacedImportName : this . config . namespacedImportName ,
81112 convertName : this . convertName . bind ( this ) ,
@@ -142,4 +173,36 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
142173
143174 return `${ prefix } Exact<${ variablesBlock === '{}' ? `{ [key: string]: never; }` : variablesBlock } >${ extraType } ` ;
144175 }
176+
177+ private collectUsedInputTypes ( {
178+ schema,
179+ documentNode,
180+ } : {
181+ schema : GraphQLSchema ;
182+ documentNode : DocumentNode ;
183+ } ) : UsedNamedInputTypes {
184+ const schemaTypes = schema . getTypeMap ( ) ;
185+
186+ const usedInputTypes : UsedNamedInputTypes = { } ;
187+
188+ visit ( documentNode , {
189+ VariableDefinition : variableDefinitionNode => {
190+ visit ( variableDefinitionNode , {
191+ NamedType : namedTypeNode => {
192+ const foundInputType = schemaTypes [ namedTypeNode . name . value ] ;
193+ if (
194+ foundInputType &&
195+ ( foundInputType instanceof GraphQLInputObjectType ||
196+ foundInputType instanceof GraphQLScalarType ||
197+ foundInputType instanceof GraphQLEnumType )
198+ ) {
199+ usedInputTypes [ namedTypeNode . name . value ] = foundInputType ;
200+ }
201+ } ,
202+ } ) ;
203+ } ,
204+ } ) ;
205+
206+ return usedInputTypes ;
207+ }
145208}
0 commit comments