DEV Community

Cover image for Getting Started with GraphQL
Ameni Ben Saada
Ameni Ben Saada

Posted on

Getting Started with GraphQL

GraphQL vs REST :
During this few months, I have been working with GraphQL and I discrovered the power of using it.💪

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 provides a more efficient and flexible approach to data fetching and manipulation compared to traditional RESTful APIs.

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. And with this we eliminates the problem of over-fetching or under-fetching data that we face with REST APIs.

To make sure that this point is clear, let’s see this image

Image description

As you can notice in this example, for the rest API we sent a get request and as a response we got all the data saved 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]!
}
Enter fullscreen mode Exit fullscreen mode

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
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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, preventing runtime errors and ensuring data integrity.

Overall, GraphQL provides a more efficient and flexible way to fetch and manipulate data, reduces over-fetching and under-fetching issues, enhances developer productivity, and enables the building of powerful and responsive APIs. It has gained popularity among developers due to its many advantages. ✅✅

In this blog we mainly learned what are the advantages of using GraphQL👏, stay tuned for the next one in which we are going to learn how to implement GraphQL in real application using Apollo, a powerful and popular GraphQL client.🔥🔥

Hope you find this helpful don’t hesitate for any comments and questions.

👩‍💻happy coding!

Top comments (1)

Collapse
 
eyacode profile image
EyaCode

very helpful and informative, wanted to ask about graphql mutation , and what's AST in graphql ?