Skip to content

Instantly share code, notes, and snippets.

@rajiff
Created January 18, 2018 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajiff/75d7f078da40824acab8a96ae0c058f9 to your computer and use it in GitHub Desktop.
Save rajiff/75d7f078da40824acab8a96ae0c058f9 to your computer and use it in GitHub Desktop.
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const graphqlHTTP = require('express-graphql');
const graphqlSchema = require('./graphqlSchema');
const {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString
} = require('graphql');
let app = express();
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve() {
return 'Hello world example to GraphQL';
}
}
}
})
});
// Configure morgan to log your requests, with a standard date & time format
morgan.token('time', (req, res) => new Date().toISOString());
app.use(morgan('[:time] :remote-addr :method :url :status :res[content-length] :response-time ms'));
// Setup bodyParsing middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Mount it at root, as this will be the only end point needed
app.use('/', graphqlHTTP({
schema: graphqlSchema,
graphiql: true
}));
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment