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
// src/controller/graphqlController.js | |
const graphqlHTTP = require('express-graphql'); | |
const { buildSchema } = require('graphql'); | |
// Construct a schema, using GraphQL schema language | |
const schema = buildSchema(` | |
type Query { | |
employee(id: Int!): Employee | |
employees: [Employee], |
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
// src/controller/graphqlController.js | |
const graphqlHTTP = require('express-graphql'); | |
const { buildSchema } = require('graphql'); | |
// Construct a schema, using GraphQL schema language | |
const schema = buildSchema(` | |
type Query { | |
employee(id: Int!): Employee # `!` signifies this is mandatory | |
employees: [Employee], |
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 graphqlController = require('./controller/graphqlController'); | |
app.use('/graphql', graphqlController); |
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 router = express.Router(); | |
const employeeService = require('../service/employeeService'); | |
router.get('/api/employees', (req, res) => res.json(employeeService.getAll())); | |
router.get('/api/employees/:id', (req, res) => { | |
const employeeId = req.params.id; | |
return res.json(employeeService.getById(employeeId)); |
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
REST APIs | GraphQL | |
---|---|---|
one URL serves only one request which leads to 100s of URL to maintan | One URL to serves every request | |
Multiple roundtrip to get various kinds of data | Single trip to get various combinations of data | |
Unwanted data travel from server to client (Wastage of bandwidth) | Only required data is travelled which make response light and minimum number | |
No flexibility | Much more flexible | |
Keeps Backend and frontend team tightly coupled | Backend and frontend team are loosely coupled | |
Change in structure of APIs needs the client to change as well | Change in structure of API is not needed in client side | |