Skip to content

Commit

Permalink
Merge pull request #213 from SpaceK33z/fix-nested-imports
Browse files Browse the repository at this point in the history
fix(nested importing)
  • Loading branch information
Divyendu Singh committed Sep 1, 2018
2 parents eac5194 + e5cd6e5 commit c336171
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
3 changes: 3 additions & 0 deletions fixtures/import-nested/a.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# import Query.first, Query.second from "c.graphql"

type Query
4 changes: 4 additions & 0 deletions fixtures/import-nested/all.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# import Query.first, Query.second from "a.graphql"
# import Query.third from "b.graphql"

type Query
3 changes: 3 additions & 0 deletions fixtures/import-nested/b.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# import Query.third from "c.graphql"

type Query
6 changes: 6 additions & 0 deletions fixtures/import-nested/c.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
}
11 changes: 11 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ type Query {
t.is(importSchema('fixtures/import-duplicate/all.graphql'), expectedSDL)
})

test('importSchema: import nested', t => {
const expectedSDL = `\
type Query {
first: String
second: Float
third: String
}
`
t.is(importSchema('fixtures/import-nested/all.graphql'), expectedSDL)
})

test('importSchema: field types', t => {
const expectedSDL = `\
type A {
Expand Down
33 changes: 12 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
DocumentNode,
Kind,
} from 'graphql'
import { flatten, groupBy, includes, keyBy } from 'lodash'
import { flatten, groupBy, includes, keyBy, isEqual } from 'lodash'
import * as path from 'path'

import { completeDefinitionPool, ValidDefinitionNode } from './definition'
Expand Down Expand Up @@ -182,7 +182,7 @@ function collectDefinitions(
sdl: string,
filePath: string,
schemas?: { [key: string]: string },
processedFiles: Set<string> = new Set(),
processedFiles: Map<string, RawModule[]> = new Map(),
typeDefinitions: ValidDefinitionNode[][] = [],
allDefinitions: ValidDefinitionNode[][] = [],
): {
Expand All @@ -208,31 +208,22 @@ function collectDefinitions(
// Add typedefinitions to running total
typeDefinitions.push(currentTypeDefinitions)

// Mark file as processed (for circular dependency cases)
processedFiles.add(key)

// Read imports from current file
const rawModules = parseSDL(sdl)
const mergedModules: RawModule[] = []

// 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 => {
rawModules.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))
: m.from
if (!processedFiles.has(moduleFilePath)) {
isFile(filePath) && isFile(m.from)
? path.resolve(path.join(dirname, m.from))
: m.from

const processedFile = processedFiles.get(key)
if (!processedFile || !processedFile.find(rModule => isEqual(rModule, m))) {
// Mark this specific import line as processed for this file (for cicular dependency cases)
processedFiles.set(key, processedFile ? processedFile.concat(m) : [m])

collectDefinitions(
m.imports,
read(moduleFilePath, schemas),
Expand Down

0 comments on commit c336171

Please sign in to comment.