UNPKG

7.59 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 gql#graphql-tag
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 documentMode?: DocumentMode;
92 /**
93 * @default ""
94 * @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.
95 * This is useful if you wish to generate base types from `typescript-operations` plugin into a different file, and import it from there.
96 */
97 importOperationTypesFrom?: string;
98 /**
99 * @default ""
100 * @description This config should be used if `documentMode` is `external`. This has 2 usage:
101 * - 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
102 * - '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.
103 *
104 * @exampleMarkdown
105 * ```yml
106 * config:
107 * documentMode: external
108 * importDocumentNodeExternallyFrom: path/to/document-node-file
109 * ```
110 *
111 * ```yml
112 * config:
113 * documentMode: external
114 * importDocumentNodeExternallyFrom: near-operation-file
115 * ```
116 *
117 */
118 importDocumentNodeExternallyFrom?: string;
119 /**
120 * @default false
121 * @description This config adds PURE magic comment to the static variables to enforce treeshaking for your bundler.
122 */
123 pureMagicComment?: boolean;
124}
125export interface ClientSideBasePluginConfig extends ParsedConfig {
126 gqlImport: string;
127 documentNodeImport: string;
128 operationResultSuffix: string;
129 dedupeOperationSuffix: boolean;
130 omitOperationSuffix: boolean;
131 noExport: boolean;
132 documentVariablePrefix: string;
133 documentVariableSuffix: string;
134 fragmentVariablePrefix: string;
135 fragmentVariableSuffix: string;
136 documentMode?: DocumentMode;
137 importDocumentNodeExternallyFrom?: 'near-operation-file' | string;
138 importOperationTypesFrom?: string;
139 globalNamespace?: boolean;
140 pureMagicComment?: boolean;
141}
142export declare class ClientSideBaseVisitor<TRawConfig extends RawClientSideBasePluginConfig = RawClientSideBasePluginConfig, TPluginConfig extends ClientSideBasePluginConfig = ClientSideBasePluginConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
143 protected _schema: GraphQLSchema;
144 protected _fragments: LoadedFragment[];
145 protected _collectedOperations: OperationDefinitionNode[];
146 protected _documents: Types.DocumentFile[];
147 protected _additionalImports: string[];
148 constructor(_schema: GraphQLSchema, _fragments: LoadedFragment[], rawConfig: TRawConfig, additionalConfig: Partial<TPluginConfig>, documents?: Types.DocumentFile[]);
149 protected _extractFragments(document: FragmentDefinitionNode | OperationDefinitionNode, withNested?: boolean): string[];
150 protected _transformFragments(document: FragmentDefinitionNode | OperationDefinitionNode): string[];
151 protected _includeFragments(fragments: string[]): string;
152 protected _prepareDocument(documentStr: string): string;
153 protected _gql(node: FragmentDefinitionNode | OperationDefinitionNode): string;
154 protected _generateFragment(fragmentDocument: FragmentDefinitionNode): string | void;
155 private get fragmentsGraph();
156 get fragments(): string;
157 protected _parseImport(importStr: string): ParsedImport;
158 protected _generateImport({ moduleName, propName }: ParsedImport, varName: string, isTypeImport: boolean): string | null;
159 private clearExtension;
160 getImports(options?: {
161 excludeFragments?: boolean;
162 }): string[];
163 protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: string, operationResultType: string, operationVariablesTypes: string): string;
164 protected getDocumentNodeSignature(resultType: string, variablesTypes: string, node: FragmentDefinitionNode | OperationDefinitionNode): string;
165 OperationDefinition(node: OperationDefinitionNode): string;
166}