An Introduction to GraphQL
Posted By : Ankit Uniyal | 31-Oct-2018
In this blog, we are going to explore what GraphQL is why it's useful and understand its basic query syntax.
GraphQL was designed by Facebook and it is a query language for your API through which you can provide the complete description of your data in the API.
1.GraphQL is fast.
2.GraphQL is flexible.
3.GraphQL is easy to use at simple to maintain.
GraphQL can operate on HTTP thus we can make HTTP request using GraphQL.The major thing about GraphQL query is that where Client determines what should come back from the server whereas in traditional REST API endpoints, the server determines what data should come back from the server.
With a REST API if a client needs different data it typically requires us to added new endpoints or changes an existing one. Using a GraphQL API though, the client just needs to change its query, making graphQL API's much simpler to maintain.
Thus, GraphQL creates fast and flexible APIs, giving clients complete control to ask for just the data they need. Fewer HTTP requests with flexible data querying and Less code to manage.
Now, before getting into more detail, let's discuss Why GraphQL?
The main goal behind designing the GraphQL was to make the query smarter. Now, so far we have been designing queries and designing the endpoints such as an API's pretty easily but sometimes the only problem with them is over fetching and under fetching. Sometimes the data is so less that you have to make another query or sometimes the data is coming is so much that you don't use all of the data, you use only selected point of data. Nothing big much of a worry when you are at a scale of big companies like Facebook or google where millions or billions of people are just querying at every single second then it can be a little bit of expensive on the cost of the server.
The GraphQl exposes the single endpoint as you will send a query to the endpoint with Special Query Language syntax and that query is basically a string. Now, the server returns JSON object to the query.
A basic example is let's say to get the name of the person whose id=1.
GET /graphql?query={ person(id: "1") { name } }
Or we can also use:
{ person(id: "1") { name } }
Response:
{ "name": "Ankit" }
Stay tuned for more updates.
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankit Uniyal
Ankit has knowledge in Javascript, NodeJS, AngularJS and MongoDB also have experience in using AWS Services.