UNPKG

11.9 kBTypeScriptView Raw
1import { ASTNode, FragmentDefinitionNode, OperationDefinitionNode } from 'graphql';
2import { FragmentImport, ImportDeclaration } from './imports.js';
3import { ConvertFn, ConvertOptions, DeclarationKind, LoadedFragment, NamingConvention, NormalizedScalarsMap, ParsedScalarsMap, ScalarsMap } from './types.js';
4import { DeclarationBlockConfig } from './utils.js';
5export interface BaseVisitorConvertOptions {
6 useTypesPrefix?: boolean;
7 useTypesSuffix?: boolean;
8}
9export type InlineFragmentTypeOptions = 'inline' | 'combine' | 'mask';
10export interface ParsedConfig {
11 scalars: ParsedScalarsMap;
12 convert: ConvertFn;
13 typesPrefix: string;
14 typesSuffix: string;
15 addTypename: boolean;
16 nonOptionalTypename: boolean;
17 externalFragments: LoadedFragment[];
18 fragmentImports: ImportDeclaration<FragmentImport>[];
19 immutableTypes: boolean;
20 useTypeImports: boolean;
21 dedupeFragments: boolean;
22 allowEnumStringTypes: boolean;
23 inlineFragmentTypes: InlineFragmentTypeOptions;
24 emitLegacyCommonJSImports: boolean;
25}
26export interface RawConfig {
27 /**
28 * @description Makes scalars strict.
29 *
30 * If scalars are found in the schema that are not defined in `scalars`
31 * an error will be thrown during codegen.
32 * @default false
33 *
34 * @exampleMarkdown
35 * ```ts filename="codegen.ts"
36 * import type { CodegenConfig } from '@graphql-codegen/cli';
37 *
38 * const config: CodegenConfig = {
39 * // ...
40 * generates: {
41 * 'path/to/file': {
42 * // plugins...
43 * config: {
44 * strictScalars: true,
45 * },
46 * },
47 * },
48 * };
49 * export default config;
50 * ```
51 */
52 strictScalars?: boolean;
53 /**
54 * @description Allows you to override the type that unknown scalars will have.
55 * @default any
56 *
57 * @exampleMarkdown
58 * ```ts filename="codegen.ts"
59 * import type { CodegenConfig } from '@graphql-codegen/cli';
60 *
61 * const config: CodegenConfig = {
62 * // ...
63 * generates: {
64 * 'path/to/file': {
65 * // plugins...
66 * config: {
67 * defaultScalarType: 'unknown'
68 * },
69 * },
70 * },
71 * };
72 * export default config;
73 * ```
74 */
75 defaultScalarType?: string;
76 /**
77 * @description Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
78 *
79 * @exampleMarkdown
80 * ```ts filename="codegen.ts"
81 * import type { CodegenConfig } from '@graphql-codegen/cli';
82 *
83 * const config: CodegenConfig = {
84 * // ...
85 * generates: {
86 * 'path/to/file': {
87 * // plugins...
88 * config: {
89 * scalars: {
90 * ID: {
91 * input: 'string',
92 * output: 'string | number'
93 * }
94 * DateTime: 'Date',
95 * JSON: '{ [key: string]: any }',
96 * }
97 * },
98 * },
99 * },
100 * };
101 * export default config;
102 * ```
103 */
104 scalars?: ScalarsMap;
105 /**
106 * @default change-case-all#pascalCase
107 * @description Allow you to override the naming convention of the output.
108 * You can either override all namings, or specify an object with specific custom naming convention per output.
109 * The format of the converter must be a valid `module#method`.
110 * Allowed values for specific output are: `typeNames`, `enumValues`.
111 * You can also use "keep" to keep all GraphQL names as-is.
112 * Additionally, you can set `transformUnderscore` to `true` if you want to override the default behavior,
113 * which is to preserve underscores.
114 *
115 * Available case functions in `change-case-all` are `camelCase`, `capitalCase`, `constantCase`, `dotCase`, `headerCase`, `noCase`, `paramCase`, `pascalCase`, `pathCase`, `sentenceCase`, `snakeCase`, `lowerCase`, `localeLowerCase`, `lowerCaseFirst`, `spongeCase`, `titleCase`, `upperCase`, `localeUpperCase` and `upperCaseFirst`
116 * [See more](https://github.com/btxtiger/change-case-all)
117 *
118 * @exampleMarkdown
119 * ## Override All Names
120 * ```ts filename="codegen.ts"
121 * import type { CodegenConfig } from '@graphql-codegen/cli';
122 *
123 * const config: CodegenConfig = {
124 * // ...
125 * generates: {
126 * 'path/to/file': {
127 * // plugins...
128 * config: {
129 * namingConvention: 'change-case-all#lowerCase',
130 * },
131 * },
132 * },
133 * };
134 * export default config;
135 * ```
136 *
137 * ## Upper-case enum values
138 * ```ts filename="codegen.ts"
139 * import type { CodegenConfig } from '@graphql-codegen/cli';
140 *
141 * const config: CodegenConfig = {
142 * // ...
143 * generates: {
144 * 'path/to/file': {
145 * // plugins...
146 * config: {
147 * namingConvention: {
148 * typeNames: 'change-case-all#pascalCase',
149 * enumValues: 'change-case-all#upperCase',
150 * }
151 * },
152 * },
153 * },
154 * };
155 * export default config;
156 * ```
157 *
158 * ## Keep names as is
159 * ```ts filename="codegen.ts"
160 * import type { CodegenConfig } from '@graphql-codegen/cli';
161 *
162 * const config: CodegenConfig = {
163 * // ...
164 * generates: {
165 * 'path/to/file': {
166 * // plugins...
167 * config: {
168 * namingConvention: 'keep',
169 * },
170 * },
171 * },
172 * };
173 * export default config;
174 * ```
175 *
176 * ## Remove Underscores
177 * ```ts filename="codegen.ts"
178 * import type { CodegenConfig } from '@graphql-codegen/cli';
179 *
180 * const config: CodegenConfig = {
181 * // ...
182 * generates: {
183 * 'path/to/file': {
184 * // plugins...
185 * config: {
186 * namingConvention: {
187 * typeNames: 'change-case-all#pascalCase',
188 * transformUnderscore: true
189 * }
190 * },
191 * },
192 * },
193 * };
194 * export default config;
195 * ```
196 */
197 namingConvention?: NamingConvention;
198 /**
199 * @default ""
200 * @description Prefixes all the generated types.
201 *
202 * @exampleMarkdown
203 * ```ts filename="codegen.ts"
204 * import type { CodegenConfig } from '@graphql-codegen/cli';
205 *
206 * const config: CodegenConfig = {
207 * // ...
208 * generates: {
209 * 'path/to/file': {
210 * // plugins...
211 * config: {
212 * typesPrefix: 'I',
213 * },
214 * },
215 * },
216 * };
217 * export default config;
218 * ```
219 */
220 typesPrefix?: string;
221 /**
222 * @default ""
223 * @description Suffixes all the generated types.
224 *
225 * @exampleMarkdown
226 * ```ts filename="codegen.ts"
227 * import type { CodegenConfig } from '@graphql-codegen/cli';
228 *
229 * const config: CodegenConfig = {
230 * // ...
231 * generates: {
232 * 'path/to/file': {
233 * // plugins...
234 * config: {
235 * typesSuffix: 'I',
236 * },
237 * },
238 * },
239 * };
240 * export default config;
241 * ```
242 */
243 typesSuffix?: string;
244 /**
245 * @default false
246 * @description Does not add `__typename` to the generated types, unless it was specified in the selection set.
247 *
248 * @exampleMarkdown
249 * ```ts filename="codegen.ts"
250 * import type { CodegenConfig } from '@graphql-codegen/cli';
251 *
252 * const config: CodegenConfig = {
253 * // ...
254 * generates: {
255 * 'path/to/file': {
256 * // plugins...
257 * config: {
258 * skipTypename: true
259 * },
260 * },
261 * },
262 * };
263 * export default config;
264 * ```
265 */
266 skipTypename?: boolean;
267 /**
268 * @default false
269 * @description Automatically adds `__typename` field to the generated types, even when they are not specified
270 * in the selection set, and makes it non-optional
271 *
272 * @exampleMarkdown
273 * ```ts filename="codegen.ts"
274 * import type { CodegenConfig } from '@graphql-codegen/cli';
275 *
276 * const config: CodegenConfig = {
277 * // ...
278 * generates: {
279 * 'path/to/file': {
280 * // plugins...
281 * config: {
282 * nonOptionalTypename: true
283 * },
284 * },
285 * },
286 * };
287 * export default config;
288 * ```
289 */
290 nonOptionalTypename?: boolean;
291 /**
292 * @name useTypeImports
293 * @type boolean
294 * @default false
295 * @description Will use `import type {}` rather than `import {}` when importing only types. This gives
296 * compatibility with TypeScript's "importsNotUsedAsValues": "error" option
297 *
298 * @exampleMarkdown
299 * ```ts filename="codegen.ts"
300 * import type { CodegenConfig } from '@graphql-codegen/cli';
301 *
302 * const config: CodegenConfig = {
303 * // ...
304 * generates: {
305 * 'path/to/file': {
306 * // plugins...
307 * config: {
308 * useTypeImports: true
309 * },
310 * },
311 * },
312 * };
313 * export default config;
314 * ```
315 */
316 useTypeImports?: boolean;
317 /**
318 * @ignore
319 */
320 externalFragments?: LoadedFragment[];
321 /**
322 * @ignore
323 */
324 fragmentImports?: ImportDeclaration<FragmentImport>[];
325 /**
326 * @ignore
327 */
328 globalNamespace?: boolean;
329 /**
330 * @description Removes fragment duplicates for reducing data transfer.
331 * It is done by removing sub-fragments imports from fragment definition
332 * Instead - all of them are imported to the Operation node.
333 * @type boolean
334 * @default false
335 * @deprecated This option is no longer needed. It will be removed in the next major version.
336 */
337 dedupeFragments?: boolean;
338 /**
339 * @ignore
340 */
341 allowEnumStringTypes?: boolean;
342 /**
343 * @description Whether fragment types should be inlined into other operations.
344 * "inline" is the default behavior and will perform deep inlining fragment types within operation type definitions.
345 * "combine" is the previous behavior that uses fragment type references without inlining the types (and might cause issues with deeply nested fragment that uses list types).
346 *
347 * @type string
348 * @default inline
349 */
350 inlineFragmentTypes?: InlineFragmentTypeOptions;
351 /**
352 * @default true
353 * @description Emit legacy common js imports.
354 * Default it will be `true` this way it ensure that generated code works with [non-compliant bundlers](https://github.com/dotansimha/graphql-code-generator/issues/8065).
355 */
356 emitLegacyCommonJSImports?: boolean;
357}
358export declare class BaseVisitor<TRawConfig extends RawConfig = RawConfig, TPluginConfig extends ParsedConfig = ParsedConfig> {
359 protected _parsedConfig: TPluginConfig;
360 protected _declarationBlockConfig: DeclarationBlockConfig;
361 readonly scalars: NormalizedScalarsMap;
362 constructor(rawConfig: TRawConfig, additionalConfig: Partial<TPluginConfig>);
363 protected getVisitorKindContextFromAncestors(ancestors: ASTNode[]): string[];
364 get config(): TPluginConfig;
365 convertName(node: ASTNode | string, options?: BaseVisitorConvertOptions & ConvertOptions): string;
366 getOperationSuffix(node: FragmentDefinitionNode | OperationDefinitionNode | string, operationType: string): string;
367 getFragmentSuffix(node: FragmentDefinitionNode | string): string;
368 getFragmentName(node: FragmentDefinitionNode | string): string;
369 getFragmentVariableName(node: FragmentDefinitionNode | string): string;
370 protected getPunctuation(_declarationKind: DeclarationKind): string;
371}