GraphQL, a query language for your API.
What is GraphQL?
API is a specification of possible interaction with a software component. APIs have become most essential components of software infrastructures. GraphQL is a new API standard that provides more efficient, powerful and flexible alternative to REST. This application software was developed and open-sourced by Facebook, and now maintained by a community of companies. GraphQL enables a single endpoint and responds for precisely declared data from clients. It is not a database technology instead it’s a query language for APIs and a runtime() server for fulfilling those queries with your existing data.
Why GraphQL when REST existed?
- Increased mobile usage creates need for more efficient data loading – GraphQL minimizes amount of data that need to be transferred over the network and thus improves application performance.
- Variety of different frontend frameworks and platforms on client side – different frameworks and platforms makes it difficult to build and maintain one API that would fit all the requirements. With GraphQL each client can precisely access data.
- Fast development speed and expectation for rapid feature development – continuous deployments are the most essential step these days in many companies, with frequent product updates. With latest design changes GraphQL does not have endpoint changes since its single API entry from client.
When did GraphQL come into existence?
- It was first developed by Facebook in 2012 and used in all native mobile apps.
- It was first presented publicly at ReactJs Conf in 2012 which was later made an opensource software.
- It lets the client specify the exact data it needs.
- It makes easier to aggregate data from multiple sources.
- It uses a type system to explain kind of data.
- It is adaptable to any programming language and framework.
- Server: A component that serves your API. Servers vary based on the languages.
- Client: Another component to run a GraphQL powered App is a GraphQL client that connects to your endpoint.
- Schema: It is the contract between the server and client, it specifies the capabilities of the API and defines how clients interact with the data. The syntax for writing schemas is named Schema Definition Language (SDL)
Advantages of GraphQL:
- It reduces the number of requests made from the client
- Connection with multiple services.
- Different client platforms:
If a product has different platforms like web app, iOS app, Android app, and developer API, instead of querying for each of those platforms, it will serve all apps and their data sources.
GraphQL vs REST – Benefits:
- REST is not very flexible and inefficient to keep up with the rapidly changing requirements from the clients.
- REST gathers data by accessing multiple endpoints.
- GraphQL client exactly specifies data it needs in a query.
- No More Over fetching and under fetching
Over fetching : REST return fixed data structures. GraphQL returns exact data queried by the client and thus speeds up client performance by loading required data.
Under fetching (n+1): sometimes required data is not returned by a single end point, multiple endpoints are to be hit. Ex: to get followers of users in Facebook, hit /user endpoint and then hit /users/<userId>/followers endpoint for each user to get all required details.
- Rapid changes on frontend:
Clients can specify their exact data requirements, no backend adjustments required when frontend changes its design and data.
- Insightful Analytics on the Backend
Specific data requirements from client helps in evolving an API and deprecating specific fields that are not requested by clients anymore.
GraphQL uses resolver functions within to collect the data that is requested by a client. Instrumenting and measuring performance of those resolvers provides crucial insights about bottlenecks in your system.
- Benefits of a Schema & Type System
Schema actually serves as contract between Frontend and backend teams and they know how data is sent over network thus can work independently.
- GraphQL server with a connected database:
This architecture comes in pictures when a new application is built from scratch and GraphQL is implemented for API interaction, then server reads the query’s payload and fetches the information required from the database. Later it constructs the response object as per the query necessities and returns it to the client.
- GraphQL layer that integrates existing systems:
With the existing systems like Legacy applications, third party APIs, microservices and other architectures new client applications can be developed that can interact with GraphQL server to fetch the data they need. The GraphQL server is then liable for fetching the information from the prevailing systems and package it up in the GraphQL response format.
- Hybrid approach:
This is the combination of both Greenfield case and a legacy case that can interact with a newly created server and a connected database along with an existing system.
This is a tool, or a client application used to execute queries related to GraphQL. This can used for API testing on GraphQL queries.
Only one API call is given for any queries to be fetched and filtered from the application instead of using multiple endpoints.
This is a powerful IDE for interactively working with GraphQL API. It features an editor for queries, mutations and subscriptions, equipped with autocompletion, validation along with documentation explorer to quickly visualize schema structure. It displays query history, also lets you work with multiple GraphQL APIs side-by-side.
Contact for further details:
Varshini B N
Specialist, Digital Transformation