GrapghQL

GraphQL

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.

Developed by Facebook in 2012 and open-sourced in 2015, GraphQL allows clients to define the structure of the data required and returned from the server, thus preventing excessively large amounts of data from being returned.

GraphQL provides a complete description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

GraphQL_Logo.svg.png

Benefits of GraphQL

  • bulletGet Predictable Results – Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.
  • bulletGet Many Resources in a Single Request – GraphQL queries access not just the properties of one resource but also smoothly follow references between them. While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections.
  • bulletType System – GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint. GraphQL uses types to ensure Apps only ask for what’s possible and provide clear and helpful errors. Apps can use types to avoid writing manual parsing code.

GraphQL vs REST API

It’s important to understand the differences between GraphQL and REST API and how they work together. Click below to explore some of the major differences between the two.

Endpoints :

REST APIs put different information on different endpoints or paths/URLs. There is an endpoint for products and there is an endpoint for cart. They are different URLs. Because they are different URLs, if you are interested in both cart and product information at the same time, you have to run more than one HTTP request because that is how the API is structured.

With GraphQL there is only one single endpoint: /graphql

When you want to query for anything, for example products and carts, you can always ask those questions at the same time.

I want to know about product 1234 and I also want to know about the category tree

With GraphQL, you can ask a more complex question and you can do it in a single API request.

Return only what you need :

REST APIs tend to return all properties associated with an object.

If you send a request for a product, the response always returns the product description and the product description can be quite large. If you are only interested in the name and the image, the response has given you too much information.

Overfetching :

GraphQL requires that each field be requested explicitly. You have to be very explicit about every single field that you want to request. So, in our REST API for example you would say, “I’m interested in product ID 123” and the response would send you every field related to that product.

With GraphQL you have to say, “I’m interested in product 123 and I want to know the name, the brand, the price and sale price, and I want the default image” .

You have to ask for that very explicitly. As a result, we get rid of the problem of overfetching because you have to be extremely explicit about what you want.

Resources

GraphQL Cheatsheet REFERENCE

Leave a comment

Your email address will not be published. Required fields are marked *