Why Need to Choose GraphQL Over Rest?
GraphQL - Technology from Facebook
GraphQL is a query language, created by Facebook with the purpose of building client applications based on intuitive and flexible syntax, for describing their data requirements and interactions.
One of the primary challenges with traditional REST calls is the inability of the client to request a customized (limited or expanded) set of data. In most cases, once the client requests information from the server, it either gets all or none of the fields.
Another difficulty is working and maintain multiple endpoints. As a platform grows, consequently the number will increase. Therefore, clients often need to ask for data from different endpoints.
When building a GraphQL server, it is only necessary to have one URL for all data fetching and mutating. Thus, a client can request a set of data by sending a query string, describing what they want, to a server.
GraphQL is presented as a revolutionary new way to think about APIs. Instead of working with rigid server-defined endpoints, you can send queries to get exactly the data you’re looking for in one request. And it’s true — GraphQL can be transformative when adopted in an organization, enabling frontend and backend teams to collaborate more smoothly than ever before. But in practice, both of these technologies involve sending an HTTP request and receiving some result, and GraphQL has many elements of the REST model built in.
Where is it useful?
GraphQL helps where your client needs a flexible response format to avoid extra queries and/or massive data transformation with the overhead of keeping them in sync. Using a GraphQL server makes it very easy for a client side developer to change the response format without any change on the backend.
With GraphQL, you can describe the required data in a more natural way. It can speed up development, because in application structures like top-down rendering in React, the required data is more similar to your component structure.
Why GraphQL over OData and REST?
Choose the GraphQL over OData and REST, because of following reasons:
- Need to support the linked resource, to give an example, let say in university we have a student which belongs to department, We need to get student details along with department details, To achieve same thing need to make multiple calls to server if we are using OData or REST.
- Another case like a client application wants to get student’s course details, to fulfill this demand, need to define custom REST or OData endpoint, GraphQL avoids the need of such custom endpoint, as we define a product data model as a graph, so that get the data as link exist between the nodes of graph.
- To support the backward compatibility of services and data contract, Using OData or REST need to maintain URL or DataContract versing also need to do message routing in some cases. But using GraphQL we don’t need to do it. As client knows what data it is consuming using GraphQL request and from single GraphQL endpoint.
- OData apply convention for data fetching as compare to REST, same thing is done in GraphQL using Type System, Arguments and Selection Sets.
- In OData meta data is published in EDM format and REST is struggling to define the meta data standard as WADL or Swagger and many more. Here GraphQL is published meta data in defined format through introspection api and also provided document explorer for it.
- WS-Trust, WS-Federation and WS-Security is already addressed by OAuth 2.0 and Open Id Connect 1.0 for API so one can use it for GraphQL as well. For more details: - https://www.ceymplon.lk/service/it-service/customised-software-applications