Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/presets/near-operation-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,16 @@ export const preset: Types.OutputPreset<NearOperationFileConfig> = {
const artifacts: Array<Types.GenerateOptions> = [];

for (const [filename, record] of filePathsMap.entries()) {
const fragmentNames = new Set<string>();
let fragmentImportsArr = record.fragmentImports;
const externalFragments = record.externalFragments.filter(fragment => {
if (fragmentNames.has(fragment.name)) {
return false;
}

fragmentNames.add(fragment.name);
return true;
});

if (importAllFragmentsFrom) {
fragmentImportsArr = record.fragmentImports.map<ImportDeclaration<FragmentImport>>(t => {
Expand Down Expand Up @@ -377,7 +386,7 @@ export const preset: Types.OutputPreset<NearOperationFileConfig> = {
// are exported from operations file
exportFragmentSpreadSubTypes: true,
namespacedImportName: importTypesNamespace,
externalFragments: record.externalFragments,
externalFragments,
fragmentImports: fragmentImportsArr,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,53 @@ describe('near-operation-file preset', () => {
];

describe('Issues', () => {
it('dedupes repeated external fragments when multiple documents are merged into the same output file', async () => {
const result = await executePreset({
baseOutputDir: './src/',
config: {},
presetConfig: {
cwd: '/some/deep/path',
baseTypesPath: 'types.ts',
fileName: 'types',
},
schemaAst: schemaNode,
schema: getCachedDocumentNodeFromSchema(schemaNode),
documents: [
{
location: '/some/deep/path/src/graphql/query-a.graphql',
document: parse(/* GraphQL */ `
query QueryA {
user {
...UserFields
}
}
`),
},
{
location: '/some/deep/path/src/graphql/query-b.graphql',
document: parse(/* GraphQL */ `
query QueryB {
user {
...UserFields
}
}
`),
},
{
location: '/some/deep/path/src/graphql/user-fragment.graphql',
document: fragmentAst,
},
],
plugins: [],
pluginMap: {},
});

expect(result).toHaveLength(1);
expect(result[0].filename).toBe('/some/deep/path/src/graphql/types.generated.ts');
expect(result[0].config.externalFragments).toHaveLength(1);
expect(result[0].config.externalFragments[0].name).toBe('UserFields');
});

it('#5002 - error when inline fragment does not specify the name of the type', async () => {
const testSchema = parse(/* GraphQL */ `
scalar Date
Expand Down