Skip to content

Commit 844c374

Browse files
authored
feat: change loader to be async and add tests
change loader to be async and add tests
2 parents 17d53d2 + ab0462e commit 844c374

File tree

13 files changed

+194
-34
lines changed

13 files changed

+194
-34
lines changed

README.md

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,47 @@ Webpack loader for [`graphql-import`](https://github.com/graphcool/graphql-impor
1111
yarn add --dev graphql-import-loader
1212
```
1313

14-
Add the following to the `rules` section in your `webpack.config.js`
14+
## Usage
15+
16+
Resolve GraphQL file import statements as a string. See the tests for more details
17+
18+
```graphql
19+
# import { A } from 'src/schema/a.graphql'
20+
# import { B } from 'src/schema/b.graphql'
21+
# import { C D } from 'src/schema/cd.graphql'
22+
23+
type Complex {
24+
id: ID!
25+
a: A!
26+
b: B!
27+
c: C!
28+
d: D!
29+
}
30+
```
1531

1632
```js
17-
rules: [{
18-
exclude: /node_modules/,
19-
test: /\.graphql$/,
20-
use: [{ loader: 'graphql-import-loader' }]
21-
}],
33+
import typeDefs from './schema.graphql'
2234
```
2335

24-
## Usage
36+
```js
37+
// webpack.config.js
2538

26-
You can now `require` or `import` `.graphql` files (resolved as strings) in your application:
39+
module.exports = {
40+
module: {
41+
rules: [
42+
{
43+
exclude: /node_modules/,
44+
test: /\.graphql$/,
45+
use: [{ loader: 'graphql-import-loader' }]
46+
}
47+
]
48+
}
49+
}
50+
```
51+
52+
## Examples
53+
54+
Simple Server:
2755

2856
```ts
2957
import { GraphQLServer } from 'graphql-yoga'
@@ -32,4 +60,9 @@ import typeDefs from './schema.graphql'
3260

3361
const server = new GraphQLServer({ typeDefs, resolvers })
3462
server.start(() => console.log('Server running on :4000'))
35-
```
63+
```
64+
65+
66+
Advanced:
67+
68+
[serverless-prisma](https://github.com/jgeschwendt/serverless-prisma): Serverless starter kit using Prisma (early-stages)

jest.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
"roots": [
3+
"<rootDir>"
4+
],
5+
"transform": {
6+
"^.+\\.ts?$": "ts-jest"
7+
},
8+
"testRegex": "test/.*\\.test\\.ts$",
9+
"moduleFileExtensions": [
10+
"js",
11+
"ts",
12+
"node"
13+
]
14+
}

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@
2525
"scripts": {
2626
"prepare": "npm run build",
2727
"build": "rm -rf dist && tsc -d",
28-
"test": "npm run build"
29-
},
30-
"devDependencies": {
31-
"typescript": "2.7.2"
28+
"pretest": "npm run build",
29+
"test": "jest"
3230
},
3331
"dependencies": {
3432
"graphql-import": "^0.4.5"
33+
},
34+
"devDependencies": {
35+
"@types/jest": "^22.1.4",
36+
"@types/node": "^9.4.6",
37+
"ts-jest": "^22.4.1",
38+
"graphql": "^0.13.1",
39+
"jest": "^22.4.2",
40+
"memory-fs": "^0.4.1",
41+
"typescript": "^2.7.2",
42+
"webpack": "^4.0.1"
3543
}
3644
}

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { importSchema } from 'graphql-import'
22

3-
export default source => {
4-
this.value = source
5-
return `module.exports = \`${importSchema(source)}\``
3+
export default function(source) {
4+
const callback = this.async();
5+
6+
this.cacheable()
7+
8+
callback(null, `module.exports = \`${importSchema(source)}\``)
69
}

test/compiler.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as memoryfs from 'memory-fs'
2+
import * as path from 'path'
3+
import * as webpack from 'webpack'
4+
import { Stats } from 'webpack'
5+
6+
export default (fixture, options = {}) => {
7+
const compiler = webpack({
8+
mode: 'development',
9+
context: path.resolve(__dirname),
10+
entry: `./fixtures/${fixture}`,
11+
output: {
12+
path: path.resolve(__dirname),
13+
filename: 'bundle.js',
14+
},
15+
module: {
16+
rules: [{
17+
test: /\.graphql$/,
18+
use: [
19+
{ loader: path.resolve(__dirname, '..', 'dist/src') }
20+
]
21+
}]
22+
}
23+
})
24+
25+
compiler.outputFileSystem = new memoryfs()
26+
27+
return new Promise<Stats>((resolve, reject) => {
28+
compiler.run((err, stats) => {
29+
if (err) reject(err)
30+
31+
resolve(stats)
32+
})
33+
})
34+
}

test/fixtures/a.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type A {
2+
id: ID!
3+
valueA: String
4+
}

test/fixtures/b.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type B {
2+
id: ID!
3+
valueB: String
4+
}

test/fixtures/cd.graphql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type C {
2+
id: ID!
3+
valueC: String
4+
}
5+
6+
type D {
7+
id: ID!
8+
valueD: String
9+
}

test/fixtures/complex.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# import { A } from 'test/fixtures/a.graphql'
2+
# import { B } from 'test/fixtures/b.graphql'
3+
# import { C D } from 'test/fixtures/cd.graphql'
4+
5+
type Complex {
6+
id: ID!
7+
a: A!
8+
b: B!
9+
c: C!
10+
d: D!
11+
}

test/fixtures/simple.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type Simple {
2+
id: ID!
3+
value: String
4+
}

0 commit comments

Comments
 (0)