From 23a73339c188f491e58215e2a8188ff0ea409688 Mon Sep 17 00:00:00 2001 From: Mathias Vestergaard Date: Tue, 9 Jul 2019 23:54:16 +0200 Subject: [PATCH] Guard against duplicate fragment names in near-operations-file --- .../presets/near-operation-file/src/index.ts | 9 +++++++++ .../tests/near-operation-file.spec.ts | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/packages/presets/near-operation-file/src/index.ts b/packages/presets/near-operation-file/src/index.ts index 96a9496736a..735869802ed 100644 --- a/packages/presets/near-operation-file/src/index.ts +++ b/packages/presets/near-operation-file/src/index.ts @@ -102,6 +102,7 @@ export const preset: Types.OutputPreset = { add: addPlugin, }; + const duplicateFragmentNames: string[] = []; const fragmentNameToFile: FragmentNameToFile = options.documents.reduce((prev, documentRecord) => { const fragments: FragmentDefinitionNode[] = documentRecord.content.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION) as FragmentDefinitionNode[]; @@ -110,6 +111,10 @@ export const preset: Types.OutputPreset = { const filePath = appendExtensionToFilePath(documentRecord.filePath, extension); const importName = baseVisitor.convertName(fragment, { suffix: 'Fragment' }); + if (prev[fragment.name.value]) { + duplicateFragmentNames.push(fragment.name.value); + } + prev[fragment.name.value] = { filePath, importName, onType: fragment.typeCondition.name.value, node: fragment }; } } @@ -117,6 +122,10 @@ export const preset: Types.OutputPreset = { return prev; }, {}); + if (duplicateFragmentNames.length) { + throw new Error(`Multiple fragments with the name(s) "${duplicateFragmentNames.join(', ')}" were found.`); + } + const absTypesPath = resolve(baseDir, join(options.baseOutputDir, options.presetConfig.baseTypesPath)); return options.documents diff --git a/packages/presets/near-operation-file/tests/near-operation-file.spec.ts b/packages/presets/near-operation-file/tests/near-operation-file.spec.ts index b005039734b..23b171944d7 100644 --- a/packages/presets/near-operation-file/tests/near-operation-file.spec.ts +++ b/packages/presets/near-operation-file/tests/near-operation-file.spec.ts @@ -155,6 +155,23 @@ describe('near-operation-file preset', () => { expect(result.map(o => o.plugins)[0]).toEqual(expect.arrayContaining([{ add: `import * as Types from '../types';\n` }])); }); + it('should fail when multiple fragments with the same name are found', () => { + expect(() => + preset.buildGeneratesSection({ + baseOutputDir: './src/', + config: {}, + presetConfig: { + cwd: '/some/deep/path', + baseTypesPath: 'types.ts', + }, + schema: schemaDocumentNode, + documents: [testDocuments[1], testDocuments[1]], + plugins: [{ typescript: {} }], + pluginMap: { typescript: {} as any }, + }) + ).toThrow('Multiple fragments with the name(s) "UserFields" were found.'); + }); + it('Should NOT prepend the "add" plugin with Types import when selection set does not include direct fields', async () => { const result = await preset.buildGeneratesSection({ baseOutputDir: './src/',