UNPKG

8.95 kBTypeScriptView Raw
1import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor';
2import { FragmentDefinitionNode, OperationDefinitionNode, GraphQLSchema } from 'graphql';
3import { Types } from '@graphql-codegen/plugin-helpers';
4import { LoadedFragment, ParsedImport } from './types';
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 * ```yaml
27 * config:
28 * gqlImport: graphql.macro#gql
29 * ```
30 *
31 * ## Gatsby
32 *
33 * ```yaml
34 * config:
35 * gqlImport: gatsby#graphql
36 * ```
37 */
38 gqlImport?: string;
39 /**
40 * @default graphql#DocumentNode
41 * @description Customize from which module will `DocumentNode` be imported from.
42 * This is useful if you want to use modules other than `graphql`, e.g. `@graphql-typed-document-node`.
43 */
44 documentNodeImport?: string;
45 /**
46 * @default false
47 * @description Set this configuration to `true` if you wish to tell codegen to generate code with no `export` identifier.
48 */
49 noExport?: boolean;
50 /**
51 * @default false
52 * @description Set this configuration to `true` if you wish to make sure to remove duplicate operation name suffix.
53 */
54 dedupeOperationSuffix?: boolean;
55 /**
56 * @default false
57 * @description Set this configuration to `true` if you wish to disable auto add suffix of operation name, like `Query`, `Mutation`, `Subscription`, `Fragment`.
58 */
59 omitOperationSuffix?: boolean;
60 /**
61 * @default ""
62 * @description Adds a suffix to generated operation result type names
63 */
64 operationResultSuffix?: string;
65 /**
66 * @default ""
67 * @description Changes the GraphQL operations variables prefix.
68 */
69 documentVariablePrefix?: string;
70 /**
71 * @default Document
72 * @description Changes the GraphQL operations variables suffix.
73 */
74 documentVariableSuffix?: string;
75 /**
76 * @default ""
77 * @description Changes the GraphQL fragments variables prefix.
78 */
79 fragmentVariablePrefix?: string;
80 /**
81 * @default FragmentDoc
82 * @description Changes the GraphQL fragments variables suffix.
83 */
84 fragmentVariableSuffix?: string;
85 /**
86 * @default graphQLTag
87 * @description Declares how DocumentNode are created:
88 *
89 * - `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
90 * - `documentNode`: document nodes will be generated as objects when we generate the templates.
91 * - `documentNodeImportFragments`: Similar to documentNode except it imports external fragments instead of embedding them.
92 * - `external`: document nodes are imported from an external file. To be used with `importDocumentNodeExternallyFrom`
93 *
94 * Note that some plugins (like `typescript-graphql-request`) also supports `string` for this parameter.
95 *
96 */
97 documentMode?: DocumentMode;
98 /**
99 * @default true
100 * @description If you are using `documentNode: documentMode | documentNodeImportFragments`, you can set this to `true` to apply document optimizations for your GraphQL document.
101 * This will remove all "loc" and "description" fields from the compiled document, and will remove all empty arrays (such as `directives`, `arguments` and `variableDefinitions`).
102 */
103 optimizeDocumentNode?: boolean;
104 /**
105 * @default ""
106 * @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.
107 * This is useful if you wish to generate base types from `typescript-operations` plugin into a different file, and import it from there.
108 */
109 importOperationTypesFrom?: string;
110 /**
111 * @default ""
112 * @description This config should be used if `documentMode` is `external`. This has 2 usage:
113 *
114 * - 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
115 * - '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.
116 *
117 * @exampleMarkdown
118 * ```yaml
119 * config:
120 * documentMode: external
121 * importDocumentNodeExternallyFrom: path/to/document-node-file
122 * ```
123 *
124 * ```yaml
125 * config:
126 * documentMode: external
127 * importDocumentNodeExternallyFrom: near-operation-file
128 * ```
129 *
130 */
131 importDocumentNodeExternallyFrom?: string;
132 /**
133 * @default false
134 * @description This config adds PURE magic comment to the static variables to enforce treeshaking for your bundler.
135 */
136 pureMagicComment?: boolean;
137 /**
138 * @default false
139 * @description If set to true, it will enable support for parsing variables on fragments.
140 */
141 experimentalFragmentVariables?: boolean;
142}
143export interface ClientSideBasePluginConfig extends ParsedConfig {
144 gqlImport: string;
145 documentNodeImport: string;
146 operationResultSuffix: string;
147 dedupeOperationSuffix: boolean;
148 omitOperationSuffix: boolean;
149 noExport: boolean;
150 documentVariablePrefix: string;
151 documentVariableSuffix: string;
152 fragmentVariablePrefix: string;
153 fragmentVariableSuffix: string;
154 documentMode?: DocumentMode;
155 importDocumentNodeExternallyFrom?: 'near-operation-file' | string;
156 importOperationTypesFrom?: string;
157 globalNamespace?: boolean;
158 pureMagicComment?: boolean;
159 optimizeDocumentNode: boolean;
160 experimentalFragmentVariables?: boolean;
161}
162export declare class ClientSideBaseVisitor<TRawConfig extends RawClientSideBasePluginConfig = RawClientSideBasePluginConfig, TPluginConfig extends ClientSideBasePluginConfig = ClientSideBasePluginConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
163 protected _schema: GraphQLSchema;
164 protected _fragments: LoadedFragment[];
165 protected _collectedOperations: OperationDefinitionNode[];
166 protected _documents: Types.DocumentFile[];
167 protected _additionalImports: string[];
168 protected _imports: Set<string>;
169 constructor(_schema: GraphQLSchema, _fragments: LoadedFragment[], rawConfig: TRawConfig, additionalConfig: Partial<TPluginConfig>, documents?: Types.DocumentFile[]);
170 protected _extractFragments(document: FragmentDefinitionNode | OperationDefinitionNode, withNested?: boolean): string[];
171 protected _transformFragments(document: FragmentDefinitionNode | OperationDefinitionNode): string[];
172 protected _includeFragments(fragments: string[], nodeKind: 'FragmentDefinition' | 'OperationDefinition'): string;
173 protected _prepareDocument(documentStr: string): string;
174 protected _gql(node: FragmentDefinitionNode | OperationDefinitionNode): string;
175 protected _generateFragment(fragmentDocument: FragmentDefinitionNode): string | void;
176 private get fragmentsGraph();
177 get fragments(): string;
178 protected _parseImport(importStr: string): ParsedImport;
179 protected _generateImport({ moduleName, propName }: ParsedImport, varName: string, isTypeImport: boolean): string | null;
180 private clearExtension;
181 getImports(options?: {
182 excludeFragments?: boolean;
183 }): string[];
184 protected buildOperation(_node: OperationDefinitionNode, _documentVariableName: string, _operationType: string, _operationResultType: string, _operationVariablesTypes: string, _hasRequiredVariables: boolean): string;
185 protected getDocumentNodeSignature(_resultType: string, _variablesTypes: string, _node: FragmentDefinitionNode | OperationDefinitionNode): string;
186 /**
187 * Checks if the specific operation has variables that are non-null (required), and also doesn't have default.
188 * This is useful for deciding of `variables` should be optional or not.
189 * @param node
190 */
191 protected checkVariablesRequirements(node: OperationDefinitionNode): boolean;
192 getOperationVariableName(node: OperationDefinitionNode): string;
193 OperationDefinition(node: OperationDefinitionNode): string;
194}