Created
January 18, 2018 08:32
-
-
Save rajiff/75d7f078da40824acab8a96ae0c058f9 to your computer and use it in GitHub Desktop.
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
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