@nmarks/graphql-codegen-per-operation-file-preset 1.0.5 → 1.0.6

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
@@ -185,17 +185,65 @@ exports.preset = {
185
185
  document: singleDefDocument,
186
186
  location: source.documents[0].location,
187
187
  };
188
+ // Find other fragments in the same source file (they are "local" now but will be "external" after split)
189
+ const localFragments = definitions
190
+ .filter(d => d.definition.kind === graphql_1.Kind.FRAGMENT_DEFINITION && d.name !== name)
191
+ .map(d => d.definition);
192
+ // Add local fragments to external fragments list so they're available during generation
193
+ const localFragmentNodes = localFragments.map(frag => ({
194
+ level: 0,
195
+ isExternal: true,
196
+ name: frag.name.value,
197
+ onType: frag.typeCondition.name.value,
198
+ node: frag,
199
+ }));
200
+ const allExternalFragments = [
201
+ ...source.externalFragments,
202
+ ...localFragmentNodes,
203
+ ];
188
204
  // Update fragment imports to use the correct output path for this operation
189
205
  const updatedFragmentImports = source.fragmentImports.map(fragmentImport => ({
190
206
  ...fragmentImport,
191
207
  outputPath: filename, // Update to actual output path
192
208
  }));
209
+ // Generate fragment imports for local fragments (they'll be in separate files after splitting)
210
+ const localFragmentImports = localFragments.map((frag) => {
211
+ var _a;
212
+ const fragmentFilePath = (0, utils_js_1.generateOperationFilePath)(source.documents[0].location, frag.name.value, folder, extension);
213
+ // Get fragment import identifiers (document + type)
214
+ const identifiers = [
215
+ {
216
+ name: `${frag.name.value}FragmentDoc`,
217
+ kind: 'document',
218
+ },
219
+ {
220
+ name: `${frag.name.value}Fragment`,
221
+ kind: 'type',
222
+ },
223
+ ];
224
+ return {
225
+ baseDir,
226
+ baseOutputDir: options.baseOutputDir,
227
+ outputPath: filename,
228
+ importSource: {
229
+ path: fragmentFilePath,
230
+ identifiers,
231
+ },
232
+ emitLegacyCommonJSImports: options.config.emitLegacyCommonJSImports,
233
+ importExtension: options.config.importExtension,
234
+ typesImport: (_a = options.config.useTypeImports) !== null && _a !== void 0 ? _a : false,
235
+ };
236
+ });
237
+ const allFragmentImports = [
238
+ ...updatedFragmentImports,
239
+ ...localFragmentImports,
240
+ ];
193
241
  // Check if THIS specific operation uses types (not the whole source file)
194
242
  const singleDefDocumentWithFragments = {
195
243
  ...singleDefDocument,
196
244
  definitions: [
197
245
  ...singleDefDocument.definitions,
198
- ...source.externalFragments.map(fragment => fragment.node),
246
+ ...allExternalFragments.map(fragment => fragment.node),
199
247
  ],
200
248
  };
201
249
  const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject, options.config);
@@ -226,8 +274,8 @@ exports.preset = {
226
274
  ...options.config,
227
275
  exportFragmentSpreadSubTypes: true,
228
276
  namespacedImportName: importTypesNamespace,
229
- externalFragments: source.externalFragments,
230
- fragmentImports: updatedFragmentImports,
277
+ externalFragments: allExternalFragments,
278
+ fragmentImports: allFragmentImports,
231
279
  };
232
280
  artifacts.push({
233
281
  ...options,
package/esm/index.js CHANGED
@@ -181,17 +181,65 @@ export const preset = {
181
181
  document: singleDefDocument,
182
182
  location: source.documents[0].location,
183
183
  };
184
+ // Find other fragments in the same source file (they are "local" now but will be "external" after split)
185
+ const localFragments = definitions
186
+ .filter(d => d.definition.kind === Kind.FRAGMENT_DEFINITION && d.name !== name)
187
+ .map(d => d.definition);
188
+ // Add local fragments to external fragments list so they're available during generation
189
+ const localFragmentNodes = localFragments.map(frag => ({
190
+ level: 0,
191
+ isExternal: true,
192
+ name: frag.name.value,
193
+ onType: frag.typeCondition.name.value,
194
+ node: frag,
195
+ }));
196
+ const allExternalFragments = [
197
+ ...source.externalFragments,
198
+ ...localFragmentNodes,
199
+ ];
184
200
  // Update fragment imports to use the correct output path for this operation
185
201
  const updatedFragmentImports = source.fragmentImports.map(fragmentImport => ({
186
202
  ...fragmentImport,
187
203
  outputPath: filename, // Update to actual output path
188
204
  }));
205
+ // Generate fragment imports for local fragments (they'll be in separate files after splitting)
206
+ const localFragmentImports = localFragments.map((frag) => {
207
+ var _a;
208
+ const fragmentFilePath = generateOperationFilePath(source.documents[0].location, frag.name.value, folder, extension);
209
+ // Get fragment import identifiers (document + type)
210
+ const identifiers = [
211
+ {
212
+ name: `${frag.name.value}FragmentDoc`,
213
+ kind: 'document',
214
+ },
215
+ {
216
+ name: `${frag.name.value}Fragment`,
217
+ kind: 'type',
218
+ },
219
+ ];
220
+ return {
221
+ baseDir,
222
+ baseOutputDir: options.baseOutputDir,
223
+ outputPath: filename,
224
+ importSource: {
225
+ path: fragmentFilePath,
226
+ identifiers,
227
+ },
228
+ emitLegacyCommonJSImports: options.config.emitLegacyCommonJSImports,
229
+ importExtension: options.config.importExtension,
230
+ typesImport: (_a = options.config.useTypeImports) !== null && _a !== void 0 ? _a : false,
231
+ };
232
+ });
233
+ const allFragmentImports = [
234
+ ...updatedFragmentImports,
235
+ ...localFragmentImports,
236
+ ];
189
237
  // Check if THIS specific operation uses types (not the whole source file)
190
238
  const singleDefDocumentWithFragments = {
191
239
  ...singleDefDocument,
192
240
  definitions: [
193
241
  ...singleDefDocument.definitions,
194
- ...source.externalFragments.map(fragment => fragment.node),
242
+ ...allExternalFragments.map(fragment => fragment.node),
195
243
  ],
196
244
  };
197
245
  const needsTypesImport = needsSchemaTypesImport(singleDefDocumentWithFragments, schemaObject, options.config);
@@ -222,8 +270,8 @@ export const preset = {
222
270
  ...options.config,
223
271
  exportFragmentSpreadSubTypes: true,
224
272
  namespacedImportName: importTypesNamespace,
225
- externalFragments: source.externalFragments,
226
- fragmentImports: updatedFragmentImports,
273
+ externalFragments: allExternalFragments,
274
+ fragmentImports: allFragmentImports,
227
275
  };
228
276
  artifacts.push({
229
277
  ...options,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nmarks/graphql-codegen-per-operation-file-preset",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
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"