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 083f565..03a1d86 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 44504c8..e3a4f91 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) && isFile(m.from) ? path.resolve(path.join(dirname, m.from))