Skip to content

Commit

Permalink
Revert "Support for node_modules in graphql imports (#216)"
Browse files Browse the repository at this point in the history
This reverts commit 4c44f56.
  • Loading branch information
SpaceK33z committed Sep 7, 2018
1 parent 4c44f56 commit e43898c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 75 deletions.
6 changes: 0 additions & 6 deletions fixtures/import-module/a.graphql

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"devDependencies": {
"@types/graphql": "0.12.6",
"@types/lodash": "4.14.116",
"@types/resolve-from": "0.0.18",
"@types/node": "9.6.31",
"ava": "0.25.0",
"ava-ts": "0.25.1",
Expand All @@ -54,7 +53,6 @@
"typescript": "3.0.1"
},
"dependencies": {
"lodash": "^4.17.4",
"resolve-from": "^4.0.0"
"lodash": "^4.17.4"
}
}
41 changes: 0 additions & 41 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import test from 'ava'
import * as fs from 'fs'
import { parseImportLine, parseSDL, importSchema } from '.'

test('parseImportLine: parse single import', t => {
Expand Down Expand Up @@ -45,13 +44,6 @@ test('parseImportLine: different path', t => {
})
})

test('parseImportLine: module in node_modules', t => {
t.deepEqual(parseImportLine(`import A from "module-name"`), {
imports: ['A'],
from: 'module-name',
})
})

test('parseSDL: non-import comment', t => {
t.deepEqual(parseSDL(`#importent: comment`), [])
})
Expand All @@ -73,39 +65,6 @@ test('parse: multi line import', t => {
])
})

test('Module in node_modules', t => {
const b = `\
# import lower from './lower.graphql'
type B {
id: ID!
nickname: String! @lower
}
`
const lower = `\
directive @lower on FIELD_DEFINITION
`
const expectedSDL = `\
type A {
id: ID!
author: B!
}
type B {
id: ID!
nickname: String! @lower
}
directive @lower on FIELD_DEFINITION
`
const moduleDir = 'node_modules/graphql-import-test'
if (!fs.existsSync(moduleDir)) {
fs.mkdirSync(moduleDir)
}
fs.writeFileSync(moduleDir + '/b.graphql', b)
fs.writeFileSync(moduleDir + '/lower.graphql', lower)
t.is(importSchema('fixtures/import-module/a.graphql'), expectedSDL)
})

test('importSchema: imports only', t => {
const expectedSDL = `\
type Query {
Expand Down
31 changes: 6 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from 'graphql'
import { flatten, groupBy, includes, keyBy, isEqual } from 'lodash'
import * as path from 'path'
import * as resolveFrom from 'resolve-from'

import { completeDefinitionPool, ValidDefinitionNode } from './definition'

Expand Down Expand Up @@ -165,29 +164,6 @@ function isEmptySDL(sdl: string): boolean {
)
}

/**
* Resolve the path of an import.
* First it will try to find a file relative from the file the import is in, if that fails it will try to resolve it as a module so imports from packages work correctly.
*
* @param filePath Path the import was made from
* @param importFrom Path given for the import
* @returns Full resolved path to a file
*/
function resolveModuleFilePath(filePath: string, importFrom: string): string {
const dirname = path.dirname(filePath)
if (isFile(filePath) && isFile(importFrom)) {
try {
return fs.realpathSync(path.join(dirname, importFrom))
} catch (e) {
if (e.code === 'ENOENT') {
return resolveFrom(dirname, importFrom)
}
}
}

return importFrom
}

/**
* Recursively process all schema files. Keeps track of both the filtered
* type definitions, and all type definitions, because they might be needed
Expand All @@ -214,6 +190,7 @@ function collectDefinitions(
typeDefinitions: ValidDefinitionNode[][]
} {
const key = isFile(filePath) ? path.resolve(filePath) : filePath
const dirname = path.dirname(filePath)

// Get TypeDefinitionNodes from current schema
const document = getDocumentFromSDL(sdl)
Expand All @@ -237,12 +214,16 @@ function collectDefinitions(
// Process each file (recursively)
rawModules.forEach(m => {
// If it was not yet processed (in case of circular dependencies)
const moduleFilePath = resolveModuleFilePath(filePath, m.from)
const 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 e43898c

Please sign in to comment.