Skip to content

Commit

Permalink
Fix fragment imports for near-operation-file with graphQLTag
Browse files Browse the repository at this point in the history
  • Loading branch information
wassim-k committed Apr 12, 2023
1 parent 131b54f commit 51d93be
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-bottles-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Fix fragment imports for near-operation-file with graphQLTag
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
import gqlTag from 'graphql-tag';
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.js';
import { LoadedFragment, ParsedImport } from './types.js';
import { buildScalarsFromConfig, getConfigValue } from './utils.js';
import { buildScalarsFromConfig, unique, flatten, getConfigValue, groupBy } from './utils.js';
import { FragmentImport, ImportDeclaration, generateFragmentImportStatement } from './imports.js';

gqlTag.enableExperimentalFragmentVariables();

Expand Down Expand Up @@ -568,7 +569,7 @@ export class ClientSideBaseVisitor<
return path;
}

public getImports(): string[] {
public getImports(options: { excludeFragments?: boolean } = {}): string[] {
for (const i of this._additionalImports || []) {
this._imports.add(i);
}
Expand Down Expand Up @@ -621,6 +622,31 @@ export class ClientSideBaseVisitor<
break;
}

const excludeFragments =
options.excludeFragments || this.config.globalNamespace || this.config.documentMode !== DocumentMode.graphQLTag;

if (!excludeFragments) {
const deduplicatedImports = Object.values(groupBy(this.config.fragmentImports, fi => fi.importSource.path))
.map(
(fragmentImports): ImportDeclaration<FragmentImport> => ({
...fragmentImports[0],
importSource: {
...fragmentImports[0].importSource,
identifiers: unique(
flatten(fragmentImports.map(fi => fi.importSource.identifiers)),
identifier => identifier.name
),
},
emitLegacyCommonJSImports: this.config.emitLegacyCommonJSImports,
})
)
.filter(fragmentImport => fragmentImport.outputPath !== fragmentImport.importSource.path);

for (const fragmentImport of deduplicatedImports) {
this._imports.add(generateFragmentImportStatement(fragmentImport, 'document'));
}
}

return Array.from(this._imports);
}

Expand Down
16 changes: 16 additions & 0 deletions packages/plugins/other/visitor-plugin-common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,19 @@ export function isOneOfInputObjectType(

return isOneOfType;
}

export function groupBy<T>(array: Array<T>, key: (item: T) => string | number): { [key: string]: Array<T> } {
return array.reduce<{ [key: string]: Array<T> }>((acc, item) => {
const group = (acc[key(item)] ??= []);
group.push(item);
return acc;
}, {});
}

export function flatten<T>(array: Array<Array<T>>): Array<T> {
return ([] as Array<T>).concat(...array);
}

export function unique<T>(array: Array<T>, key: (item: T) => string): Array<T> {
return Object.values(array.reduce((acc, item) => ({ [key(item)]: item, ...acc }), {}));
}

0 comments on commit 51d93be

Please sign in to comment.