My early experiments with GraphQL
The word ‘GraphQL’ struck my head for the first time during fall of 2018 while working on a digital transformation proposal for one of our customer. In order for me to completely interpret proposed architecture, I needed to understand what GraphQL is , what it can do and how it fits the bill for specific ask in the given context. The research on GraphQL led me to the fascinating journey of experimenting with its core features. I started with simple React application with MongoDB backend and GraphQL-Apollo for my sample project. So far, It’s been so much fun to play with GraphQL and thought it may be good idea to share it with my connections to see if anyone else is looking at GraphQL.
First thing first, What’s GraphQL? : The GraphQL is query language for APIs. If the name appears to familiar (or confusing), let me first remove the misconception that may arise from the name - it has nothing to do with graph database. The word ‘graph’ in GraphQL is meant for the way the data is represented. GrpahQL is query language for APIs. It enables API call over single end-point and client can precisely query the back-end through the API query, the response from API will be as per the specification in API query. GraphQL is not tied to specific backend – it can seamlessly be used with existing setup of backend --- databases or APIs.
To Visualize it better - GraphQL layer resides between the front-end applications and backend – the backend could include services(APIs) or any databases (SQL and NoSQL). GraphQL greatly simplifies the interaction between front-end and the backend. It puts client in charge by providing an ability to exactly specify the data they need through its declarative nature. This is a huge plus compare to REST APIs where it exposes all the data available for a given entity and then leaves on client to parse through what’s required at client side. GraphQL API responds with JSON object exactly specifying the key names per the request query – in other words the request query forms the structure of the response and greatly simplifies the read operation from the response.
Here is an example to visualize:
So, Is GraphQL the replacement for REST? Let me put it this way – GraphQL definitely looks whole lot better than REST and offers unique advantages - I wouldn’t pass any judgement at my experimental phase yet. However, several API developers have put out their views on this comparison that can be searched on web. These are some of the notable advantages of GraphQL over REST :
- Client has control over requested data:
Schema is used to describe the data and relationship on the server side using schema definition language. On the client side – Query language is used by clients to request data from a sever
Here is an example schema for classic customer-order scenario.
const typeDefs = ` type Customer {
name: String
age: Int
orders: [String]
}
Additionally, sever-side includes resolver function for a schema fields to respond to the client request. The example resolver function for the above schema definition looks like below:
const resolvers = {
Query: {
customers: () => customers,
}
};
The Query on the client side would look like below:
type Query {
customers: [Customer]
}
- Its definitely more efficient as it serves only the requested data over the single end-point
- GraphQL API version controlling better off than REST : fields can be deprecated over the period of time in GraphQL, unlike in REST where teams end-up managing multiple end-points in this context. BIG PLUS!
How to get started with GraphQL?
Though GraphQL is relatively very new - there’s very detailed documentation out there that can help you have quick start in learning GraphQL. Graphql.org is a great place to start the learning, it contains pretty much everything that you need to get started and to evolve in to the advance concepts along with code examples. You could also follow blogs, videos that tech enthusiasts has published over the internet. However, keep in mind though that the landscape is quickly evolving and you may run in to issue of version incompatibilities in following some of those. Graphql.org is well maintained and I found it very easy to follow. Additionally, one should explore tool called GraphiQL to get hang of how the queries are structured. GraphiQL includes good documentation and one can play with queries using example APIs from GitHub and few other services. Here is an demo URL for GraphiQL : https://graphql.github.io/swapi-graphql/
Is GraphQL worth spending time on? : Absolutely yes. GraphQL is quickly getting attention in the industry and carries huge potential of getting into mainstream due to its unique advantages over REST. With application modernization, Cloud migration and serverless architecture adoption on top of the list of digital transformation for many companies – GraphQL is positioned really well. Another way to look at its potential à is to look at its journey so far. It started as an in-house project within Facebook before being publicly released in 2015, since then there’s constant traction in its adoption and usage. Many big companies along with Facebook are already using it in production. GraphQL libraries on npmtrends are showing consistent rise. I wouldn’t be surprised if by end if 2019 – GraphQL becomes mainstream technology for building APIs.
Sr. Manager- Projects @ Cognizant | SAFe, CSM
6 å¹´Good read Ramesh! Thanks for sharing!
Program Delivery Director, Product Manager, Digital Transformation & Business Development (top and bottom-line revenue)
6 å¹´Gone through this article, very nice and informative. Thank you for sharing, Ramesh
Senior QE Strategist | Cybersecurity Expert | Transformational Leader in Tech
6 å¹´Very interesting read! Thanks for sharing Ramesh Bar, CSM