Skip to content

Commit 757287b

Browse files
authoredApr 26, 2018
fix: multiple import declarations from the same path not working
2 parents 61a39f5 + cb71d95 commit 757287b

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed
 

‎fixtures/import-duplicate/a.graphql

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type Query {
2+
first: String
3+
second: Float
4+
third: String
5+
unused: String
6+
}

‎fixtures/import-duplicate/all.graphql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# import Query.first, Query.second from "a.graphql"
2+
# import Query.third from "a.graphql"

‎src/index.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ type Query {
7676
t.is(importSchema('fixtures/imports-only/all.graphql'), expectedSDL)
7777
})
7878

79+
test('importSchema: import duplicate', t => {
80+
const expectedSDL = `\
81+
type Query {
82+
first: String
83+
second: Float
84+
third: String
85+
}
86+
`
87+
t.is(importSchema('fixtures/import-duplicate/all.graphql'), expectedSDL)
88+
})
89+
7990
test('importSchema: field types', t => {
8091
const expectedSDL = `\
8192
type A {

‎src/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,20 @@ function collectDefinitions(
193193

194194
// Read imports from current file
195195
const rawModules = parseSDL(sdl)
196+
const mergedModules: RawModule[] = []
196197

197-
// Process each file (recursively)
198+
// Merge imports from the same path
198199
rawModules.forEach(m => {
200+
const mergedModule = mergedModules.find(mm => mm.from === m.from)
201+
if (mergedModule) {
202+
mergedModule.imports = mergedModule.imports.concat(m.imports)
203+
} else {
204+
mergedModules.push(m)
205+
}
206+
})
207+
208+
// Process each file (recursively)
209+
mergedModules.forEach(m => {
199210
// If it was not yet processed (in case of circular dependencies)
200211
const moduleFilePath = isFile(filePath) && isFile(m.from)
201212
? path.resolve(path.join(dirname, m.from))

0 commit comments

Comments
 (0)
Please sign in to comment.