UNPKG

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