UNPKG

10.3 kBTypeScriptView Raw
1import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.js';
2import { FragmentDefinitionNode, OperationDefinitionNode, GraphQLSchema } from 'graphql';
3import { Types } from '@graphql-codegen/plugin-helpers';
4import { LoadedFragment, ParsedImport } from './types.js';
5export declare enum DocumentMode {
6 graphQLTag = "graphQLTag",
7 documentNode = "documentNode",
8 documentNodeImportFragments = "documentNodeImportFragments",
9 external = "external",
10 string = "string"
11}
12export interface RawClientSideBasePluginConfig extends RawConfig {
13 /**
14 * @description Deprecated. Changes the documentMode to `documentNode`.
15 * @default false
16 */
17 noGraphQLTag?: boolean;
18 /**
19 * @default graphql-tag#gql
20 * @description Customize from which module will `gql` be imported from.
21 * This is useful if you want to use modules other than `graphql-tag`, e.g. `graphql.macro`.
22 *
23 * @exampleMarkdown
24 * ## graphql.macro
25 *
26 * ```ts filename="codegen.ts"
27 * import type { CodegenConfig } from '@graphql-codegen/cli';
28 *
29 * const config: CodegenConfig = {
30 * // ...
31 * generates: {
32 * 'path/to/file': {
33 * // plugins...
34 * config: {
35 * gqlImport: 'graphql.macro#gql'
36 * },
37 * },
38 * },
39 * };
40 * export default config;
41 * ```
42 *
43 * ## Gatsby
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': {
52 * // plugins...
53 * config: {
54 * gqlImport: 'gatsby#graphql'
55 * },
56 * },
57 * },
58 * };
59 * export default config;
60 * ```
61 */
62 gqlImport?: string;
63 /**
64 * @default graphql#DocumentNode
65 * @description Customize from which module will `DocumentNode` be imported from.
66 * This is useful if you want to use modules other than `graphql`, e.g. `@graphql-typed-document-node`.
67 */
68 documentNodeImport?: string;
69 /**
70 * @default false
71 * @description Set this configuration to `true` if you wish to tell codegen to generate code with no `export` identifier.
72 */
73 noExport?: boolean;
74 /**
75 * @default false
76 * @description Set this configuration to `true` if you wish to make sure to remove duplicate operation name suffix.
77 */
78 dedupeOperationSuffix?: boolean;
79 /**
80 * @default false
81 * @description Set this configuration to `true` if you wish to disable auto add suffix of operation name, like `Query`, `Mutation`, `Subscription`, `Fragment`.
82 */
83 omitOperationSuffix?: boolean;
84 /**
85 * @default ""
86 * @description Adds a suffix to generated operation result type names
87 */
88 operationResultSuffix?: string;
89 /**
90 * @default ""
91 * @description Changes the GraphQL operations variables prefix.
92 */
93 documentVariablePrefix?: string;
94 /**
95 * @default Document
96 * @description Changes the GraphQL operations variables suffix.
97 */
98 documentVariableSuffix?: string;
99 /**
100 * @default ""
101 * @description Changes the GraphQL fragments variables prefix.
102 */
103 fragmentVariablePrefix?: string;
104 /**
105 * @default FragmentDoc
106 * @description Changes the GraphQL fragments variables suffix.
107 */
108 fragmentVariableSuffix?: string;
109 /**
110 * @default graphQLTag
111 * @description Declares how DocumentNode are created:
112 *
113 * - `graphQLTag`: `graphql-tag` or other modules (check `gqlImport`) will be used to generate document nodes. If this is used, document nodes are generated on client side i.e. the module used to generate this will be shipped to the client
114 * - `documentNode`: document nodes will be generated as objects when we generate the templates.
115 * - `documentNodeImportFragments`: Similar to documentNode except it imports external fragments instead of embedding them.
116 * - `external`: document nodes are imported from an external file. To be used with `importDocumentNodeExternallyFrom`
117 *
118 * Note that some plugins (like `typescript-graphql-request`) also supports `string` for this parameter.
119 *
120 */
121 documentMode?: DocumentMode;
122 /**
123 * @default true
124 * @description If you are using `documentNode: documentMode | documentNodeImportFragments`, you can set this to `true` to apply document optimizations for your GraphQL document.
125 * This will remove all "loc" and "description" fields from the compiled document, and will remove all empty arrays (such as `directives`, `arguments` and `variableDefinitions`).
126 */
127 optimizeDocumentNode?: boolean;
128 /**
129 * @default ""
130 * @description This config is used internally by presets, but you can use it manually to tell codegen to prefix all base types that it's using.
131 * This is useful if you wish to generate base types from `typescript-operations` plugin into a different file, and import it from there.
132 */
133 importOperationTypesFrom?: string;
134 /**
135 * @default ""
136 * @description This config should be used if `documentMode` is `external`. This has 2 usage:
137 *
138 * - any string: This would be the path to import document nodes from. This can be used if we want to manually create the document nodes e.g. Use `graphql-tag` in a separate file and export the generated document
139 * - 'near-operation-file': This is a special mode that is intended to be used with `near-operation-file` preset to import document nodes from those files. If these files are `.graphql` files, we make use of webpack loader.
140 *
141 * @exampleMarkdown
142 * ```ts filename="codegen.ts"
143 * import type { CodegenConfig } from '@graphql-codegen/cli';
144 *
145 * const config: CodegenConfig = {
146 * // ...
147 * generates: {
148 * 'path/to/file': {
149 * // plugins...
150 * config: {
151 * documentMode: 'external',
152 * importDocumentNodeExternallyFrom: 'path/to/document-node-file',
153 * },
154 * },
155 * },
156 * };
157 * export default config;
158 * ```
159 *
160 * ```ts filename="codegen.ts"
161 * import type { CodegenConfig } from '@graphql-codegen/cli';
162 *
163 * const config: CodegenConfig = {
164 * // ...
165 * generates: {
166 * 'path/to/file': {
167 * // plugins...
168 * config: {
169 * documentMode: 'external',
170 * importDocumentNodeExternallyFrom: 'near-operation-file',
171 * },
172 * },
173 * },
174 * };
175 * export default config;
176 * ```
177 *
178 */
179 importDocumentNodeExternallyFrom?: string;
180 /**
181 * @default false
182 * @description This config adds PURE magic comment to the static variables to enforce treeshaking for your bundler.
183 */
184 pureMagicComment?: boolean;
185 /**
186 * @default false
187 * @description If set to true, it will enable support for parsing variables on fragments.
188 */
189 experimentalFragmentVariables?: boolean;
190}
191export interface ClientSideBasePluginConfig extends ParsedConfig {
192 gqlImport: string;
193 documentNodeImport: string;
194 operationResultSuffix: string;
195 dedupeOperationSuffix: boolean;
196 omitOperationSuffix: boolean;
197 noExport: boolean;
198 documentVariablePrefix: string;
199 documentVariableSuffix: string;
200 fragmentVariablePrefix: string;
201 fragmentVariableSuffix: string;
202 documentMode?: DocumentMode;
203 importDocumentNodeExternallyFrom?: 'near-operation-file' | string;
204 importOperationTypesFrom?: string;
205 globalNamespace?: boolean;
206 pureMagicComment?: boolean;
207 optimizeDocumentNode: boolean;
208 experimentalFragmentVariables?: boolean;
209}
210export declare class ClientSideBaseVisitor<TRawConfig extends RawClientSideBasePluginConfig = RawClientSideBasePluginConfig, TPluginConfig extends ClientSideBasePluginConfig = ClientSideBasePluginConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
211 protected _schema: GraphQLSchema;
212 protected _fragments: LoadedFragment[];
213 protected _collectedOperations: OperationDefinitionNode[];
214 protected _documents: Types.DocumentFile[];
215 protected _additionalImports: string[];
216 protected _imports: Set<string>;
217 constructor(_schema: GraphQLSchema, _fragments: LoadedFragment[], rawConfig: TRawConfig, additionalConfig: Partial<TPluginConfig>, documents?: Types.DocumentFile[]);
218 protected _extractFragments(document: FragmentDefinitionNode | OperationDefinitionNode, withNested?: boolean): string[];
219 protected _transformFragments(document: FragmentDefinitionNode | OperationDefinitionNode): string[];
220 protected _includeFragments(fragments: string[], nodeKind: 'FragmentDefinition' | 'OperationDefinition'): string;
221 protected _prepareDocument(documentStr: string): string;
222 protected _gql(node: FragmentDefinitionNode | OperationDefinitionNode): string;
223 protected _generateFragment(fragmentDocument: FragmentDefinitionNode): string | void;
224 private get fragmentsGraph();
225 get fragments(): string;
226 protected _parseImport(importStr: string): ParsedImport;
227 protected _generateImport({ moduleName, propName }: ParsedImport, varName: string, isTypeImport: boolean): string | null;
228 private clearExtension;
229 getImports(options?: {
230 excludeFragments?: boolean;
231 }): string[];
232 protected buildOperation(_node: OperationDefinitionNode, _documentVariableName: string, _operationType: string, _operationResultType: string, _operationVariablesTypes: string, _hasRequiredVariables: boolean): string;
233 protected getDocumentNodeSignature(_resultType: string, _variablesTypes: string, _node: FragmentDefinitionNode | OperationDefinitionNode): string;
234 /**
235 * Checks if the specific operation has variables that are non-null (required), and also doesn't have default.
236 * This is useful for deciding of `variables` should be optional or not.
237 * @param node
238 */
239 protected checkVariablesRequirements(node: OperationDefinitionNode): boolean;
240 getOperationVariableName(node: OperationDefinitionNode): string;
241 OperationDefinition(node: OperationDefinitionNode): string;
242}