GraphQL vs REST :
While working with GraphQL, I discovered how useful it can be for shaping API responses. 💪
So, I wanted to share with you what I recently learned and how Graphql helped me to make the work more flexible.✅😄
Let’s start with what is Graphql in the first place ❓😕❓
GraphQL is an open-source query language and runtime for APIs (Application Programming Interfaces) that was developed by Facebook. It lets clients select fields from a typed schema. That can make some data-fetching workflows more flexible, but it does not make every GraphQL API faster than REST.
You may be wondering now : emm🤔! and why is that ?
So let’s discover why should we use GraphQL over REST ? What are the benefits?
Efficient Data Fetching:
GraphQL allows clients to request exactly the data they need and no more. Instead of receiving fixed responses, clients can specify their data requirements in a GraphQL query. This can reduce over-fetching and extra requests when the schema fits the client’s needs. REST APIs can also offer field selection and purpose-built endpoints; backend query design still matters with either approach.
To make sure that this point is clear, let’s see this image
As you can notice in this example, the REST endpoint shown returns a fixed set of fields, as defined by that particular API unlike in the GraphQL query (queries are used to request data from the server in GraphQL and we have the mutations that allows us to create, update and delete data) we were able to specify which fields we need and in the response we got exactly what we wanted😀
Declarative and Strong Typing: GraphQL uses a schema language to define the data model and operations available in the API. This schema acts as a contract between the client and server, ensuring a clear understanding of the data structure and operations. It provides strong typing, allowing clients to know in advance what types of data they can expect and what operations are available.
What we mean by that ?🤔
well let’s imagine that we are building a simple application that sells books, we have 2 main types books and categories. Here's how we could define the schema for this application in GraphQL:
type Book {
id: ID!
name: String!
price: Float!
category: Category!
}
type Category {
id: ID!
name: String!
description: String!
books: [Book!]!
}
type Query {
getBook(id: ID!): Book
getBooksByCategory(categoryId: ID!): [Book]!
getCategory(id: ID!): Category
getAllCategories: [Category]!
}
In this example, we have first the Book and Category types representing the entities in our application. We have also the Id, String and Float types ,we used them with different fields that are required in our case. Now if we want for example to get a book by it’s id our query will look like this
query {
getBook(id: "123") {
name
price
category {
name
}
}
}
Here, If we enter a field that does not exist in the schema, GraphQL server will respond with an error, indicating that the field is not valid, catching an invalid query before execution. Resolver bugs, authorization failures and other runtime errors can still happen; a schema does not replace application validation or database constraints.
Tradeoffs to keep in mind
GraphQL gives clients control over selected fields, but the server must still enforce authorization, pagination and query-cost limits. Nested resolvers can also produce N+1 database queries if batching and data loading are not designed carefully. Choose GraphQL or REST based on the product and team’s requirements, rather than expecting one to be universally better.
To continue learning, see the official guides to queries, authorization and performance.
Hope you find this helpful don’t hesitate for any comments and questions.
👩💻happy coding!

Top comments (1)
very helpful and informative, wanted to ask about graphql mutation , and what's AST in graphql ?