A Quick start to GraphQL and Sangria

GraphQL

GraphQL is a data query language developed  by Facebook in 2012 before it was publicly released in 2015. It is a query language for our API, and a server-side runtime for executing queries by using a type system we define for our data.

It gives us an alternative to REST and ad-hoc webservice architectures. It allows the clients to define the structure of the data required, and exactly similar structure of the data is returned from the server. It is a strongly typed runtime which allows clients to tell what data is needed. This avoids both the issues of over-fetching as well as under-fetching of data.

A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type. For example, a GraphQL service is like this:

Once a GraphQL service is running (typically at a URL on a web service), it can be sent GraphQL queries to validate and execute.

Why GraphQL is better then REST API?

GraphQL was developed to meet up with the need for more flexibility and efficiency! It solves many of the disadvantages and inefficiencies that developers experience while interacting with REST APIs.

To illustrate the major differences between REST and GraphQL while fetching data from an API, let’s consider a simple example scenario:

In a blogging application, an app needs to display the titles of the posts of a specific user. The same screen also displays the names of the last 3 followers of that user. How would that situation be solved with REST and GraphQL?

With a REST API, you would typically gather the data by accessing multiple endpoints. There could be endpoints like :/users/, /users//posts, /users//followers for the followers we have to access all the endpoints. In GraphQL on the other hand, we simply send a single query to the GraphQL server that includes the concrete data requirements. The server then responds with a JSON object where all these requirements are fulfilled.

Sangria

Sangria is a Scala GraphQL implementation.
Here is how you can add it to your SBT project:
libraryDependencies += “org.sangria-graphql” %% “sangria” % “1.3.2”

ayoooooshhhh

You can find an example application that uses akka-http with sangria here
The sangria query should have three fields in the post data. Which is shown below :

In the above sangria query it requires the three field ,it’s actually a JSON data. This JSON data wrap the GraphQL query in it. In sangria query, field Query required string while fields variables and operations are optional, depending on the query.

When the graphql query arrives at server, it is parsed by the QueryParser object’s parse() method.

Once the query is parsed, it is passed on to the executor to execute that query.

For the actual practice of the sangria Query click the link provided here

ObjectType is used to represent the data going out of the server. The fields described in this object become part of the JSON data sent to the client.

The argument is used to represent the data that is passed to a query in the Sangria request. The support to submit data to the server is with the InputObjectType. It defines the fields for which we can send data to the server. This object becomes the part of the argument for the Sangria Request.

So, that was a quick start to graphQL and Sangria. It’s as simple as depicted above. I hope it would help you! 🙂

References :

http://sangria-graphql.org/learn/
http://graphql.org/


knoldus-advt-sticker

Written by 

Ayush Aggarwal is a Software Consultant having experience of more than 11 months. He has knowledge of various programming languages such as JAVA, C and C++, and also has good knowledge of database technologies like MySQL. He has worked upon Object Oriented Paradigm and a Java and Android enthusiast. His hobbies include playing football, basketball etc.

Leave a Reply

%d bloggers like this: