Skip to content

Commit

Permalink
fix: multiple import declarations from the same path not working
Browse files Browse the repository at this point in the history
  • Loading branch information
schickling committed Apr 26, 2018
2 parents 61a39f5 + cb71d95 commit 757287b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions fixtures/import-duplicate/a.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Query {
first: String
second: Float
third: String
unused: String
}
2 changes: 2 additions & 0 deletions fixtures/import-duplicate/all.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# import Query.first, Query.second from "a.graphql"
# import Query.third from "a.graphql"
11 changes: 11 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 757287b

Please sign in to comment.