diff --git a/README.md b/README.md index b907d38..fc640ad 100644 --- a/README.md +++ b/README.md @@ -11,19 +11,47 @@ Webpack loader for [`graphql-import`](https://github.com/graphcool/graphql-impor yarn add --dev graphql-import-loader ``` -Add the following to the `rules` section in your `webpack.config.js` +## Usage + +Resolve GraphQL file import statements as a string. See the tests for more details + +```graphql +# import { A } from 'src/schema/a.graphql' +# import { B } from 'src/schema/b.graphql' +# import { C D } from 'src/schema/cd.graphql' + +type Complex { + id: ID! + a: A! + b: B! + c: C! + d: D! +} +``` ```js - rules: [{ - exclude: /node_modules/, - test: /\.graphql$/, - use: [{ loader: 'graphql-import-loader' }] - }], +import typeDefs from './schema.graphql' ``` -## Usage +```js +// webpack.config.js -You can now `require` or `import` `.graphql` files (resolved as strings) in your application: +module.exports = { + module: { + rules: [ + { + exclude: /node_modules/, + test: /\.graphql$/, + use: [{ loader: 'graphql-import-loader' }] + } + ] + } +} +``` + +## Examples + +Simple Server: ```ts import { GraphQLServer } from 'graphql-yoga' @@ -32,4 +60,9 @@ import typeDefs from './schema.graphql' const server = new GraphQLServer({ typeDefs, resolvers }) server.start(() => console.log('Server running on :4000')) -``` \ No newline at end of file +``` + + +Advanced: + +[serverless-prisma](https://github.com/jgeschwendt/serverless-prisma): Serverless starter kit using Prisma (early-stages) diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..714750d --- /dev/null +++ b/jest.config.js @@ -0,0 +1,14 @@ +module.exports = { + "roots": [ + "" + ], + "transform": { + "^.+\\.ts?$": "ts-jest" + }, + "testRegex": "test/.*\\.test\\.ts$", + "moduleFileExtensions": [ + "js", + "ts", + "node" + ] +} diff --git a/package.json b/package.json index d19969e..7cf8685 100644 --- a/package.json +++ b/package.json @@ -25,12 +25,20 @@ "scripts": { "prepare": "npm run build", "build": "rm -rf dist && tsc -d", - "test": "npm run build" - }, - "devDependencies": { - "typescript": "2.7.2" + "pretest": "npm run build", + "test": "jest" }, "dependencies": { "graphql-import": "^0.4.5" + }, + "devDependencies": { + "@types/jest": "^22.1.4", + "@types/node": "^9.4.6", + "ts-jest": "^22.4.1", + "graphql": "^0.13.1", + "jest": "^22.4.2", + "memory-fs": "^0.4.1", + "typescript": "^2.7.2", + "webpack": "^4.0.1" } } diff --git a/src/index.ts b/src/index.ts index b14265f..1772008 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,9 @@ import { importSchema } from 'graphql-import' -export default source => { - this.value = source - return `module.exports = \`${importSchema(source)}\`` +export default function(source) { + const callback = this.async(); + + this.cacheable() + + callback(null, `module.exports = \`${importSchema(source)}\``) } diff --git a/test/compiler.ts b/test/compiler.ts new file mode 100644 index 0000000..5282887 --- /dev/null +++ b/test/compiler.ts @@ -0,0 +1,34 @@ +import * as memoryfs from 'memory-fs' +import * as path from 'path' +import * as webpack from 'webpack' +import { Stats } from 'webpack' + +export default (fixture, options = {}) => { + const compiler = webpack({ + mode: 'development', + context: path.resolve(__dirname), + entry: `./fixtures/${fixture}`, + output: { + path: path.resolve(__dirname), + filename: 'bundle.js', + }, + module: { + rules: [{ + test: /\.graphql$/, + use: [ + { loader: path.resolve(__dirname, '..', 'dist/src') } + ] + }] + } + }) + + compiler.outputFileSystem = new memoryfs() + + return new Promise((resolve, reject) => { + compiler.run((err, stats) => { + if (err) reject(err) + + resolve(stats) + }) + }) +} diff --git a/test/fixtures/a.graphql b/test/fixtures/a.graphql new file mode 100644 index 0000000..e763e50 --- /dev/null +++ b/test/fixtures/a.graphql @@ -0,0 +1,4 @@ +type A { + id: ID! + valueA: String +} diff --git a/test/fixtures/b.graphql b/test/fixtures/b.graphql new file mode 100644 index 0000000..e541134 --- /dev/null +++ b/test/fixtures/b.graphql @@ -0,0 +1,4 @@ +type B { + id: ID! + valueB: String +} diff --git a/test/fixtures/cd.graphql b/test/fixtures/cd.graphql new file mode 100644 index 0000000..f922bef --- /dev/null +++ b/test/fixtures/cd.graphql @@ -0,0 +1,9 @@ +type C { + id: ID! + valueC: String +} + +type D { + id: ID! + valueD: String +} diff --git a/test/fixtures/complex.graphql b/test/fixtures/complex.graphql new file mode 100644 index 0000000..d85677a --- /dev/null +++ b/test/fixtures/complex.graphql @@ -0,0 +1,11 @@ +# import { A } from 'test/fixtures/a.graphql' +# import { B } from 'test/fixtures/b.graphql' +# import { C D } from 'test/fixtures/cd.graphql' + +type Complex { + id: ID! + a: A! + b: B! + c: C! + d: D! +} diff --git a/test/fixtures/simple.graphql b/test/fixtures/simple.graphql new file mode 100644 index 0000000..dc2ba62 --- /dev/null +++ b/test/fixtures/simple.graphql @@ -0,0 +1,4 @@ +type Simple { + id: ID! + value: String +} diff --git a/test/loader.test.ts b/test/loader.test.ts new file mode 100644 index 0000000..9e776d5 --- /dev/null +++ b/test/loader.test.ts @@ -0,0 +1,49 @@ +import compiler from './compiler' + +const simple = `module.exports = \`type Simple { + id: ID! + value: String +} +\`` + +const complex = `module.exports = \`type Complex { + id: ID! + a: A! + b: B! + c: C! + d: D! +} + +type A { + id: ID! + valueA: String +} + +type B { + id: ID! + valueB: String +} + +type C { + id: ID! + valueC: String +} + +type D { + id: ID! + valueD: String +} +\`` + +const fixtures = { + 'simple.graphql': simple, + 'complex.graphql': complex, +} + +Object.keys(fixtures).forEach(fixture => { + test(`Fixture: ${fixture}`, async () => { + const stats = await compiler(fixture) + const output = stats.toJson().modules[0].source + expect(output).toBe(fixtures[fixture]) + }) +}) diff --git a/tsconfig.json b/tsconfig.json index edeb74c..ec40e96 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,5 +10,9 @@ "outDir": "dist", "lib": ["es2015"] }, - "exclude": ["node_modules"] + "exclude": [ + "dist", + "node_modules", + "test" + ] } diff --git a/yarn.lock b/yarn.lock deleted file mode 100644 index 3a2f75a..0000000 --- a/yarn.lock +++ /dev/null @@ -1,17 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - -graphql-import@^0.4.5: - version "0.4.5" - resolved "https://registry.yarnpkg.com/graphql-import/-/graphql-import-0.4.5.tgz#e2f18c28d335733f46df8e0733d8deb1c6e2a645" - dependencies: - lodash "^4.17.4" - -lodash@^4.17.4: - version "4.17.5" - resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" - -typescript@2.7.2: - version "2.7.2" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.7.2.tgz#2d615a1ef4aee4f574425cdff7026edf81919836"