GraphQL(Graph Query Language)
Introduction to GraphQL:
GraphQL is a query language for your API, and a server-side run time for executing queries by using a type system you define for your data
GraphQL invented by Facebook on 2012 support with multiple technology like
C#/.NET,Clojure,Elixir,Erlang,Go,Groovy,Java,JavaScript,PHP,Python,Scala,Ruby
So we can say this API is technology independent
In sort GraphQL is a relatively new concept from Facebook that is billed as an alternative to REST for Web APIs.
Why it called as Alternative of Rest, what is the main purpose to use it?
Let’s discuss with small example
Assume I have one Model i.e. Movie with some fields like below
I have 2 requirements and I developed it using REST API
Now let’s find out what is drawback In Rest that’s why GraphQL came into picture
1. Fetch List of movie info which stored in Database
2. Fetch Movie Info based on Selected MovieId
In first requirement my method return type is List<Movie> so it will give me Movies with all the fields like below
But I don’t want all fields as a part of response which present in Model class means I want only movieName and release Date as part of response so what I can do?
To customize my desired Response I need to create one more separate dto and I need to populate required field to that dto then I have to return that from controller right ?
Then I need to return List<Response> from controller instead of List<Movie> right?
Assume in future client change his requirement and he raised CR (Change Request)
To display actors information along with movieName and releaseDate as part of Response for this API call
Then again I need to add one more field in Response class with name actors and again I need to change business logic in service Think if requirement will keep on change then how many times I need to format my Response dto and business logic
Same for requirement 2, I don’t want to display all fields of movie as a response I want some specific field like movieName and director and producer information so for that again I need to format my Response dto and business
It looks my application is tightly coupled with business so how can we make it generic?
We can make it generic using our Rest API also but we need to write few annotation in Response dto and we need to depend on some third party lib like JsonPath to parse desired response which seems bit burden for developer but which can be more easy using GraphQL
To overcome this issue GraphQL introduced.
Now let’s see how we can ignore above issue using GraphQL
Really it’s a beautiful concept everything u can achieve using type Schema
Type Schema: where we need to specify our desired response format like below
See the line of 6 to 9 who contains our query details like
Line no 7: we are indicating hey my query name is all Movies when I call this Query allMovies then please return List<Movie> with desired fields , but you don’t worry I will send request with required fields which I want as part of response like below
Request Query
I want List of movie with fields’ movieName and director
Tomorrow I want some more field like only movieName and release Date
So notice both Request properly
The fields which we mentioned in Request Query those field data will be fetched from DB and return as Response to us
See below complete Request and Response
Result:
Line no 8:
We are informing to GraphQL based on input movieId Give me Movie Object
As I explain above for requirement 1 same way we can get response which we need based on business so in future requirement is changing then we no need to bother about business logic just change the request query pass the field which you want
Let’s see one Example of req-2
I want a movie object with field movieName, director and actors info only
Request Query
Result:
In future u want releaseDate as part of response then u can add that field name in Request query so you will get required response in proper JSON format like below
This is main purpose of GraphQL but if you will go through official website of GraphQL some more features also available like Queries and Mutation
Source code : Download