From 721bb9e62ebe9dbfebd469d7e6c0b468446f1ab5 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Thu, 22 Aug 2019 09:23:48 +0800 Subject: [PATCH 1/4] finish the basic implementation --- packages/@jsonql/koa/Dockerfile | 0 packages/@jsonql/koa/README.md | 45 ++++++++++++++++++++++++ packages/@jsonql/koa/config/default.json | 4 +++ packages/@jsonql/koa/docker-compose.yml | 0 packages/@jsonql/koa/index.js | 19 ++++++++++ packages/@jsonql/koa/package.json | 26 ++++++++++++++ packages/@jsonql/koa/tests/main.test.js | 2 ++ 7 files changed, 96 insertions(+) create mode 100644 packages/@jsonql/koa/Dockerfile create mode 100644 packages/@jsonql/koa/README.md create mode 100644 packages/@jsonql/koa/config/default.json create mode 100644 packages/@jsonql/koa/docker-compose.yml create mode 100644 packages/@jsonql/koa/index.js create mode 100644 packages/@jsonql/koa/package.json create mode 100644 packages/@jsonql/koa/tests/main.test.js diff --git a/packages/@jsonql/koa/Dockerfile b/packages/@jsonql/koa/Dockerfile new file mode 100644 index 00000000..e69de29b diff --git a/packages/@jsonql/koa/README.md b/packages/@jsonql/koa/README.md new file mode 100644 index 00000000..83fe3d92 --- /dev/null +++ b/packages/@jsonql/koa/README.md @@ -0,0 +1,45 @@ +# @jsonql/koa + +> The complete jsonql api server with Koa + +It comes with `koa-cors`, `koa-bodyparser`. + +# Installation + +```sh +$ npm i @jsonql/koa +``` + +# How to + +Just import and setup the config + +```js +const jsonqlKoa = require('@jsonql/koa') +const { join } = require('path') + +const { app, server, stop } = jsonqlKoa({ + resolverDir: join(__dirname, 'resolvers'), // default + contractDir: join(__dirname, 'contract') // default +}) +``` + +By default it will start on the port `8001` if you want to change it, +pass `port` to the config. + +```js +const jsonqlKoa = require('@jsonql/koa') +const { app, server, stop } = jsonqlKoa({ + port: 7890 +}) +``` + +As you can see from the example, it return the following object + +- app - the Koa app instance +- server - the underlying `http.server` +- stop - a function to call and close the server + +--- + +Joel Chu (c) 2019 diff --git a/packages/@jsonql/koa/config/default.json b/packages/@jsonql/koa/config/default.json new file mode 100644 index 00000000..8ca2dd8e --- /dev/null +++ b/packages/@jsonql/koa/config/default.json @@ -0,0 +1,4 @@ +{ + "name": "jsonql-koa-stock-setup", + "port": 8001 +} diff --git a/packages/@jsonql/koa/docker-compose.yml b/packages/@jsonql/koa/docker-compose.yml new file mode 100644 index 00000000..e69de29b diff --git a/packages/@jsonql/koa/index.js b/packages/@jsonql/koa/index.js new file mode 100644 index 00000000..b4063bc8 --- /dev/null +++ b/packages/@jsonql/koa/index.js @@ -0,0 +1,19 @@ +// the main file +const debug = require('debug')('@jsonql_koa:main') +const Koa = require('koa') +const cors = require('koa-cors') +const bodyparser = require('koa-bodyparser') +const jsonql = require('jsonql-koa') + +/** + * @param {object} [config={}] configuration + * @return {object} app, server, stop + */ +module.exports = function(config = {}) { + + const app = new Koa() + app.use(cors()) + app.use(bodyparser()) + app.use(jsonql(config)) + +} diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json new file mode 100644 index 00000000..eb914c24 --- /dev/null +++ b/packages/@jsonql/koa/package.json @@ -0,0 +1,26 @@ +{ + "name": "@jsonql/koa", + "version": "0.0.1", + "description": "Complete jsonql Koa setup + extra in later release", + "main": "index.js", + "scripts": { + "test": "DEBUG=@jsonql_koa* ava --verbose" + }, + "keywords": [ + "jsonql", + "koa", + "server", + "socket", + "WebSocket", + "API" + ], + "author": "Joel Chu ", + "license": "ISC", + "dependencies": { + "debug": "^4.1.1", + "jsonql-koa": "^1.3.8", + "koa": "^2.8.1", + "koa-bodyparser": "^4.2.1", + "koa-cors": "0.0.16" + } +} diff --git a/packages/@jsonql/koa/tests/main.test.js b/packages/@jsonql/koa/tests/main.test.js new file mode 100644 index 00000000..5ca1fe69 --- /dev/null +++ b/packages/@jsonql/koa/tests/main.test.js @@ -0,0 +1,2 @@ +// The main test +const test = require('ava') -- Gitee From c6ee19f5c735d23c85571893d73f69d275207fc1 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Thu, 22 Aug 2019 09:36:23 +0800 Subject: [PATCH 2/4] final on the setup and update the README with @TODO --- packages/@jsonql/koa/README.md | 12 ++++++++---- packages/@jsonql/koa/index.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/@jsonql/koa/README.md b/packages/@jsonql/koa/README.md index 83fe3d92..712747cd 100644 --- a/packages/@jsonql/koa/README.md +++ b/packages/@jsonql/koa/README.md @@ -4,15 +4,15 @@ It comes with `koa-cors`, `koa-bodyparser`. -# Installation +## Installation ```sh $ npm i @jsonql/koa ``` -# How to +## How to -Just import and setup the config +Just import and pass a `config` (or nothing to use all the default options) ```js const jsonqlKoa = require('@jsonql/koa') @@ -25,7 +25,7 @@ const { app, server, stop } = jsonqlKoa({ ``` By default it will start on the port `8001` if you want to change it, -pass `port` to the config. +pass `port` to the `config`. ```js const jsonqlKoa = require('@jsonql/koa') @@ -40,6 +40,10 @@ As you can see from the example, it return the following object - server - the underlying `http.server` - stop - a function to call and close the server +## TODO + +We will add the file upload interface, socket server and other new features in the later release. + --- Joel Chu (c) 2019 diff --git a/packages/@jsonql/koa/index.js b/packages/@jsonql/koa/index.js index b4063bc8..1c40fe92 100644 --- a/packages/@jsonql/koa/index.js +++ b/packages/@jsonql/koa/index.js @@ -11,9 +11,23 @@ const jsonql = require('jsonql-koa') */ module.exports = function(config = {}) { + const port = ('port' in config) ? config.port : 8001; + const app = new Koa() app.use(cors()) app.use(bodyparser()) app.use(jsonql(config)) + const server = app.listen(port) + const stop = () => { + debug(`stop @jsonql/koa server`) + server.close() + } + debug(`@jsonql/koa server started on @${port}`) + + return { + app, + server, + stop + } } -- Gitee From 7f7afa8652002880803187476ee7d9bb7a39d482 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Thu, 22 Aug 2019 09:54:03 +0800 Subject: [PATCH 3/4] setup the test files but unable to test due to ava is not available to install --- packages/@jsonql/koa/package.json | 4 ++++ .../resolvers/mutation/change-something.js | 7 +++++++ .../fixtures/resolvers/query/get-something.js | 7 +++++++ packages/@jsonql/koa/tests/fixtures/setup.js | 7 +++++++ packages/@jsonql/koa/tests/main.test.js | 18 ++++++++++++++++++ 5 files changed, 43 insertions(+) create mode 100644 packages/@jsonql/koa/tests/fixtures/resolvers/mutation/change-something.js create mode 100644 packages/@jsonql/koa/tests/fixtures/resolvers/query/get-something.js create mode 100644 packages/@jsonql/koa/tests/fixtures/setup.js diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index eb914c24..ffba8e7f 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -22,5 +22,9 @@ "koa": "^2.8.1", "koa-bodyparser": "^4.2.1", "koa-cors": "0.0.16" + }, + "devDependencies": { + "ava": "^2.3.0", + "superkoa": "^1.0.3" } } diff --git a/packages/@jsonql/koa/tests/fixtures/resolvers/mutation/change-something.js b/packages/@jsonql/koa/tests/fixtures/resolvers/mutation/change-something.js new file mode 100644 index 00000000..5bf48fa8 --- /dev/null +++ b/packages/@jsonql/koa/tests/fixtures/resolvers/mutation/change-something.js @@ -0,0 +1,7 @@ +/** + * @param {string} payload what to change + * @return {string} a mutated message + */ +module.exports = function changeSomething(payload) { + return `Bye ${payload}` +} diff --git a/packages/@jsonql/koa/tests/fixtures/resolvers/query/get-something.js b/packages/@jsonql/koa/tests/fixtures/resolvers/query/get-something.js new file mode 100644 index 00000000..8fa81a4e --- /dev/null +++ b/packages/@jsonql/koa/tests/fixtures/resolvers/query/get-something.js @@ -0,0 +1,7 @@ +/** + * @return {string} msg + * + */ +module.exports = function getSomething() { + return `Hello` +} diff --git a/packages/@jsonql/koa/tests/fixtures/setup.js b/packages/@jsonql/koa/tests/fixtures/setup.js new file mode 100644 index 00000000..07e066bb --- /dev/null +++ b/packages/@jsonql/koa/tests/fixtures/setup.js @@ -0,0 +1,7 @@ +const server = require('../../') +const { join } = require('path') + +module.exports = () => server({ + resolverDir: join(__dirname, 'resolvers'), + contractDir: join(__dirname, 'contract') +}) diff --git a/packages/@jsonql/koa/tests/main.test.js b/packages/@jsonql/koa/tests/main.test.js index 5ca1fe69..c70970a1 100644 --- a/packages/@jsonql/koa/tests/main.test.js +++ b/packages/@jsonql/koa/tests/main.test.js @@ -1,2 +1,20 @@ // The main test const test = require('ava') +const server = require('./fixtures/setup') +const request = require('superkoa') + +test.before(t => { + const { app, stop } = server() + t.context.app = app; + t.context.stop = stop; +}) + +test.after(t => { + t.context.stop() +}) + +test.todo(`It should able to say hello world`) + +test.todo(`It should able to query the jsonql api`) + +test.todo(`It should able to mutated with jsonql api`) -- Gitee From b2e8944567a798375159dfd2c223a45d0a923d6d Mon Sep 17 00:00:00 2001 From: Joelchu Date: Thu, 22 Aug 2019 21:30:13 +0800 Subject: [PATCH 4/4] strange thing about the ava can not install --- packages/@jsonql/koa/Dockerfile | 8 ++++++++ packages/@jsonql/koa/README.md | 12 ++++++++++++ packages/@jsonql/koa/docker-compose.yml | 11 +++++++++++ packages/@jsonql/koa/package.json | 16 +++------------- packages/@jsonql/koa/tests/fixtures/start.js | 0 5 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 packages/@jsonql/koa/tests/fixtures/start.js diff --git a/packages/@jsonql/koa/Dockerfile b/packages/@jsonql/koa/Dockerfile index e69de29b..97ae4701 100644 --- a/packages/@jsonql/koa/Dockerfile +++ b/packages/@jsonql/koa/Dockerfile @@ -0,0 +1,8 @@ +FROM node:lts-alpine + +WORKDIR /usr/app + +COPY package.json . +RUN npm install --quiet + +COPY . . diff --git a/packages/@jsonql/koa/README.md b/packages/@jsonql/koa/README.md index 712747cd..31697a27 100644 --- a/packages/@jsonql/koa/README.md +++ b/packages/@jsonql/koa/README.md @@ -47,3 +47,15 @@ We will add the file upload interface, socket server and other new features in t --- Joel Chu (c) 2019 + + +"dependencies": { + "debug": "^4.1.1", + "jsonql-koa": "^1.3.8", + "koa": "^2.8.1", + "koa-bodyparser": "^4.2.1", + "koa-cors": "0.0.16" +}, +"devDependencies": { + "superkoa": "^1.0.3" +} diff --git a/packages/@jsonql/koa/docker-compose.yml b/packages/@jsonql/koa/docker-compose.yml index e69de29b..223c2dcb 100644 --- a/packages/@jsonql/koa/docker-compose.yml +++ b/packages/@jsonql/koa/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' + +services: + web: + build: . + command: npm run start + volumes: + - .:/usr/app/ + - /usr/app/node_modules + ports: + - "8001:8001" diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index ffba8e7f..932692b0 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -4,7 +4,8 @@ "description": "Complete jsonql Koa setup + extra in later release", "main": "index.js", "scripts": { - "test": "DEBUG=@jsonql_koa* ava --verbose" + "test": "DEBUG=@jsonql_koa* ava --verbose", + "start": "node ./test/fixtures/start.js" }, "keywords": [ "jsonql", @@ -15,16 +16,5 @@ "API" ], "author": "Joel Chu ", - "license": "ISC", - "dependencies": { - "debug": "^4.1.1", - "jsonql-koa": "^1.3.8", - "koa": "^2.8.1", - "koa-bodyparser": "^4.2.1", - "koa-cors": "0.0.16" - }, - "devDependencies": { - "ava": "^2.3.0", - "superkoa": "^1.0.3" - } + "license": "ISC" } diff --git a/packages/@jsonql/koa/tests/fixtures/start.js b/packages/@jsonql/koa/tests/fixtures/start.js new file mode 100644 index 00000000..e69de29b -- Gitee