From cb71d9576fa189c87e815924a5f6c526fba68571 Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Tue, 3 Apr 2018 13:49:50 +0200 Subject: [PATCH] Fix multiple import declarations from the same path not working --- fixtures/import-duplicate/a.graphql | 6 ++++++ fixtures/import-duplicate/all.graphql | 2 ++ src/index.test.ts | 11 +++++++++++ src/index.ts | 13 ++++++++++++- 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 fixtures/import-duplicate/a.graphql create mode 100644 fixtures/import-duplicate/all.graphql diff --git a/fixtures/import-duplicate/a.graphql b/fixtures/import-duplicate/a.graphql new file mode 100644 index 0000000..e332ee7 --- /dev/null +++ b/fixtures/import-duplicate/a.graphql @@ -0,0 +1,6 @@ +type Query { + first: String + second: Float + third: String + unused: String +} \ No newline at end of file diff --git a/fixtures/import-duplicate/all.graphql b/fixtures/import-duplicate/all.graphql new file mode 100644 index 0000000..9fde323 --- /dev/null +++ b/fixtures/import-duplicate/all.graphql @@ -0,0 +1,2 @@ +# import Query.first, Query.second from "a.graphql" +# import Query.third from "a.graphql" diff --git a/src/index.test.ts b/src/index.test.ts index 96ca943..bbe217e 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -76,6 +76,17 @@ type Query { t.is(importSchema('fixtures/imports-only/all.graphql'), expectedSDL) }) +test('importSchema: import duplicate', t => { + const expectedSDL = `\ +type Query { + first: String + second: Float + third: String +} +` + t.is(importSchema('fixtures/import-duplicate/all.graphql'), expectedSDL) +}) + test('importSchema: field types', t => { const expectedSDL = `\ type A { diff --git a/src/index.ts b/src/index.ts index 6ffd03a..ec19109 100644 --- a/src/index.ts +++ b/src/index.ts @@ -193,9 +193,20 @@ function collectDefinitions( // Read imports from current file const rawModules = parseSDL(sdl) + const mergedModules: RawModule[] = [] - // Process each file (recursively) + // Merge imports from the same path rawModules.forEach(m => { + const mergedModule = mergedModules.find(mm => mm.from === m.from) + if (mergedModule) { + mergedModule.imports = mergedModule.imports.concat(m.imports) + } else { + mergedModules.push(m) + } + }) + + // Process each file (recursively) + mergedModules.forEach(m => { // If it was not yet processed (in case of circular dependencies) const moduleFilePath = isFile(filePath) ? path.resolve(path.join(dirname, m.from)) : m.from if (!processedFiles.has(moduleFilePath)) {