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

View File

@ -11,13 +11,12 @@ An example GraphQL API using:
## Setup
1. Install the latest [Node.js](https://nodejs.org) and [npm](https://npmjs.com).
2. Run `npm install` within the `api` directory in Terminal.
3. Copy `.env.example`, rename it `.env` and customize.
2. Duplicate `.env.example` as `.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
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()
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 {
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 cors from 'kcors'
import compress from 'koa-compress'
import KoaRouter from 'koa-router'
import koaBody from 'koa-bodyparser'
import { makeExecutableSchema } from 'graphql-tools'
import { graphqlKoa } from 'graphql-server-koa'
import { apolloUploadKoa } from 'apollo-upload-server'
import typeDefs from './schema.graphql'
import resolvers from './resolvers'
import graphqlTools from 'graphql-tools'
import graphqlServerKoa from 'graphql-server-koa'
import apolloUploadServer from 'apollo-upload-server'
import types from './schema.mjs'
import resolvers from './resolvers.mjs'
const server = new Koa()
const router = new KoaRouter()
const schema = makeExecutableSchema({ typeDefs, resolvers })
server
// Enable Cross-Origin Resource Sharing (CORS)
@ -25,8 +23,10 @@ server
// GraphQL API
router.post(
'/graphql',
apolloUploadKoa({ uploadDir: './uploads' }),
graphqlKoa({ schema })
apolloUploadServer.apolloUploadKoa({ uploadDir: './uploads' }),
graphqlServerKoa.graphqlKoa({
schema: graphqlTools.makeExecutableSchema({ typeDefs: [types], resolvers })
})
)
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