├── README.md ├── data.json ├── package.json ├── schema.js └── server.js /README.md: -------------------------------------------------------------------------------- 1 | # Customerbase GraphQL Server 2 | 3 | Node.js/Express CRUD backend using GraphQL and JSON-Server 4 | 5 | ### Version 6 | 1.0.0 7 | 8 | ## Usage 9 | 10 | -Install Dependencies 11 | 12 | ```bash 13 | $ npm install 14 | ``` 15 | 16 | -Run JSON-Server (Port 3000) 17 | 18 | ```bash 19 | $ npm run json:server 20 | ``` 21 | 22 | -Run Server (Port 4000) 23 | 24 | ```bash 25 | $ npm run dev:server 26 | ``` 27 | 28 | -Visit Graphiql IDE 29 | 30 | Go to http://localhost:4000/graphql -------------------------------------------------------------------------------- /data.json: -------------------------------------------------------------------------------- 1 | { 2 | "customers": [ 3 | { 4 | "id": "1", 5 | "name": "John Doe", 6 | "email": "john@gmail.com", 7 | "age": 36 8 | }, 9 | { 10 | "id": "2", 11 | "name": "Keith Wilson", 12 | "email": "kieth@gmail.com", 13 | "age": 50 14 | }, 15 | { 16 | "id": "3", 17 | "name": "Tom Jones", 18 | "email": "tom@gmail.com", 19 | "age": 23 20 | }, 21 | { 22 | "id": "5", 23 | "name": "Jen Thompson", 24 | "email": "jen@gmail.com", 25 | "age": 22 26 | }, 27 | { 28 | "name": "Harry White", 29 | "email": "harry@gmail.com", 30 | "age": 34, 31 | "id": "rytP7hhUW" 32 | } 33 | ] 34 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "customerbase", 3 | "version": "1.0.0", 4 | "description": "Simple GraphQL server", 5 | "main": "server.js", 6 | "scripts": { 7 | "dev:server": "nodemon server.js", 8 | "json:server": "json-server --watch data.json" 9 | }, 10 | "author": "Brad Traversy", 11 | "license": "MIT", 12 | "dependencies": { 13 | "axios": "^0.16.2", 14 | "express": "^4.15.3", 15 | "express-graphql": "^0.6.7", 16 | "graphql": "^0.10.5", 17 | "json-server": "^0.11.2", 18 | "nodemon": "^1.11.0" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /schema.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | const { 3 | GraphQLObjectType, 4 | GraphQLString, 5 | GraphQLInt, 6 | GraphQLSchema, 7 | GraphQLList, 8 | GraphQLNonNull 9 | } = require('graphql'); 10 | 11 | /* 12 | // Hardcoded data 13 | const customers = [ 14 | {id:'1', name:'John Doe', email:'jdoe@gmail.com', age:35}, 15 | {id:'2', name:'Steve Smith', email:'steve@gmail.com', age:25}, 16 | {id:'3', name:'Sara Williams', email:'sara@gmail.com', age:32}, 17 | ]; 18 | */ 19 | 20 | // Customer Type 21 | const CustomerType = new GraphQLObjectType({ 22 | name:'Customer', 23 | fields:() => ({ 24 | id: {type:GraphQLString}, 25 | name: {type: GraphQLString}, 26 | email: {type: GraphQLString}, 27 | age: {type: GraphQLInt}, 28 | }) 29 | }); 30 | 31 | // Root Query 32 | const RootQuery= new GraphQLObjectType({ 33 | name:'RootQueryType', 34 | fields:{ 35 | customer:{ 36 | type:CustomerType, 37 | args:{ 38 | id:{type:GraphQLString} 39 | }, 40 | resolve(parentValue, args){ 41 | /* 42 | for(let i = 0;i < customers.length;i++){ 43 | if(customers[i].id == args.id){ 44 | return customers[i]; 45 | } 46 | } 47 | */ 48 | return axios.get('http://localhost:3000/customers/'+ args.id) 49 | .then(res => res.data); 50 | 51 | } 52 | }, 53 | customers:{ 54 | type: new GraphQLList(CustomerType), 55 | resolve(parentValue, args){ 56 | return axios.get('http://localhost:3000/customers') 57 | .then(res => res.data); 58 | } 59 | } 60 | } 61 | }); 62 | 63 | // Mutations 64 | const mutation = new GraphQLObjectType({ 65 | name:'Mutation', 66 | fields:{ 67 | addCustomer:{ 68 | type:CustomerType, 69 | args:{ 70 | name: {type: new GraphQLNonNull(GraphQLString)}, 71 | email: {type: new GraphQLNonNull(GraphQLString)}, 72 | age: {type: new GraphQLNonNull(GraphQLInt)} 73 | }, 74 | resolve(parentValue, args){ 75 | return axios.post('http://localhost:3000/customers', { 76 | name:args.name, 77 | email: args.email, 78 | age:args.age 79 | }) 80 | .then(res => res.data); 81 | } 82 | }, 83 | deleteCustomer:{ 84 | type:CustomerType, 85 | args:{ 86 | id:{type: new GraphQLNonNull(GraphQLString)} 87 | }, 88 | resolve(parentValue, args){ 89 | return axios.delete('http://localhost:3000/customers/'+args.id) 90 | .then(res => res.data); 91 | } 92 | }, 93 | editCustomer:{ 94 | type:CustomerType, 95 | args:{ 96 | id:{type: new GraphQLNonNull(GraphQLString)}, 97 | name: {type: GraphQLString}, 98 | email: {type: GraphQLString}, 99 | age: {type: GraphQLInt} 100 | }, 101 | resolve(parentValue, args){ 102 | return axios.patch('http://localhost:3000/customers/'+args.id, args) 103 | .then(res => res.data); 104 | } 105 | }, 106 | } 107 | }); 108 | 109 | module.exports = new GraphQLSchema({ 110 | query: RootQuery, 111 | mutation 112 | }); -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const expressGraphQL = require('express-graphql'); 3 | const schema = require('./schema.js'); 4 | const cors = require('cors'); 5 | 6 | const app = express(); 7 | 8 | // enable `cors` to set HTTP response header: Access-Control-Allow-Origin: * 9 | app.use(cors()); 10 | 11 | app.use('/graphql', expressGraphQL({ 12 | schema:schema, 13 | graphiql:true 14 | })); 15 | 16 | app.listen(4000, () => { 17 | console.log('Server is running on port 4000..'); 18 | }); 19 | --------------------------------------------------------------------------------