Using ESM instead of Webpack for the API.

This commit is contained in:
Jayden Seric 2017-10-02 16:50:25 +11:00
parent c916cef8de
commit 9420391054
9 changed files with 1090 additions and 2771 deletions

6
api/.gitignore vendored
View File

@ -1,4 +1,4 @@
/dist .esm-cache
/uploads
db.json
.env .env
db.json
/uploads

3664
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,60 +1,38 @@
{ {
"name": "apollo-upload-examples-api", "name": "apollo-upload-examples-api",
"private": true, "private": true,
"engines": {
"node": ">=8.6",
"npm": ">=5.4"
},
"dependencies": { "dependencies": {
"@std/esm": "^0.11.0",
"apollo-upload-server": "^2.0.4", "apollo-upload-server": "^2.0.4",
"babel-core": "^6.26.0", "dotenv": "^4.0.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-0": "^6.24.1",
"dotenv-cli": "^1.4.0",
"graphql": "^0.10.5", "graphql": "^0.10.5",
"graphql-server-koa": "^1.1.2", "graphql-server-koa": "^1.1.2",
"graphql-tag": "^2.4.2",
"graphql-tools": "^1.2.3", "graphql-tools": "^1.2.3",
"kcors": "^2.2.1", "kcors": "^2.2.1",
"koa": "^2.3.0", "koa": "^2.3.0",
"koa-bodyparser": "^4.2.0", "koa-bodyparser": "^4.2.0",
"koa-compress": "^2.0.0", "koa-compress": "^2.0.0",
"koa-router": "^7.2.1", "koa-router": "^7.2.1",
"lowdb": "^0.17.2", "lowdb": "^0.17.2"
"source-map-support": "^0.4.18",
"webpack": "^3.6.0"
}, },
"devDependencies": { "devDependencies": {
"babel-eslint": "^7.2.3",
"eslint": "^4.8.0", "eslint": "^4.8.0",
"eslint-plugin-import": "^2.7.0", "eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.2.0", "eslint-plugin-node": "^5.2.0",
"eslint-plugin-prettier": "^2.3.1", "eslint-plugin-prettier": "^2.3.1",
"prettier": "^1.7.3", "nodemon": "^1.12.1",
"webpack-watch-server": "^1.2.1" "prettier": "^1.7.3"
}, },
"scripts": { "scripts": {
"lint": "eslint .", "lint": "eslint . --ext mjs",
"dev": "dotenv webpack-watch-server", "dev": "nodemon --ext mjs",
"build": "dotenv webpack", "start": "node --require @std/esm --require dotenv/config server.mjs"
"start": "dotenv node dist"
},
"engines": {
"node": ">=8.1.2",
"npm": ">=5"
},
"babel": {
"presets": [
[
"env",
{
"targets": {
"node": "8"
}
}
],
"stage-0"
]
}, },
"eslintConfig": { "eslintConfig": {
"parser": "babel-eslint",
"parserOptions": { "parserOptions": {
"sourceType": "module", "sourceType": "module",
"ecmaVersion": 2017, "ecmaVersion": 2017,
@ -77,17 +55,21 @@
"prettier" "prettier"
], ],
"rules": { "rules": {
"node/no-unsupported-features": "off", "curly": [
"prettier/prettier": [ "error",
"multi"
],
"node/no-unsupported-features": [
"error", "error",
{ {
"semi": false, "ignores": ["modules"]
"singleQuote": true
} }
] ],
"prettier/prettier": "error"
} }
}, },
"eslintIgnore": [ "prettier": {
"dist" "semi": false,
] "singleQuote": true
}
} }

View File

