@graphql-codegen/typed-document-node 6.0.2 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/visitor.js CHANGED
@@ -59,13 +59,16 @@ class TypeScriptDocumentNodesVisitor extends visitor_plugin_common_1.ClientSideB
59
59
  };
60
60
  }
61
61
  getDocumentNodeSignature(resultType, variablesTypes, node) {
62
+ const shouldUseImportPrefix = !!this.config.importOperationTypesFrom;
63
+ const resultImportPrefix = shouldUseImportPrefix && resultType !== 'unknown' ? 'Types.' : '';
64
+ const variablesImportPrefix = shouldUseImportPrefix && variablesTypes !== 'unknown' ? 'Types.' : '';
62
65
  if (this.config.documentMode === visitor_plugin_common_1.DocumentMode.documentNode ||
63
66
  this.config.documentMode === visitor_plugin_common_1.DocumentMode.documentNodeImportFragments ||
64
67
  this.config.documentMode === visitor_plugin_common_1.DocumentMode.graphQLTag) {
65
- return ` as unknown as DocumentNode<${resultType}, ${variablesTypes}>`;
68
+ return ` as unknown as DocumentNode<${resultImportPrefix}${resultType}, ${variablesImportPrefix}${variablesTypes}>`;
66
69
  }
67
70
  if (this.config.documentMode === visitor_plugin_common_1.DocumentMode.string) {
68
- return ` as unknown as TypedDocumentString<${resultType}, ${variablesTypes}>`;
71
+ return ` as unknown as TypedDocumentString<${resultImportPrefix}${resultType}, ${variablesImportPrefix}${variablesTypes}>`;
69
72
  }
70
73
  return super.getDocumentNodeSignature(resultType, variablesTypes, node);
71
74
  }
package/esm/visitor.js CHANGED
@@ -55,13 +55,16 @@ export class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor {
55
55
  };
56
56
  }
57
57
  getDocumentNodeSignature(resultType, variablesTypes, node) {
58
+ const shouldUseImportPrefix = !!this.config.importOperationTypesFrom;
59
+ const resultImportPrefix = shouldUseImportPrefix && resultType !== 'unknown' ? 'Types.' : '';
60
+ const variablesImportPrefix = shouldUseImportPrefix && variablesTypes !== 'unknown' ? 'Types.' : '';
58
61
  if (this.config.documentMode === DocumentMode.documentNode ||
59
62
  this.config.documentMode === DocumentMode.documentNodeImportFragments ||
60
63
  this.config.documentMode === DocumentMode.graphQLTag) {
61
- return ` as unknown as DocumentNode<${resultType}, ${variablesTypes}>`;
64
+ return ` as unknown as DocumentNode<${resultImportPrefix}${resultType}, ${variablesImportPrefix}${variablesTypes}>`;
62
65
  }
63
66
  if (this.config.documentMode === DocumentMode.string) {
64
- return ` as unknown as TypedDocumentString<${resultType}, ${variablesTypes}>`;
67
+ return ` as unknown as TypedDocumentString<${resultImportPrefix}${resultType}, ${variablesImportPrefix}${variablesTypes}>`;
65
68
  }
66
69
  return super.getDocumentNodeSignature(resultType, variablesTypes, node);
67
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/typed-document-node",
3
- "version": "6.0.2",
3
+ "version": "6.1.0",
4
4
  "description": "GraphQL Code Generator plugin for generating ready-to-use TypedDocumentNode based on GraphQL operations",
5
5
  "peerDependencies": {
6
6
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
@@ -46,4 +46,30 @@ export interface TypeScriptTypedDocumentNodesConfig extends RawClientSideBasePlu
46
46
  * ```
47
47
  */
48
48
  addTypenameToSelectionSets?: boolean;
49
+ /**
50
+ * @description Allows you to import the operation types from a different file.
51
+ * @default ""
52
+ *
53
+ * @exampleMarkdown
54
+ * ```ts filename="codegen.ts"
55
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
56
+ *
57
+ * const config: CodegenConfig = {
58
+ * // ...
59
+ * generates: {
60
+ * 'path/to/file.ts': {
61
+ * plugins: ['typescript', 'typescript-operations']
62
+ * },
63
+ * 'path/to/file2.ts': {
64
+ * plugins: ['typed-document-node'],
65
+ * config: {
66
+ * importOperationTypesFrom: 'path/to/file.ts'
67
+ * },
68
+ * },
69
+ * },
70
+ * };
71
+ * export default config;
72
+ * ```
73
+ */
74
+ importOperationTypesFrom?: string;
49
75
  }
@@ -46,4 +46,30 @@ export interface TypeScriptTypedDocumentNodesConfig extends RawClientSideBasePlu
46
46
  * ```
47
47
  */
48
48
  addTypenameToSelectionSets?: boolean;
49
+ /**
50
+ * @description Allows you to import the operation types from a different file.
51
+ * @default ""
52
+ *
53
+ * @exampleMarkdown
54
+ * ```ts filename="codegen.ts"
55
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
56
+ *
57
+ * const config: CodegenConfig = {
58
+ * // ...
59
+ * generates: {
60
+ * 'path/to/file.ts': {
61
+ * plugins: ['typescript', 'typescript-operations']
62
+ * },
63
+ * 'path/to/file2.ts': {
64
+ * plugins: ['typed-document-node'],
65
+ * config: {
66
+ * importOperationTypesFrom: 'path/to/file.ts'
67
+ * },
68
+ * },
69
+ * },
70
+ * };
71
+ * export default config;
72
+ * ```
73
+ */
74
+ importOperationTypesFrom?: string;
49
75
  }