UNPKG

8.4 kBTypeScriptView Raw
1import { RawDocumentsConfig, AvoidOptionalsConfig } from '@graphql-codegen/visitor-plugin-common';
2/**
3 * @description This plugin generates TypeScript types based on your GraphQLSchema _and_ your GraphQL operations and fragments.
4 * It generates types for your GraphQL documents: Query, Mutation, Subscription and Fragment.
5 *
6 * Note: In most configurations, this plugin requires you to use `typescript as well, because it depends on its base types.
7 */
8export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
9 /**
10 * @description The [GraphQL spec](https://spec.graphql.org/draft/#sel-FAHjBJFCAACE_Gh7d)
11 * allows arrays and a single primitive value for list input. This allows to
12 * deactivate that behavior to only accept arrays instead of single values. If
13 * set to `false`, the definition: `query foo(bar: [Int!]!): Foo` will output
14 * `bar: Array<Int>` instead of `bar: Array<Int> | Int` for the variable part.
15 * @default true
16 *
17 * @exampleMarkdown
18 * ```ts filename="codegen.ts"
19 * import type { CodegenConfig } from '@graphql-codegen/cli';
20 *
21 * const config: CodegenConfig = {
22 * // ...
23 * generates: {
24 * 'path/to/file.ts': {
25 * plugins: ['typescript'],
26 * config: {
27 * arrayInputCoercion: false
28 * },
29 * },
30 * },
31 * };
32 * export default config;
33 * ```
34 */
35 arrayInputCoercion?: boolean;
36 /**
37 * @description This will cause the generator to avoid using TypeScript optionals (`?`) on types,
38 * so the following definition: `type A { myField: String }` will output `myField: Maybe<string>`
39 * instead of `myField?: Maybe<string>`.
40 * @default false
41 *
42 * @exampleMarkdown
43 * ## Override all definition types
44 *
45 * ```ts filename="codegen.ts"
46 * import type { CodegenConfig } from '@graphql-codegen/cli';
47 *
48 * const config: CodegenConfig = {
49 * // ...
50 * generates: {
51 * 'path/to/file.ts': {
52 * plugins: ['typescript'],
53 * config: {
54 * avoidOptionals: true
55 * },
56 * },
57 * },
58 * };
59 * export default config;
60 * ```
61 *
62 * ## Override only specific definition types
63 *
64 * ```ts filename="codegen.ts"
65 * import type { CodegenConfig } from '@graphql-codegen/cli';
66 *
67 * const config: CodegenConfig = {
68 * // ...
69 * generates: {
70 * 'path/to/file.ts': {
71 * plugins: ['typescript'],
72 * config: {
73 * avoidOptionals: {
74 * field: true
75 * inputValue: true
76 * object: true
77 * defaultValue: true
78 * }
79 * },
80 * },
81 * },
82 * };
83 * export default config;
84 * ```
85 */
86 avoidOptionals?: boolean | AvoidOptionalsConfig;
87 /**
88 * @description Generates immutable types by adding `readonly` to properties and uses `ReadonlyArray`.
89 * @default false
90 *
91 * @exampleMarkdown
92 * ```ts filename="codegen.ts"
93 * import type { CodegenConfig } from '@graphql-codegen/cli';
94 *
95 * const config: CodegenConfig = {
96 * // ...
97 * generates: {
98 * 'path/to/file.ts': {
99 * plugins: ['typescript'],
100 * config: {
101 * immutableTypes: true
102 * },
103 * },
104 * },
105 * };
106 * export default config;
107 * ```
108 */
109 immutableTypes?: boolean;
110 /**
111 * @description Flatten fragment spread and inline fragments into a simple selection set before generating.
112 * @default false
113 *
114 * @exampleMarkdown
115 * ```ts filename="codegen.ts"
116 * import type { CodegenConfig } from '@graphql-codegen/cli';
117 *
118 * const config: CodegenConfig = {
119 * // ...
120 * generates: {
121 * 'path/to/file.ts': {
122 * plugins: ['typescript', 'typescript-operations'],
123 * config: {
124 * flattenGeneratedTypes: true
125 * },
126 * },
127 * },
128 * };
129 * export default config;
130 * ```
131 */
132 flattenGeneratedTypes?: boolean;
133 /**
134 * @description Include all fragments types when flattenGeneratedTypes is enabled.
135 * @default false
136 *
137 * @exampleMarkdown
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.ts': {
145 * plugins: ['typescript', 'typescript-operations'],
146 * config: {
147 * flattenGeneratedTypes: true,
148 * flattenGeneratedTypesIncludeFragments: true
149 * },
150 * },
151 * },
152 * };
153 * export default config;
154 * ```
155 */
156 flattenGeneratedTypesIncludeFragments?: boolean;
157 /**
158 * @description Set to `true` in order to generate output without `export` modifier.
159 * This is useful if you are generating `.d.ts` file and want it to be globally available.
160 * @default false
161 *
162 * @exampleMarkdown
163 * ```ts filename="codegen.ts"
164 * import type { CodegenConfig } from '@graphql-codegen/cli';
165 *
166 * const config: CodegenConfig = {
167 * // ...
168 * generates: {
169 * 'path/to/file.ts': {
170 * plugins: ['typescript'],
171 * config: {
172 * noExport: true
173 * },
174 * },
175 * },
176 * };
177 * export default config;
178 * ```
179 */
180 noExport?: boolean;
181 globalNamespace?: boolean;
182 /**
183 * @name addOperationExport
184 * @type boolean
185 * @description Add const export of the operation name to output file. Pay attention that the file should be `d.ts`.
186 * You can combine it with `near-operation-file preset` and therefore the types will be generated along with graphql file. Then you need to set extension in `presetConfig` to be `.gql.d.ts` and by that you can import `gql` file in `ts` files.
187 * It will allow you to get everything with one import:
188 *
189 * ```ts
190 * import { GetClient, GetClientQuery, GetClientQueryVariables } from './GetClient.gql.js'
191 * ```
192 * @default false
193 * @see https://github.com/dotansimha/graphql-code-generator/issues/3949
194 *
195 * @exampleMarkdown
196 * ```ts filename="codegen.ts"
197 * import type { CodegenConfig } from '@graphql-codegen/cli';
198 *
199 * const config: CodegenConfig = {
200 * // ...
201 * generates: {
202 * "./typings/api.ts": {
203 * "plugins": [
204 * "typescript"
205 * ]
206 * },
207 * "./": {
208 * "preset": "near-operation-file",
209 * "presetConfig": {
210 * "baseTypesPath": "./typings/api.ts",
211 * "extension": ".gql.d.ts"
212 * },
213 * "plugins": [
214 * "@graphql-codegen/typescript-operations"
215 * ],
216 * "config": {
217 * "addOperationExport": true
218 * }
219 * }
220 * };
221 * export default config;
222 * ```
223 */
224 addOperationExport?: boolean;
225 /**
226 * @description Allow to override the type value of `Maybe`.
227 * @default T | null
228 *
229 * @exampleMarkdown
230 * ## Allow undefined
231 * ```ts filename="codegen.ts"
232 * import type { CodegenConfig } from '@graphql-codegen/cli';
233 *
234 * const config: CodegenConfig = {
235 * // ...
236 * generates: {
237 * 'path/to/file.ts': {
238 * plugins: ['typescript'],
239 * config: {
240 * maybeValue: 'T | null | undefined'
241 * },
242 * },
243 * },
244 * };
245 * export default config;
246 * ```
247 *
248 * ## Allow `null` in resolvers:
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.ts': {
256 * plugins: ['typescript'],
257 * config: {
258 * maybeValue: 'T extends PromiseLike<infer U> ? Promise<U | null> : T | null'
259 * },
260 * },
261 * },
262 * };
263 * export default config;
264 * ```
265 */
266 maybeValue?: string;
267}