Exit e-book
Show all chapters

Sign up to our Newsletter

Signing up to our newsletter allows you to read all our ebooks.

    How To GraphQL with Scala


    GraphQL is a specification developed by Facebook which describes the new API syntax for managing backend data. This syntax is in many ways a much more efficient and powerful alternative to REST. The main advantages of GraphQL are:

    The client may specify exactly what data is needed.

    GraphQL minimizes the amount of data transferred over a network noticeably improves application speed, it prevents underfetching. In REST some endpoints don’t provide enough required data and the client has to make additional requests to get all needed information.

    Aggregating data is easier.

    You can develop a backend that is a facade for multiple data sources and with GraphQL, you can aggregate it transparently for the client. It’s easier to get all related data in one query. Data described in the type system. One of the advantages is that both teams (frontend and backend) could work independently if both sides know schema from the beginning. Adding new fields is almost transparent for the client, so you can evolve an API without changing any client’s code! The specification also supports providing information about deprecation of fields – client is able to still use this field, but knows that it will be removed in the future and will be prepared for this change. I would say that GraphQL Schema is a contract and API documentation in one file.

    Some History and context

    Before 2012 Facebook has put an effort into developing a mobile client for their platform. Instead of sending a lot of requests, they have decided to go for a Data­Driven API.

    Such API was supposed to be simple to learn and powerful to describe all of Facebook. GraphQL was born to fill all those needs. But Specification was patented in 2012 and many clients didn’t use it because of that.

    GraphQL became more and more popular over the years. In November 2017 Facebook decided to change the license to MIT. Since then you can use it even for commercial purposes. Now, the popularity of a specification and community around graphql.org is growing continuously.

    For example, You can find GraphQL as a common API standard for:

    Just naming a few above. To see more visit: https://graphql.org/users/

    Basic comparison to REST

    1. GraphQL and REST are totally different
    2. Both aren’t competitors, GraphQL is not a descendant.
    3. You can use them simultaneously in one project.
    4. Moreover, GraphQL isn’t a magic bullet and …
    5. One could be “better” than the other provided that they are used properly.


    Core GraphQL Concept

    GraphQL is a query string sent to the server. That query defines what data should be fetched and returned in JSON format.

    GraphQL defines the shape of data. The response is mostly a mirror of a query, so you can predict what you’ll get back from the server.

    GraphQL has a hierarchical nature, it follows relationships between objects. Usually, in REST you have to make multiple requests and then merge responses
    to achieve the same result.

    Graph Databases uses a similar structure internally and many of them provide.

    GraphQL-friendly interfaces for interchanging data.

    GraphQL has defined a particular type on each level of query and each of that type has defined available fields. It allows the client to verify a query before executing it. It also enables query introspection and rich tooling e.g. auto-completion. Moreover, the client could fetch this information {types/fields) beforehand, which is used by tools to speed up the developing process.

    Finally, the user gets access to possible query structure and response types.


    Not a fan of reading online?

    Download PDF