Skip to content

Commit ff9bce6

Browse files
committed
Collect used enums
1 parent 8babe46 commit ff9bce6

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

packages/plugins/typescript/operations/src/index.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { oldVisit, PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
2-
import { LoadedFragment, optimizeOperations } from '@graphql-codegen/visitor-plugin-common';
3-
import { concatAST, FragmentDefinitionNode, GraphQLSchema, Kind } from 'graphql';
2+
import { optimizeOperations } from '@graphql-codegen/visitor-plugin-common';
3+
import { concatAST, GraphQLSchema } from 'graphql';
44
import { TypeScriptDocumentsPluginConfig } from './config.js';
55
import { TypeScriptDocumentsVisitor } from './visitor.js';
66

@@ -20,19 +20,7 @@ export const plugin: PluginFunction<TypeScriptDocumentsPluginConfig, Types.Compl
2020
: rawDocuments;
2121
const allAst = concatAST(documents.map(v => v.document));
2222

23-
const allFragments: LoadedFragment[] = [
24-
...(allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION) as FragmentDefinitionNode[]).map(
25-
fragmentDef => ({
26-
node: fragmentDef,
27-
name: fragmentDef.name.value,
28-
onType: fragmentDef.typeCondition.name.value,
29-
isExternal: false,
30-
})
31-
),
32-
...(config.externalFragments || []),
33-
];
34-
35-
const visitor = new TypeScriptDocumentsVisitor(schema, config, allFragments);
23+
const visitor = new TypeScriptDocumentsVisitor(schema, config, allAst);
3624

3725
const visitorResult = oldVisit(allAst, {
3826
leave: visitor,

packages/plugins/typescript/operations/src/visitor.ts

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ import {
1313
wrapTypeWithModifiers,
1414
} from '@graphql-codegen/visitor-plugin-common';
1515
import 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';
1731
import { TypeScriptDocumentsPluginConfig } from './config.js';
1832
import { TypeScriptOperationVariablesToObject } from './ts-operation-variables-to-object.js';
1933
import { 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+
3046
export 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

Comments
 (0)