diff --git a/packages/@jsonql/koa/Dockerfile b/packages/@jsonql/koa/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..97ae4701a051df552a77c9307f4cdade95015187 --- /dev/null +++ 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 new file mode 100644 index 0000000000000000000000000000000000000000..31697a27099853f7083d159da691df3f0fd72533 --- /dev/null +++ b/packages/@jsonql/koa/README.md @@ -0,0 +1,61 @@ +# @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 pass a `config` (or nothing to use all the default options) + +```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 + +## TODO + +We will add the file upload interface, socket server and other new features in the later release. + +--- + +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/config/default.json b/packages/@jsonql/koa/config/default.json new file mode 100644 index 0000000000000000000000000000000000000000..8ca2dd8efcc89f2d43a07d45dc5f1228cd6df712 --- /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 0000000000000000000000000000000000000000..223c2dcb8287895cbc34b4d80e7108165a8f23ba --- /dev/null +++ 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/index.js b/packages/@jsonql/koa/index.js new file mode 100644 index 0000000000000000000000000000000000000000..1c40fe926a9a36973a4ed389eca12fb286a03db8 --- /dev/null +++ b/packages/@jsonql/koa/index.js @@ -0,0 +1,33 @@ +// 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 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 + } +} diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json new file mode 100644 index 0000000000000000000000000000000000000000..932692b0ffc7e3ab2efec574282dbd4161f062ed --- /dev/null +++ b/packages/@jsonql/koa/package.json @@ -0,0 +1,20 @@ +{ + "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", + "start": "node ./test/fixtures/start.js" + }, + "keywords": [ + "jsonql", + "koa", + "server", + "socket", + "WebSocket", + "API" + ], + "author": "Joel Chu ", + "license": "ISC" +} 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 0000000000000000000000000000000000000000..5bf48fa8cd80d583031461defc2517d3e51386b8 --- /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 0000000000000000000000000000000000000000..8fa81a4eecc07db0050403e783d41a3de8580b84 --- /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 0000000000000000000000000000000000000000..07e066bbbe5ee38ab9b98be4211889b61b9bf40f --- /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/fixtures/start.js b/packages/@jsonql/koa/tests/fixtures/start.js new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/packages/@jsonql/koa/tests/main.test.js b/packages/@jsonql/koa/tests/main.test.js new file mode 100644 index 0000000000000000000000000000000000000000..c70970a100f459928cae4d0a2c05593310cf9014 --- /dev/null +++ b/packages/@jsonql/koa/tests/main.test.js @@ -0,0 +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`)