A small project to illustrate using GraphQL with MongoDB in Express as simply as possible
- Create a GraphQL + MongoDB + Express project that is as simple as possible
- GraphiQL Integration to send Queries / Mutations
- Ability to use Postman or other REST Client to POST GraphQL Queries / Mutations
- Use of ES6 (but minimally)
- Full Create, Update and Query from GraphQL to MongoDB
This project makes use of ES6 which requires a 4+ version of Node https://nodejs.org/en/download/
In order to run this project you need to install MongoDB and ensure an instance is running. https://www.npmjs.com/package/mongodb
npm install mongodb --save
mongod
To see the data you are saving you can use a MongoDB Manager App such as https://robomongo.org/
The following NPM modules are required in package.json:
- express
- express-graphql
- graphql
- mongoose
- babel-cli
- babel-preset-es2015
- nodemon
Install with:
npm install
npm dev is configured with nodmon so that the server automatically restarts when code files are changes
npm run dev
npm start
npm prestart will run first, transpile the ES6 code and save to dist folder. npm start will then run the code directly from the dist folder
You can run these queries / mutations within GraphiQL, alternatively you can run them within a tool such as Postman. To do so ensure you POST the query / mutation in the body and set the content-type to GraphQL.
mutation {
addUser( name:"Jeff Beck", email:"jeff@beck.net", tel:"0800") {
id,
name,
email,
tel
}
}
Find the first user in the MongoDB, requesting just the name and email address
query {
userId(id:"56e0582a07be7df61583d9a4") {
name,
email
}
}
query {
users {
id,
name,
email
}
}
mutation {
updateUser(id:"56e0919c2c4670ed23fe4a28", name:"Jeff The Beck", email:"jeff@beck.net", tel:"0800 234 231") {
id
}
}
GraphQL doesn't care where your data is stored, in this case it's in a MongoDB.
userSchema.js uses a Mongoose Schemea and configures a number of exported functions for our GraphQL Resolvers to work with in order to load a user / list of users or create and update a user.
userType.js creates a GraphQLObjectType describing the structure of our data model.
userMutations.js and userQueries.js specify the operations available from GraphQL and link to our resolvers, in this case Mongoose operations that return a promise.