What is GraphQL: The Benefits of GraphQL at a glance
June 07, 2022
By: Ton Koop
4 min. reading time
GraphQL is a query language that allows searching and working across multiple APIs (application programming interfaces) simultaneously. This language ensures that clients get exactly the data they ask for and no more than that. For any application, it is important to send data as efficiently as possible between client and APIs with as little load as possible. This is exactly what GraphQL caters to.
GraphQL is designed to make APIs fast, flexible and developer-friendly. With GraphQL, developers can compose requests that retrieve data from multiple data sources in a single API call.
In addition, GraphQL gives API administrators the flexibility to add or remove fields without affecting existing queries. Developers can build APIs with the methods they prefer, and the GraphQL specification will ensure that it can function in a predictable way.
Rest vs GraphQL
Developers use application programming interfaces, or APIs, to bring together or transfer data and functionality within or between applications. When it comes to working with APIs, developers are often faced with two popular options: REST or GraphQL.
REST is a software architecture style that APIs conform to so that developers can interact with services in a standard way. GraphQL is a query language for APIs and a runtime for executing those queries. REST and GraphQL are similar in that they identify data sources as URLs that the application can use to retrieve data or functionality. However, there are many differences.
- GraphQL exchanges data on a single endpoint, while REST often has multiple endpoints. GraphQL resolvers retrieve the data for the fields that are needed. If one resolver fails, the rest of the query can still retrieve and return useful data. In many cases, one GraphQL query often replaces multiple REST queries.
- GraphQL prevents over-fetching and under-fetching of data - that is, an endpoint responding to a call with too much or too little information, respectively, compared to what the app needs. REST APIs are offered in different levels of resolution. Some levels fetch more data and others less. This means that an app may receive too much data. Take as an example getting back the entire customer profile, when only the customer's name and email address were needed. It may also be that too little data is retrieved, causing the app to have to make multiple API calls instead of just one.
- GraphQL uses a custom query format called Schema Definition Language (SDL), and although that custom query language is used for the request, JSON is returned. This makes it easier for clients to use the response. GraphQL client libraries have native integration with the ReactJS UI framework (and many other libraries) making GraphQL accessible to many front-end and back-end developers today.
- The developer experience differs between GraphQL and REST. To understand how REST APIs work, the developer usually uses a portal (docs environment) to discover and interact with the APIs. In GraphQL, this portal is often a built-in playground that is also suitable for development. (See: https://afosto.app/graphql ). It allows developers to explore new queries in an interactive way. The documentation is also different. REST usually uses OpenAPI specs and portals. Some extensions to OpenAPI exist. For example, Swagger Docs builds interactive documentation based on those OpenAPI specs. GraphQL developers typically use schema-based interactive documentation, such as Apollo to discover, develop, and interact with GraphQL endpoints.
GraphQL Query Example
Below is an example of a GraphQL query. In this query, we retrieve certain data from a contact. As explained above, this query also returns only the requested data as response and no further contact details. So in this case you will only get the email, first name, middle name and last name of a contact based on the ID of that contact.
If you want to know more about the application of GraphQL vs. REST within the Afosto platform please contact us!
Other articles you might be interested in:
What is the difference between GraphQL and React?
GraphQL is an api technique while React is an app framework. GraphQL can be used in a React app for data retrieval. They are not substitutable for each other.
What is a GraphQL resolver?
A resolver is a collection of functions that generate response for a GraphQL query. Simply put, a resolver acts as a GraphQL query handler and provides the correct response to a GraphQL query.