๐Ÿ’ก ๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ค๐—Ÿ ๐˜ƒ๐˜€ ๐—ฅ๐—˜๐—ฆ๐—ง: ๐—–๐—ต๐—ผ๐—ผ๐˜€๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ฅ๐—ถ๐—ด๐—ต๐˜ ๐—”๐—ฃ๐—œ ๐—ณ๐—ผ๐—ฟ ๐—ฌ๐—ผ๐˜‚๐—ฟ ๐—”๐—ฝ๐—ฝ๐—น๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป

Apurv upadhyay
3 min readNov 3, 2024

--

https://www.linkedin.com/in/apurvupadhyay/
graphql vs rest

In modern API development, the choice between ๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ค๐—Ÿ and ๐—ฅ๐—˜๐—ฆ๐—ง is crucial. Both have unique features and serve different purposes. Hereโ€™s a detailed look at their differences, pros and cons, and some code examples to guide you toward the best choice for your application.

๐Ÿ”น ๐—ฅ๐—˜๐—ฆ๐—ง (๐—ฅ๐—ฒ๐—ฝ๐—ฟ๐—ฒ๐˜€๐—ฒ๐—ป๐˜๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฆ๐˜๐—ฎ๐˜๐—ฒ ๐—ง๐—ฟ๐—ฎ๐—ป๐˜€๐—ณ๐—ฒ๐—ฟ)

๐—ฅ๐—˜๐—ฆ๐—ง is a stateless, HTTP-based architecture that is widely adopted for building APIs. Each resource, like โ€œusersโ€ or โ€œposts,โ€ has a unique URL, and standard HTTP methods (๐—š๐—˜๐—ง, ๐—ฃ๐—ข๐—ฆ๐—ง, ๐—ฃ๐—จ๐—ง, ๐——๐—˜๐—Ÿ๐—˜๐—ง๐—˜) are used to interact with these resources. Hereโ€™s what you need to know about REST:

โ€ข Pros:

โ€ข Simplicity: REST is straightforward, widely understood, and works well with traditional web apps.

โ€ข Scalability: Itโ€™s easy to scale REST APIs, as they can be cached and load-balanced effectively.

โ€ข Broad Client Compatibility: REST APIs can be called from browsers, mobile apps, and other HTTP-compatible clients.

โ€ข Cons:

โ€ข Data Over-fetching/Under-fetching: Clients may receive more data than they need (over-fetching) or miss required data (under-fetching), requiring additional requests.

โ€ข Multiple Endpoints for Related Data: Often requires multiple endpoints to fetch related data, which can slow down performance.

โ€ข Limited Query Flexibility: The API defines the data structure and format, limiting customization for the client.

๐—ฅ๐—˜๐—ฆ๐—ง ๐—ฆ๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ ๐—–๐—ผ๐—ฑ๐—ฒ (Express.js)

// Get a list of users
app.get('/users', (req, res) => {
// Returns all users with all fields
res.json(users);
});

// Get a specific user by ID
app.get('/users/:id', async (req, res) => {
const user = await getUserById(req.params.id);
res.json(user);
});

// Get posts for a specific user
app.get('/users/:id/posts', async (req, res) => {
const posts = await getUserPosts(req.params.id);
res.json(posts);
});

๐—ฆ๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ ๐—ฅ๐—˜๐—ฆ๐—ง ๐—ฅ๐—ฒ๐—พ๐˜‚๐—ฒ๐˜€๐˜๐˜€

  • Fetch all users:
GET /users
  • Fetch a specific userโ€™s details:
GET /users/{id}
  • Fetch posts for a specific user:
GET /users/{id}/posts

๐Ÿ”น ๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ค๐—Ÿ (๐—ค๐˜‚๐—ฒ๐—ฟ๐˜† ๐—Ÿ๐—ฎ๐—ป๐—ด๐˜‚๐—ฎ๐—ด๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—”๐—ฃ๐—œ๐˜€)

๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ค๐—Ÿ, developed by Facebook, allows clients to specify exactly what data they need in a single request. This is especially useful in applications that need flexible and efficient data retrieval. GraphQL uses one endpoint to handle multiple data queries and mutations, offering the client more control.

โ€ข Pros:

โ€ข Flexible Data Retrieval: Clients can request only the fields they need, reducing data over-fetching.

โ€ข Single Endpoint: Only one endpoint is needed, which simplifies the API structure.

โ€ข Efficient for Complex Data: Ideal for applications that need to fetch complex or nested data in one request.

โ€ข Cons:

โ€ข Complexity: Implementing and maintaining a GraphQL API can be more challenging.

โ€ข Client Libraries Required: GraphQL is not natively supported in browsers and requires client libraries.

โ€ข Potential for Overly Complex Queries: Poorly structured queries can lead to excessive resource usage.

๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ค๐—Ÿ ๐—ฆ๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ ๐—–๐—ผ๐—ฑ๐—ฒ (Apollo Server)

const { ApolloServer, gql } = require('apollo-server');

// Define schema
const typeDefs = gql`
type User {
id: ID!
name: String!
posts: [Post]
}

type Post {
id: ID!
title: String!
content: String!
}

type Query {
user(id: ID!): User
}
`;

// Define resolvers
const resolvers = {
Query: {
user: async (_, { id }) => getUserWithPosts(id),
},
User: {
posts: async (user) => getUserPosts(user.id),
},
};

const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});

// Sample Query
/*
query {
user(id: "1") {
name
posts {
title
}
}
}
*/

๐—ฆ๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ ๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ค๐—Ÿ ๐—ค๐˜‚๐—ฒ๐—ฟ๐˜†

query {
user(id: "1") {
name
posts {
title
content
}
}
}

๐ŸŽฏ ๐—ž๐—ฒ๐˜† ๐—ง๐—ฎ๐—ธ๐—ฒ๐—ฎ๐˜„๐—ฎ๐˜†๐˜€

โ€ข ๐—ฅ๐—˜๐—ฆ๐—ง is an excellent choice for public APIs and applications where simplicity and broad compatibility are critical.

โ€ข ๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ค๐—Ÿ is ideal for complex applications and microservices that need flexible data fetching, reduced network requests, and efficient handling of nested data.

Each approach serves specific needs, so choosing between ๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต๐—ค๐—Ÿ and ๐—ฅ๐—˜๐—ฆ๐—ง depends on your applicationโ€™s architecture, complexity, and data requirements.

โค๏ธ Share Your Thoughts!

Feel free to repost โ™ป๏ธ if you found this helpful. For more great content like this follow ๐Ÿ›  Apurv Upadhyay. Until next time, happy coding! ๐Ÿš€

#GraphQL #REST #API #WebDevelopment #SoftwareEngineering #Backend #Optimization

--

--

Apurv upadhyay
Apurv upadhyay

Written by Apurv upadhyay

Principal Software Engineer at PeerIslands โ€ข Microsoft Azure Certified Architect Expert & DevOps Specialist โ€ข 7x Azure Certified โ€ข ex-Microsoft, Bosch