diff --git a/README.md b/README.md index 48f7d67..b727778 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,25 @@ yarn add --dev graphql-import-loader ## Usage -Resolve GraphQL file import statements as a string. See the tests for more details +Resolve GraphQL file import statements with relative paths. See the tests for more details + +``` +. +├── src +│   ├── index.js +│   └── schema +│   ├── a.graphql +│   ├── b.graphql +│   ├── main.graphql +│   └── subdir +│   └── cd.graphql +└── webpack.config.js +``` ```graphql -# import { A } from 'src/schema/a.graphql' -# import { B } from 'src/schema/b.graphql' -# import { C, D } from 'src/schema/cd.graphql' +# import { A } from './a.graphql' +# import { B } from './b.graphql' +# import { C, D } from './subdir/cd.graphql' type Complex { id: ID! @@ -30,7 +43,7 @@ type Complex { ``` ```js -import typeDefs from './schema.graphql' +import typeDefs from './schema/main.graphql' ``` ```js diff --git a/jest.config.js b/jest.config.js index 714750d..75f626a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,5 +10,6 @@ module.exports = { "js", "ts", "node" - ] + ], + "testEnvironment": "node" } diff --git a/src/index.ts b/src/index.ts index 3b595f6..edcc72d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,11 @@ import { importSchema } from 'graphql-import' export default function(source) { const callback = this.async(); + const isFileRegex = /\.graphql$/i this.cacheable() - callback(null, `module.exports = \`${importSchema(source).replace(/`/g, '\\`')}\``) + const sdl = importSchema(isFileRegex.test(this.resource) ? this.resource : source) + + callback(null, `module.exports = \`${sdl.replace(/`/g, '\\`')}\``) } diff --git a/test/fixtures/complex.graphql b/test/fixtures/complex.graphql index d85677a..75403f2 100644 --- a/test/fixtures/complex.graphql +++ b/test/fixtures/complex.graphql @@ -1,6 +1,6 @@ -# import { A } from 'test/fixtures/a.graphql' -# import { B } from 'test/fixtures/b.graphql' -# import { C D } from 'test/fixtures/cd.graphql' +# import { A } from './a.graphql' +# import { B } from './b.graphql' +# import { C D } from './cd.graphql' type Complex { id: ID!