Created
March 26, 2018 09:17
-
-
Save BransonGitomeh/c6e2d4c9cc31bf6a7056e714402d52b0 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 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