Skip to content

Instantly share code, notes, and snippets.

@czbaker
Created September 21, 2017 01:48
Show Gist options
  • Select an option

  • Save czbaker/02f3affb08c219839f94261886a5333a to your computer and use it in GitHub Desktop.

Select an option

Save czbaker/02f3affb08c219839f94261886a5333a to your computer and use it in GitHub Desktop.
import path from 'path';
import express from 'express';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlConnect } from 'graphql-server-express';
// Other imports
import Mongoose from './mongoose/mongoose.js';
import GQLSchema from './graphql/schema.js';
import webpackConfig from '../webpack.config';
// Express instance
const app = express();
// Check to see if we're in a developer environment (yarn run dev)
const isDeveloping = process.env.NODE_ENV !== 'production';
// Set port using command line PORT variable if we're not using dev server
const port = isDeveloping ? 3000 : process.env.PORT;
// Webpack middleware if in dev mode.
if (isDeveloping) {
// Middleware initialization
const compiler = webpack(webpackConfig);
const webpackMiddleware = webpackDevMiddleware(compiler);
// Apply middlewares from Webpack
app.use(webpackMiddleware);
app.use(webpackHotMiddleware(compiler));
// File to serve:
const serveMe = path.resolve(__dirname, '..', 'client', 'index.html');
// Handle GET requests to the Express server
app.get('/', (req, res) => {
res.sendFile(serveMe);
res.end();
});
} else {
app.use(express.static(__dirname + '../client/dist'));
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, '../client/dist/index.html'));
});
}
// Apollo+GraphQL Endpoints
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: GQLSchema }));
app.use('/graphiql', graphiqlConnect({ endpointURL: '/graphql' }));
app.listen(port, (err) => {
if (err) console.log(`Unable to start Express server: ${err}`);
console.log(`Listening on port ${port}!`);
});
import { makeExecutableSchema } from 'graphql-tools';
import Task from '../mongoose/task.js';
const typeDefs = `
type Task {
_id: String
title: String
task: String
}
type Query {
singleTask(id: String!): Task
allTasks: [Task]
}
type Mutation {
addTask (
title: String!
task: String!
): Task
removeTask (
id: String!
): Task
}
`;
const resolvers = {
Query: {
singleTask: (root, { id }) => Task.findOne({ _id: id }),
allTasks: () => {
return Task.find({})
.then((result) => {
return result;
})
.catch((err) => {
throw new Error(`Unable to query MongoDB! ${err.stack}`);
});
},
},
Mutation: {
addTask: (root, { title, task }) => {
const newTask = new Task({ title, task });
return newTask.save()
.catch((error) => {
throw new Error(`Unable to store task in database: ${error.stack}`);
});
},
},
};
const Schema = makeExecutableSchema({
typeDefs,
resolvers,
});
export default Schema;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment