├── .babelrc ├── README.md ├── graphql └── Schema │ └── Schema.js ├── index.html ├── mongoose └── todo.js ├── package.json └── server.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015","stage-0"] 3 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Graph SQL Server Example 2 | 3 | This is a short example to help setup a GraphQL server on Node using Express and connect to Mongo DB using Mongoose. 4 | 5 | > Refer `package.json`'s ***dependencies*** and ***devDependencies*** to see the npm packages and versions used. 6 | 7 | ### Important Files and Folders 8 | 9 | |File/Folder|Description| 10 | |-----------|-----------| 11 | |**index.html**| The main HTML file| 12 | |**server.js**| GraphQL Express Server| 13 | |**graphql**| Folder related to graphQL Schema.| 14 | |**mongoose**| Folder related to Mongoose(mongodb) *Schema* and *Model*| 15 | 16 | ### Pre-requisites for this example to work. 17 | This example uses mongodb installed on the local machine. 18 | 19 | * Refer [Installation](https://docs.mongodb.com/manual/administration/install-community/) for instructions to install mongodb locally on your machine. 20 | * MongoDb has only a CLI, so you can install any UI tools for mongodb so that its easier to work on the database. I have installed [robomongo](https://robomongo.org/) and find it perfect for my basic use cases. 21 | 22 | ### Starting the GraphQL server 23 | * Before running the example we start the mongo db service using the below command. 24 | ``` 25 | sudo service mongod start 26 | ``` 27 | 28 | * Once the service is up and running, then issue the below command to start your graph QL server. 29 | ***Note*** - *babel-node is to be used in Dev environments only.* 30 | ``` 31 | npm run dev3 32 | ``` 33 | 34 | ### Testing the server 35 | * In this example we have a mongodb collection called ***TodoList*** which has the below schema: 36 | ``` 37 | itemId: Number, 38 | item: String, 39 | completed: Boolean 40 | 41 | ``` 42 | 43 | * Once the server is up, go to browser and run http://localhost:3000/. 44 | * The first section is a simple form which you can use to insert a new task to the ***TodoList*** collection. 45 | > As this is a basic example, only the property `item` is taken from the html, while `itemId` and `completed` are **hardcoded** as *1* and *false* respectively. 46 | * The second section is where we can see how GraphQL is used to GET value from ***TodoList*** where `itemId = 1`(hardcoded), by clicking on the link. 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /graphql/Schema/Schema.js: -------------------------------------------------------------------------------- 1 | import { 2 | GraphQLObjectType, 3 | GraphQLNonNull, 4 | GraphQLSchema, 5 | GraphQLString, 6 | GraphQLList, 7 | GraphQLInt, 8 | GraphQLBoolean 9 | } from 'graphql/type'; 10 | 11 | import ToDoMongo from '../../mongoose/todo' 12 | 13 | /** 14 | * generate projection object for mongoose 15 | * @param {Object} fieldASTs 16 | * @return {Project} 17 | */ 18 | export function getProjection (fieldASTs) { 19 | return fieldASTs.fieldNodes[0].selectionSet.selections.reduce((projections, selection) => { 20 | projections[selection.name.value] = true; 21 | return projections; 22 | }, {}); 23 | } 24 | 25 | var todoType = new GraphQLObjectType({ 26 | name: 'todo', 27 | description: 'todo item', 28 | fields: () => ({ 29 | itemId: { 30 | type: (GraphQLInt), 31 | description: 'The id of the todo.', 32 | }, 33 | item: { 34 | type: GraphQLString, 35 | description: 'The name of the todo.', 36 | }, 37 | completed: { 38 | type: GraphQLBoolean, 39 | description: 'Completed todo? ' 40 | } 41 | }) 42 | }); 43 | 44 | var schema = new GraphQLSchema({ 45 | query: new GraphQLObjectType({ 46 | name: 'RootQueryType', 47 | fields: { 48 | todo: { 49 | type: new GraphQLList(todoType), 50 | args: { 51 | itemId: { 52 | name: 'itemId', 53 | type: new GraphQLNonNull(GraphQLInt) 54 | } 55 | }, 56 | resolve: (root, {itemId}, source, fieldASTs) => { 57 | var projections = getProjection(fieldASTs); 58 | var foundItems = new Promise((resolve, reject) => { 59 | ToDoMongo.find({itemId}, projections,(err, todos) => { 60 | err ? reject(err) : resolve(todos) 61 | }) 62 | }) 63 | 64 | return foundItems 65 | } 66 | } 67 | } 68 | }) 69 | 70 | }); 71 | 72 | export default schema; 73 | 74 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |