Skip to content

Instantly share code, notes, and snippets.

@BransonGitomeh
Created March 26, 2018 09:17
Show Gist options
  • Save BransonGitomeh/c6e2d4c9cc31bf6a7056e714402d52b0 to your computer and use it in GitHub Desktop.
Save BransonGitomeh/c6e2d4c9cc31bf6a7056e714402d52b0 to your computer and use it in GitHub Desktop.
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { MongoClient } = require('mongodb');
const {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString
} = require('graphql');
const {
PORT = 4000,
HOST = '0.0.0.0',
MONGO_URL = 'mongodb://localhost:27017'
} = process.env
const app = express();
const main = async () => {
const db = await MongoClient.connect(MONGO_URL)
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve(_, args, { db }) {
return `world with mongodb-node version ${db.topology.clientInfo.driver.version}`;
}
}
}
})
});
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true,
context: {
db
}
}));
app.listen(PORT, HOST, () => console.log(`Graphql server started on 'http://${HOST}:${PORT}'`));
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment