Last active
October 12, 2017 21:13
-
-
Save BransonGitomeh/50e400065886131ae315c70b3d88fbe2 to your computer and use it in GitHub Desktop.
Express router, graphql, GCP cloud function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express'; | |
import graphqlHTTP from 'express-graphql'; | |
import { buildSchema } from 'graphql'; | |
const router = express.Router(); | |
const schema = buildSchema(` | |
type Query { | |
hello: String | |
}`); | |
const rootValue = { hello: () => 'Hello world students!' }; | |
router.get( | |
'/', | |
graphqlHTTP({ | |
schema, | |
rootValue, | |
graphiql: true, | |
}), | |
); | |
router.post( | |
'/', | |
graphqlHTTP(() => { | |
const startTime = Date.now(); | |
return { | |
schema, | |
rootValue, | |
graphiql: false, | |
extensions() { | |
return { runTime: `${Date.now() - startTime} ms` }; | |
}, | |
}; | |
}), | |
); | |
const test = router; | |
export { test }; |
//webpack.config.js
const path = require('path');
const webpack = require('webpack');
const fs = require('fs');
const nodeModules = {};
fs.readdirSync('node_modules')
.filter(x => ['.bin'].indexOf(x) === -1)
.forEach((mod) => {
nodeModules[mod] = `commonjs ${mod}`;
});
module.exports = {
entry: './src',
target: 'node',
devtool: 'source-map',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'index.bundle.js',
libraryTarget: 'this',
},
externals: nodeModules,
plugins: [
new webpack.BannerPlugin({
banner: 'require("source-map-support").install();',
raw: true,
entryOnly: false,
}),
],
module: {
loaders: [{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015'],
},
}],
},
stats: {
colors: true,
},
};
The current web-pack config.
test locally with
const express = require('express');
const app = express();
const { test } = require('./index.bundle.js');
app.use(test);
app.listen(3000, () => {
console.log('Cool app listening on port 3000!');
});
Put this beside the build folder where your output is.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run through webpack and babel-es2015 to ensur e you dont hit issues with node 6 thats default , this year in 2017