You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RESTful APIs: Follows a structured approach with endpoints for each resource (e.g., /users, /posts).
GraphQL APIs: Allows fetching only required data in a single request, reducing over-fetching and under-fetching.
Example: RESTful API with Express
constexpress=require('express');constapp=express();app.use(express.json());app.get('/users/:id',(req,res)=>{res.json({id: req.params.id,name: "Pisey"});});app.listen(3000,()=>console.log('Server running on port 3000'));
Example: GraphQL API with Apollo Server
const{ ApolloServer, gql }=require('apollo-server');consttypeDefs=gql` type User { id: ID! name: String! } type Query { user(id: ID!): User }`;constresolvers={Query: {user: (_,{ id })=>({ id,name: "Pisey"}),},};constserver=newApolloServer({ typeDefs, resolvers });server.listen().then(({ url })=>console.log(`Server running at ${url}`));