Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loader): Use relative path inside SDL #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions README.md
Expand Up @@ -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!
Expand All @@ -30,7 +43,7 @@ type Complex {
```

```js
import typeDefs from './schema.graphql'
import typeDefs from './schema/main.graphql'
```

```js
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Expand Up @@ -10,5 +10,6 @@ module.exports = {
"js",
"ts",
"node"
]
],
"testEnvironment": "node"
}
5 changes: 4 additions & 1 deletion src/index.ts
Expand Up @@ -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, '\\`')}\``)
}
6 changes: 3 additions & 3 deletions 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!
Expand Down