@ -11,13 +11,12 @@ An example GraphQL API using:
## Setup ## Setup
1. Install the latest [Node.js](https://nodejs.org) and [npm](https://npmjs.com). 1. Install the latest [Node.js](https://nodejs.org) and [npm](https://npmjs.com).
2. Run `npm install` within the `api` directory in Terminal. 2. Duplicate `.env.example` as `.env` and customize.
3. Copy `.env.example`, rename it `.env` and customize. 3. With Terminal in the `api` directory run `npm install`.
4. Run `npm run dev` for development, or `npm run start` for production.
For development run `npm run dev`.
For production run `npm run build && npm run start`.
## Support ## Support
See `engines` in `package.json`. See `package.json` `engines`.

View File

@ -5,7 +5,11 @@ const db = low(new FileSync('db.json'))
db.defaults({ uploads: [] }).write() db.defaults({ uploads: [] }).write()
const saveFile = file => const saveFile = file =>
db.get('uploads').push({ id: file.path, ...file }).last().write() db
.get('uploads')
.push({ id: file.path, ...file })
.last()
.write()
export default { export default {
Query: { Query: {

View File

@ -1,23 +0,0 @@
type File {
id: String!
name: String!
type: String!
size: Int!
path: String!
}
input Upload {
name: String!
type: String!
size: Int!
path: String!
}
type Query {
uploads: [File]
}
type Mutation {
singleUpload (file: Upload!): File!
multipleUpload (files: [Upload!]!): [File!]!
}

25
api/schema.mjs Normal file
View File

@ -0,0 +1,25 @@
export default /* GraphQL */ `
type File {
id: String!
name: String!
type: String!
size: Int!
path: String!
}
input Upload {
name: String!
type: String!
size: Int!
path: String!
}
type Query {
uploads: [File]
}
type Mutation {
singleUpload (file: Upload!): File!
multipleUpload (files: [Upload!]!): [File!]!
}
`

View File

@ -1,18 +1,16 @@
import 'source-map-support/register'
import Koa from 'koa' import Koa from 'koa'
import cors from 'kcors' import cors from 'kcors'
import compress from 'koa-compress' import compress from 'koa-compress'
import KoaRouter from 'koa-router' import KoaRouter from 'koa-router'
import koaBody from 'koa-bodyparser' import koaBody from 'koa-bodyparser'
import { makeExecutableSchema } from 'graphql-tools' import graphqlTools from 'graphql-tools'
import { graphqlKoa } from 'graphql-server-koa' import graphqlServerKoa from 'graphql-server-koa'
import { apolloUploadKoa } from 'apollo-upload-server' import apolloUploadServer from 'apollo-upload-server'
import typeDefs from './schema.graphql' import types from './schema.mjs'
import resolvers from './resolvers' import resolvers from './resolvers.mjs'
const server = new Koa() const server = new Koa()
const router = new KoaRouter() const router = new KoaRouter()
const schema = makeExecutableSchema({ typeDefs, resolvers })
server server
// Enable Cross-Origin Resource Sharing (CORS) // Enable Cross-Origin Resource Sharing (CORS)
@ -25,8 +23,10 @@ server
// GraphQL API // GraphQL API
router.post( router.post(
'/graphql', '/graphql',
apolloUploadKoa({ uploadDir: './uploads' }), apolloUploadServer.apolloUploadKoa({ uploadDir: './uploads' }),
graphqlKoa({ schema }) graphqlServerKoa.graphqlKoa({
schema: graphqlTools.makeExecutableSchema({ typeDefs: [types], resolvers })
})
) )
server.use(router.routes()).use(router.allowedMethods()) server.use(router.routes()).use(router.allowedMethods())

View File

@ -1,44 +0,0 @@
import path from 'path'
import { NoEmitOnErrorsPlugin } from 'webpack'
const config = {
devtool: 'source-map',
entry: {
index: './server.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs2'
},
externals: /^(?!\.|\/).+/i,
target: 'node',
node: {
__dirname: true
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: process.env.NODE_ENV === 'development'
}
}
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader'
}
]
}
}
if (process.env.NODE_ENV === 'development') {
config.plugins = [new NoEmitOnErrorsPlugin()]
}
export default config