@nmarks/graphql-codegen-per-operation-file-preset 1.0.0 → 1.0.1

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/index.js CHANGED
@@ -5,6 +5,7 @@ const tslib_1 = require("tslib");
5
5
  const path_1 = require("path");
6
6
  const graphql_1 = require("graphql");
7
7
  const add_1 = tslib_1.__importDefault(require("@graphql-codegen/add"));
8
+ const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
8
9
  const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
9
10
  const resolve_document_imports_js_1 = require("./resolve-document-imports.js");
10
11
  const utils_js_1 = require("./utils.js");
@@ -25,7 +26,7 @@ function extractDefinitions(document) {
25
26
  }
26
27
  exports.preset = {
27
28
  buildGeneratesSection: options => {
28
- var _a;
29
+ var _a, _b;
29
30
  const schemaObject = options.schemaAst
30
31
  ? options.schemaAst
31
32
  : (0, graphql_1.buildASTSchema)(options.schema, options.config);
@@ -75,12 +76,36 @@ exports.preset = {
75
76
  ...fragmentImport,
76
77
  outputPath: filename, // Update to actual output path
77
78
  }));
79
+ // Check if THIS specific operation uses types (not the whole source file)
80
+ const singleDefDocumentWithFragments = {
81
+ ...singleDefDocument,
82
+ definitions: [
83
+ ...singleDefDocument.definitions,
84
+ ...source.externalFragments.map(fragment => fragment.node),
85
+ ],
86
+ };
87
+ const needsTypesImport = (0, plugin_helpers_1.isUsingTypes)(singleDefDocumentWithFragments, [], schemaObject);
88
+ // Generate the types import statement if needed
89
+ const importStatements = [];
90
+ if (needsTypesImport && !options.config.globalNamespace) {
91
+ const schemaTypesImportStatement = (0, visitor_plugin_common_1.generateImportStatement)({
92
+ baseDir,
93
+ emitLegacyCommonJSImports: options.config.emitLegacyCommonJSImports,
94
+ importExtension: options.config.importExtension,
95
+ importSource: (0, visitor_plugin_common_1.resolveImportSource)({
96
+ path: shouldAbsolute ? (0, path_1.join)(options.baseOutputDir, baseTypesPath) : baseTypesPath,
97
+ namespace: importTypesNamespace,
98
+ }),
99
+ baseOutputDir: options.baseOutputDir,
100
+ outputPath: filename,
101
+ typesImport: (_b = options.config.useTypeImports) !== null && _b !== void 0 ? _b : false,
102
+ });
103
+ importStatements.push(schemaTypesImportStatement);
104
+ }
78
105
  const plugins = [
79
- ...(options.config.globalNamespace
80
- ? []
81
- : source.importStatements.map(importStatement => ({
82
- add: { content: importStatement },
83
- }))),
106
+ ...importStatements.map(importStatement => ({
107
+ add: { content: importStatement },
108
+ })),
84
109
  ...options.plugins,
85
110
  ];
86
111
  const config = {
package/esm/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { join } from 'path';
2
2
  import { buildASTSchema, Kind } from 'graphql';
3
3
  import addPlugin from '@graphql-codegen/add';
4
- import { getConfigValue, } from '@graphql-codegen/visitor-plugin-common';
4
+ import { isUsingTypes } from '@graphql-codegen/plugin-helpers';
5
+ import { generateImportStatement, getConfigValue, resolveImportSource, } from '@graphql-codegen/visitor-plugin-common';
5
6
  import { resolveDocumentImports } from './resolve-document-imports.js';
6
7
  import { generateOperationFilePath } from './utils.js';
7
8
  /**
@@ -21,7 +22,7 @@ function extractDefinitions(document) {
21
22
  }
22
23
  export const preset = {
23
24
  buildGeneratesSection: options => {
24
- var _a;
25
+ var _a, _b;
25
26
  const schemaObject = options.schemaAst
26
27
  ? options.schemaAst
27
28
  : buildASTSchema(options.schema, options.config);
@@ -71,12 +72,36 @@ export const preset = {
71
72
  ...fragmentImport,
72
73
  outputPath: filename, // Update to actual output path
73
74
  }));
75
+ // Check if THIS specific operation uses types (not the whole source file)
76
+ const singleDefDocumentWithFragments = {
77
+ ...singleDefDocument,
78
+ definitions: [
79
+ ...singleDefDocument.definitions,
80
+ ...source.externalFragments.map(fragment => fragment.node),
81
+ ],
82
+ };
83
+ const needsTypesImport = isUsingTypes(singleDefDocumentWithFragments, [], schemaObject);
84
+ // Generate the types import statement if needed
85
+ const importStatements = [];
86
+ if (needsTypesImport && !options.config.globalNamespace) {
87
+ const schemaTypesImportStatement = generateImportStatement({
88
+ baseDir,
89
+ emitLegacyCommonJSImports: options.config.emitLegacyCommonJSImports,
90
+ importExtension: options.config.importExtension,
91
+ importSource: resolveImportSource({
92
+ path: shouldAbsolute ? join(options.baseOutputDir, baseTypesPath) : baseTypesPath,
93
+ namespace: importTypesNamespace,
94
+ }),
95
+ baseOutputDir: options.baseOutputDir,
96
+ outputPath: filename,
97
+ typesImport: (_b = options.config.useTypeImports) !== null && _b !== void 0 ? _b : false,
98
+ });
99
+ importStatements.push(schemaTypesImportStatement);
100
+ }
74
101
  const plugins = [
75
- ...(options.config.globalNamespace
76
- ? []
77
- : source.importStatements.map(importStatement => ({
78
- add: { content: importStatement },
79
- }))),
102
+ ...importStatements.map(importStatement => ({
103
+ add: { content: importStatement },
104
+ })),
80
105
  ...options.plugins,
81
106
  ];
82
107
  const config = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nmarks/graphql-codegen-per-operation-file-preset",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "GraphQL Code Generator preset for generating one file per operation/fragment",
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"